statesystem : Add a default compareTo() for segments
authorJean-Christian Kouame <jean-christian.kouame@ericsson.com>
Tue, 1 Mar 2016 17:46:20 +0000 (12:46 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 3 Mar 2016 16:31:45 +0000 (11:31 -0500)
Implements a default compareTo() in ISegment. The comparison will be
based on the start and the end of the segment. Subclasses could extend
this default behaviour if needed.
Also, the interface become Comparable<@NonNull ISegment>.

Change-Id: Ib58e0ba80c0151d127254c8b87eafec57e624566
Signed-off-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/67620
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
analysis/org.eclipse.tracecompass.analysis.os.linux.core/src/org/eclipse/tracecompass/analysis/os/linux/core/latency/SystemCall.java
statesystem/org.eclipse.tracecompass.segmentstore.core/src/org/eclipse/tracecompass/segmentstore/core/BasicSegment.java
statesystem/org.eclipse.tracecompass.segmentstore.core/src/org/eclipse/tracecompass/segmentstore/core/ISegment.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/segment/TmfXmlPatternSegment.java

index 9cec0882b790a79390401a572ee036cfd9f71394..d89ff4e46dffe8c22f2a8f339c13e85272717d89 100644 (file)
@@ -9,19 +9,14 @@
 
 package org.eclipse.tracecompass.analysis.os.linux.core.latency;
 
-import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
-
 import java.io.Serializable;
-import java.util.Comparator;
 import java.util.Map;
 
-import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.segmentstore.core.ISegment;
-import org.eclipse.tracecompass.segmentstore.core.SegmentComparators;
 
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Ordering;
 
 /**
  * A linux kernel system call, represented as an {@link ISegment}.
@@ -33,12 +28,6 @@ public class SystemCall implements ISegment {
 
     private static final long serialVersionUID = 1554494342105208730L;
 
-    private static final Comparator<ISegment> COMPARATOR = checkNotNull(Ordering
-            .from(SegmentComparators.INTERVAL_START_COMPARATOR)
-            .compound(SegmentComparators.INTERVAL_END_COMPARATOR)
-             /* Kind of lazy, but should work! */
-            .compound(Ordering.usingToString()));
-
     /**
      * The subset of information that is available from the syscall entry event.
      */
@@ -127,11 +116,12 @@ public class SystemCall implements ISegment {
     }
 
     @Override
-    public int compareTo(@Nullable ISegment o) {
-        if (o == null) {
-            throw new IllegalArgumentException();
+    public int compareTo(@NonNull ISegment o) {
+        int ret = ISegment.super.compareTo(o);
+        if (ret != 0) {
+            return ret;
         }
-        return COMPARATOR.compare(this, o);
+        return toString().compareTo(o.toString());
     }
 
     @Override
index 61b12b9997aaa4470c149798f49b356a706554d3..b1e686904390e4d8f922c3cdbce9590d94fcb01d 100644 (file)
@@ -9,14 +9,6 @@
 
 package org.eclipse.tracecompass.segmentstore.core;
 
-import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
-
-import java.util.Comparator;
-
-import org.eclipse.jdt.annotation.Nullable;
-
-import com.google.common.collect.Ordering;
-
 /**
  * Basic implementation of {@link ISegment}.
  *
@@ -26,16 +18,6 @@ public class BasicSegment implements ISegment {
 
     private static final long serialVersionUID = -3257452887960883177L;
 
-    private static final Comparator<ISegment> COMPARATOR;
-    static {
-        /* checkNotNull() has to be called separately, or else it breaks the
-         * type inference. */
-        Comparator<ISegment> comp = Ordering
-                .from(SegmentComparators.INTERVAL_START_COMPARATOR)
-                .compound(SegmentComparators.INTERVAL_END_COMPARATOR);
-        COMPARATOR = checkNotNull(comp);
-    }
-
     private final long fStart;
     private final long fEnd;
 
@@ -67,14 +49,6 @@ public class BasicSegment implements ISegment {
         return fEnd;
     }
 
-    @Override
-    public int compareTo(@Nullable ISegment o) {
-        if (o == null) {
-            throw new IllegalArgumentException();
-        }
-        return COMPARATOR.compare(this, o);
-    }
-
     @Override
     public String toString() {
         return new String('[' + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + ']'); //$NON-NLS-1$
index e82cf387d8322b42e11f469a546f82695f536627..eff096573ab314292ec92bfd0ffd3e5259d6285c 100644 (file)
@@ -14,13 +14,15 @@ package org.eclipse.tracecompass.segmentstore.core;
 
 import java.io.Serializable;
 
+import org.eclipse.jdt.annotation.NonNull;
+
 /**
  * Generic interface for any segment (like a time range) that can be used in the
  * segment store.
  *
  * @author Alexandre Montplaisir
  */
-public interface ISegment extends Serializable, Comparable<ISegment> {
+public interface ISegment extends Serializable, Comparable<@NonNull ISegment> {
 
     /**
      * The start position/time of the segment.
@@ -36,6 +38,13 @@ public interface ISegment extends Serializable, Comparable<ISegment> {
      */
     long getEnd();
 
+    @Override
+    default int compareTo(@NonNull ISegment arg0) {
+        return SegmentComparators.INTERVAL_START_COMPARATOR
+                .thenComparing(SegmentComparators.INTERVAL_END_COMPARATOR)
+                .compare(this, arg0);
+    }
+
     /**
      * The length/duration of the segment.
      *
index 300e73098cb51e91e409293fac9d3475508957c4..78c3edf1fb028f81e62e1d1a0e16f4592516c690 100644 (file)
@@ -12,21 +12,16 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.segmentstore.core.ISegment;
-import org.eclipse.tracecompass.segmentstore.core.SegmentComparators;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 
-import com.google.common.collect.Ordering;
-
 /**
  * This class implements an XML Pattern Segment. This type of segment has
  * content and a default timestamp, which is the start time of the segment.
@@ -48,12 +43,6 @@ public class TmfXmlPatternSegment implements ISegment {
     private static final byte TYPE_STRING = 1;
     private static final byte TYPE_LONG = 2;
 
-    private static final @NonNull Comparator<ISegment> COMPARATOR = Ordering
-            .from(SegmentComparators.INTERVAL_START_COMPARATOR)
-            .compound(SegmentComparators.INTERVAL_END_COMPARATOR)
-             /* Kind of lazy, but should work! */
-            .compound(Ordering.usingToString());
-
     private final int fScale;
     private final long fStart;
     private final long fEnd;
@@ -125,11 +114,12 @@ public class TmfXmlPatternSegment implements ISegment {
     }
 
     @Override
-    public int compareTo(@Nullable ISegment o) {
-        if (o == null) {
-            throw new IllegalArgumentException("Cannot compare to null"); //$NON-NLS-1$
+    public int compareTo(@NonNull ISegment o) {
+        int ret = ISegment.super.compareTo(o);
+        if (ret != 0) {
+            return ret;
         }
-        return COMPARATOR.compare(this, o);
+        return toString().compareTo(o.toString());
     }
 
     @Override
This page took 0.033147 seconds and 5 git commands to generate.