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;
private long fEndTime = -1;
private List<ITimeEvent> fEventList = new ArrayList<>();
private List<ITimeEvent> fZoomedEventList = new ArrayList<>();
+ private Comparator<ITimeGraphEntry> fComparator;
/**
* Constructor
}
@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;
}
/*
* 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);
+ }
}
/**
* 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 + ')';