tmf: Add sorting capability to TimeGraphEntry
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / model / TimeGraphEntry.java
index d4ae0509664d4d42f153166540d099c5c22bf967..8518c577aa2cf8c7ccfb7abbe2eb19e44b470efa 100644 (file)
@@ -14,6 +14,8 @@
 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
@@ -37,6 +39,7 @@ public class TimeGraphEntry implements ITimeGraphEntry {
     private long fEndTime = -1;
     private List<ITimeEvent> fEventList = new ArrayList<>();
     private List<ITimeEvent> fZoomedEventList = new ArrayList<>();
+    private Comparator<ITimeGraphEntry> fComparator;
 
     /**
      * Constructor
@@ -89,12 +92,12 @@ public class TimeGraphEntry implements ITimeGraphEntry {
     }
 
     @Override
-    public boolean hasChildren() {
+    public synchronized boolean hasChildren() {
         return fChildren.size() > 0;
     }
 
     @Override
-    public List<? extends ITimeGraphEntry> getChildren() {
+    public synchronized List<? extends ITimeGraphEntry> getChildren() {
         return fChildren;
     }
 
@@ -220,23 +223,38 @@ public class TimeGraphEntry implements ITimeGraphEntry {
     /*
      * TODO: This method can be removed in the next major API version.
      */
-    public void addChild(TimeGraphEntry child) {
-        child.fParent = this;
-        fChildren.add(child);
+    public synchronized void addChild(TimeGraphEntry child) {
+        addChild((ITimeGraphEntry) child);
     }
 
     /**
-     * Add a child entry to this one
+     * Add a child entry to this one. If a comparator was previously set with
+     * {@link #sortChildren(Comparator)}, the entry will be inserted in its
+     * sort-order position. Otherwise it will be added to the end of the list.
      *
      * @param child
      *            The child entry
      * @since 3.1
      */
-    public void addChild(ITimeGraphEntry child) {
+    public synchronized void addChild(ITimeGraphEntry child) {
+        /*
+         * TODO: Use setParent() once it is added to the interface.
+         */
         if (child instanceof TimeGraphEntry) {
             ((TimeGraphEntry) child).fParent = this;
         }
-        fChildren.add(child);
+        if (fComparator == null) {
+            fChildren.add(child);
+        } else {
+            int i;
+            for (i = 0; i < fChildren.size(); i++) {
+                ITimeGraphEntry entry = fChildren.get(i);
+                if (fComparator.compare(child, entry) < 0) {
+                    break;
+                }
+            }
+            fChildren.add(i, child);
+        }
     }
 
     /**
@@ -248,13 +266,36 @@ public class TimeGraphEntry implements ITimeGraphEntry {
      *            The child entry
      * @since 3.1
      */
-    public void addChild(int index, ITimeGraphEntry child) {
+    public synchronized void addChild(int index, ITimeGraphEntry child) {
+        /*
+         * TODO: Use setParent() once it is added to the interface.
+         */
         if (child instanceof TimeGraphEntry) {
             ((TimeGraphEntry) child).fParent = this;
         }
         fChildren.add(index, child);
     }
 
+    /**
+     * Sort the children of this entry using the provided comparator. Subsequent
+     * calls to {@link #addChild(ITimeGraphEntry)} will use this comparator to
+     * maintain the sort order.
+     *
+     * @param comparator
+     *            The entry comparator
+     * @since 3.1
+     */
+    public synchronized void sortChildren(Comparator<ITimeGraphEntry> comparator) {
+        fComparator = comparator;
+        if (comparator == null) {
+            return;
+        }
+        ITimeGraphEntry[] array = fChildren.toArray(new ITimeGraphEntry[0]);
+        Arrays.sort(array, comparator);
+        fChildren.clear();
+        fChildren.addAll(Arrays.asList(array));
+    }
+
     @Override
     public String toString() {
         return getClass().getSimpleName() + '(' + fName + ')';
This page took 0.026102 seconds and 5 git commands to generate.