segstore: introduce sorted iterators
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.segmentstore.core / src / org / eclipse / tracecompass / segmentstore / core / ISegmentStore.java
index 2e4be6a6594251bffd946911cbfe5bcc9b64f08e..132f08a9a68f3178e1b285fe6d4720cf9af94c2f 100644 (file)
 package org.eclipse.tracecompass.segmentstore.core;
 
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.jdt.annotation.NonNull;
+
+import com.google.common.collect.Lists;
 
 /**
  * Interface for segment-storing backends.
@@ -25,6 +33,18 @@ import java.util.Collection;
  */
 public interface ISegmentStore<E extends ISegment> extends Collection<E> {
 
+    /**
+     * Sorted Iterator
+     *
+     * @param order
+     *            The desired order for the returned iterator
+     * @return An iterator over all the segments in the store in the desired order
+     * @since 1.1
+     */
+    default Iterable<E> iterator(Comparator<ISegment> order){
+        return getIntersectingElements(Long.MIN_VALUE, Long.MAX_VALUE, order);
+    }
+
     /**
      * Retrieve all elements that inclusively cross the given position.
      *
@@ -33,7 +53,25 @@ public interface ISegmentStore<E extends ISegment> extends Collection<E> {
      *            tree's X axis represents time.
      * @return The intervals that cross this position
      */
-    Iterable<E> getIntersectingElements(long position);
+    default Iterable<E> getIntersectingElements(long position){
+        return getIntersectingElements(position, position);
+    }
+
+    /**
+     * Retrieve all elements that inclusively cross the given position, sorted
+     * in the specified order.
+     *
+     * @param position
+     *            The target position. This would represent a timestamp, if the
+     *            tree's X axis represents time.
+     * @param order
+     *            The desired order for the returned iterator
+     * @return The intervals that cross this position
+     * @since 1.1
+     */
+    default Iterable<E> getIntersectingElements(long position, Comparator<ISegment> order) {
+        return getIntersectingElements(position, position, order);
+    }
 
     /**
      * Retrieve all elements that inclusively cross another segment. We define
@@ -54,6 +92,31 @@ public interface ISegmentStore<E extends ISegment> extends Collection<E> {
      */
     Iterable<E> getIntersectingElements(long start, long end);
 
+    /**
+     * Retrieve all elements that inclusively cross another segment, sorted in
+     * the specified order. We define this target segment by its start and end
+     * positions.
+     *
+     * @param start
+     *            The target start position
+     * @param end
+     *            The target end position
+     * @param order
+     *            The desired order for the returned iterator
+     * @return The intervals that cross this position
+     * @since 1.1
+     */
+    default Iterable<E> getIntersectingElements(long start, long end, Comparator<ISegment> order){
+        List<E> list = Lists.newArrayList(getIntersectingElements(start, end));
+        return new Iterable<@NonNull E>() {
+            @Override
+            public Iterator<@NonNull E> iterator() {
+                Collections.sort(list, order);
+                return list.iterator();
+            }
+        };
+    }
+
     /**
      * Dispose the data structure and release any system resources associated
      * with it.
This page took 0.026193 seconds and 5 git commands to generate.