From: Matthew Khouzam Date: Thu, 29 Jan 2015 14:51:35 +0000 (-0500) Subject: ctf: make StreamInputPacketIndexEntry not Comparable X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=c1d0f6ca3147e8344d831f3ee32dc5b0e67f9ab0;p=deliverable%2Ftracecompass.git ctf: make StreamInputPacketIndexEntry not Comparable Move the comparator to the StreamInputPacketIndex so that it is not a true compareTo. This makes sense as compareTo needs to be a true comparison not a partial compare. If compare returns 0, it should be equals, not more or less equivalent. Change-Id: I614c9f7d1eb48ac239abd7921b0fcf60fda76be3 Signed-off-by: Matthew Khouzam Reviewed-on: https://git.eclipse.org/r/40631 Reviewed-by: Hudson CI Reviewed-by: Alexandre Montplaisir --- diff --git a/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndex.java b/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndex.java index 5cb4c5fd5b..0b0665fff4 100644 --- a/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndex.java +++ b/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndex.java @@ -15,9 +15,11 @@ package org.eclipse.tracecompass.internal.ctf.core.trace; import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.ListIterator; @@ -226,9 +228,32 @@ public class StreamInputPacketIndex { public int indexOf(StreamInputPacketIndexEntry element) { int indexOf = -1; if (element != null) { - indexOf = Collections.binarySearch(fEntries, element); + indexOf = Collections.binarySearch(fEntries, element, new MonotonicComparator()); } return (indexOf < 0) ? -1 : indexOf; } + /** + * Ordering comparator for entering entries into a data structure sorted by timestamp. + */ + private static class MonotonicComparator implements Comparator { + + @Override + public int compare(StreamInputPacketIndexEntry left, StreamInputPacketIndexEntry right) { + if (left.getTimestampBegin() > right.getTimestampBegin()) { + return 1; + } + if (left.getTimestampBegin() < right.getTimestampBegin()) { + return -1; + } + if (left.getTimestampEnd() > right.getTimestampEnd()) { + return 1; + } + if (left.getTimestampEnd() < right.getTimestampEnd()) { + return -1; + } + return 0; + } + } + } diff --git a/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndexEntry.java b/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndexEntry.java index 8d72d1a849..97a477725b 100644 --- a/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndexEntry.java +++ b/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndexEntry.java @@ -20,7 +20,7 @@ import java.util.Map; *

* Represents an entry in the index of event packets. */ -public class StreamInputPacketIndexEntry implements Comparable { +public class StreamInputPacketIndexEntry { // ------------------------------------------------------------------------ // Attributes @@ -64,7 +64,7 @@ public class StreamInputPacketIndexEntry implements Comparable fAttributes = new HashMap<>(); - // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ @@ -205,7 +204,8 @@ public class StreamInputPacketIndexEntry implements Comparable o.fTimestampBegin) { - return 1; - } - if (fTimestampBegin < o.fTimestampBegin) { - return -1; - } - if (fTimestampEnd > o.fTimestampEnd) { - return 1; - } - if (fTimestampEnd < o.fTimestampEnd) { - return -1; - } - return 0; - } }