ss: introduce getIteratorOverQuark
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / statesystem / core / StateSystemUtils.java
index a3f8d70e99fc2332dcea7d24f92c67fc314e7d32..6999544e9032b27e39bcfc6e508f1705b1360b31 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2014, 2015 École Polytechnique de Montréal
+ * Copyright (c) 2014, 2016 École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
 package org.eclipse.tracecompass.statesystem.core;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.NoSuchElementException;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
@@ -273,10 +275,78 @@ public final class StateSystemUtils {
                 }
                 current = currentInterval.getEndTime() + 1;
             }
-        } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) {
+        } catch (StateSystemDisposedException | TimeRangeException e) {
             /* Nothing to do */
         }
         return null;
     }
 
+    /**
+     * Iterator over the intervals of a given attribute. Not thread-safe.
+     *
+     * @param ss
+     *            The state system on which to query intervals
+     *
+     * @param quark
+     *            The key to the attribute to iterate over
+     * @param start
+     *            The begin of the range of intervals to iterate over, if start
+     *            is before the state system's start time, the iteration will
+     *            begin at the state system's start time.
+     * @param end
+     *            The end of the range of intervals to iterate over, it can be
+     *            greater than the current state system's end time, iteration
+     *            will end at the smallest of the two.
+     * @return an Iterator over the intervals of a given attribute, with
+     *         intervals ordered
+     * @throws TimeRangeException
+     *             If end < start.
+     * @since 2.1
+     */
+    public static Iterator<ITmfStateInterval> getIteratorOverQuark(ITmfStateSystem ss, int quark, long start, long end) {
+        if (end < start) {
+            throw new TimeRangeException("iterateOverQuark: end < start !"); //$NON-NLS-1$
+        }
+
+        return new Iterator<ITmfStateInterval>() {
+            private @Nullable ITmfStateInterval fCurrent;
+
+            private long getNextQueryTime() {
+                if (fCurrent != null) {
+                    return fCurrent.getEndTime() + 1;
+                }
+                /* Iteration has not started yet */
+                return Long.max(start, ss.getStartTime());
+            }
+
+            @Override
+            public boolean hasNext() {
+                /*
+                 * Compute the query's real end time here, to update it if the
+                 * iterator is used during state system build
+                 */
+                long realEnd = Long.min(end, ss.getCurrentEndTime());
+
+                /*
+                 * Ensure that the next query time falls within state system and
+                 * query time range. By definition getNextQueryTime() is larger
+                 * than the state system's start time.
+                 */
+                return getNextQueryTime() <= realEnd;
+            }
+
+            @Override
+            public ITmfStateInterval next() {
+                if (hasNext()) {
+                    try {
+                        return fCurrent = ss.querySingleState(getNextQueryTime(), quark);
+                    } catch (StateSystemDisposedException e) {
+                        /* GOTO throw NoSuchElementException. */
+                    }
+                }
+                throw new NoSuchElementException();
+            }
+        };
+    }
+
 }
This page took 0.025716 seconds and 5 git commands to generate.