ctf: make StreamInputPacketIndexEntry not Comparable
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 29 Jan 2015 14:51:35 +0000 (09:51 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 6 Feb 2015 22:08:48 +0000 (17:08 -0500)
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 <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/40631
Reviewed-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndex.java
org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndexEntry.java

index 5cb4c5fd5bd733844f3748773d4340167b3e7e1a..0b0665fff41b4021539c0c330bf66a2536dfed56 100644 (file)
 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<StreamInputPacketIndexEntry> {
+
+        @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;
+        }
+    }
+
 }
index 8d72d1a8498a05bba19ae8f02bf206f4ca84bcf4..97a477725bafd8056b1a8a3036955cb7aa64c808 100644 (file)
@@ -20,7 +20,7 @@ import java.util.Map;
  * <p>
  * Represents an entry in the index of event packets.
  */
-public class StreamInputPacketIndexEntry implements Comparable<StreamInputPacketIndexEntry> {
+public class StreamInputPacketIndexEntry {
 
     // ------------------------------------------------------------------------
     // Attributes
@@ -64,7 +64,7 @@ public class StreamInputPacketIndexEntry implements Comparable<StreamInputPacket
     /**
      * Which target is being traced
      */
-    private String fTarget ;
+    private String fTarget;
     private long fTargetID;
 
     /**
@@ -72,7 +72,6 @@ public class StreamInputPacketIndexEntry implements Comparable<StreamInputPacket
      */
     private final Map<String, Object> fAttributes = new HashMap<>();
 
-
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -205,7 +204,8 @@ public class StreamInputPacketIndexEntry implements Comparable<StreamInputPacket
     }
 
     /**
-     * @param lostEvents the lostEvents to set
+     * @param lostEvents
+     *            the lostEvents to set
      */
     public void setLostEvents(long lostEvents) {
         fLostEvents = lostEvents;
@@ -230,7 +230,7 @@ public class StreamInputPacketIndexEntry implements Comparable<StreamInputPacket
      *            The name of the attribute
      * @return The value that was stored, or null if it wasn't found
      */
-    public Object lookupAttribute(String field){
+    public Object lookupAttribute(String field) {
         return fAttributes.get(field);
     }
 
@@ -255,24 +255,7 @@ public class StreamInputPacketIndexEntry implements Comparable<StreamInputPacket
     /**
      * @return The ID of the target
      */
-    public long getTargetId(){
+    public long getTargetId() {
         return fTargetID;
     }
-
-    @Override
-    public int compareTo(StreamInputPacketIndexEntry o) {
-        if (fTimestampBegin > o.fTimestampBegin) {
-            return 1;
-        }
-        if (fTimestampBegin < o.fTimestampBegin) {
-            return -1;
-        }
-        if (fTimestampEnd > o.fTimestampEnd) {
-            return 1;
-        }
-        if (fTimestampEnd < o.fTimestampEnd) {
-            return -1;
-        }
-        return 0;
-    }
 }
This page took 0.029824 seconds and 5 git commands to generate.