JAL-3253-applet JAL-3397 JAL-3383 fast IntervalStore for JavaScript
[jalview.git] / src / intervalstore / api / IntervalI.java
index 0dfd935..c170a43 100644 (file)
@@ -37,6 +37,33 @@ import java.util.List;
 
 public interface IntervalI
 {
+
+  /**
+   * Compare intervals by start position ascending and end position descending.
+   */
+  static Comparator<? super IntervalI> COMPARATOR_BIGENDIAN = new Comparator<IntervalI>()
+  {
+    @Override
+    public int compare(IntervalI o1, IntervalI o2)
+    {
+      int ret = Integer.signum(o1.getBegin() - o2.getBegin());
+      return (ret == 0 ? Integer.signum(o2.getEnd() - o1.getEnd()) : ret);
+    }
+  };
+
+  /**
+   * Compare intervals by start position ascending and end position ascending.
+   */
+  static Comparator<? super IntervalI> COMPARATOR_LITTLEENDIAN = new Comparator<IntervalI>()
+  {
+    @Override
+    public int compare(IntervalI o1, IntervalI o2)
+    {
+      int ret = Integer.signum(o1.getBegin() - o2.getBegin());
+      return (ret == 0 ? Integer.signum(o1.getEnd() - o2.getEnd()) : ret);
+    }
+  };
+
   /**
    * a comparator for sorting intervals by start position ascending
    */
@@ -45,7 +72,7 @@ public interface IntervalI
     @Override
     public int compare(IntervalI o1, IntervalI o2)
     {
-      return Integer.compare(o1.getBegin(), o2.getBegin());
+      return Integer.signum(o1.getBegin() - o2.getBegin());
     }
   };
 
@@ -57,26 +84,30 @@ public interface IntervalI
     @Override
     public int compare(IntervalI o1, IntervalI o2)
     {
-      return Integer.compare(o2.getEnd(), o1.getEnd());
+      return Integer.signum(o2.getEnd() - o1.getEnd());
     }
   };
 
-  int getBegin();
+  static int NOT_CONTAINED = Integer.MIN_VALUE;
+  static int CONTAINMENT_UNKNOWN = 0;
 
+  int getBegin();
   int getEnd();
 
   /**
    * Answers true if this interval contains (or matches) the given interval
+   * based solely on start and end.
    * 
    * @param i
    * @return
    */
   default boolean containsInterval(IntervalI i)
   {
-    return i != null
-            && i.getBegin() >= getBegin() && i.getEnd() <= getEnd();
+    return i != null && i.getBegin() >= getBegin()
+            && i.getEnd() <= getEnd();
   }
 
+
   /**
    * Answers true if this interval properly contains the given interval, that
    * is, it contains it and is larger than it
@@ -90,11 +121,41 @@ public interface IntervalI
             && (i.getBegin() > getBegin() || i.getEnd() < getEnd());
   }
 
-  default boolean equalsInterval(IntervalI i)
-  {
-    return i != null && i.getBegin() == getBegin()
-            && i.getEnd() == getEnd();
-  }
+  /**
+   * Slower than equalsInterval; also includes type.
+   * 
+   * Ensure that subclasses override equals(Object). For example:
+   * 
+   * public boolean equals(Object o) { return o != null && o instanceof XXX &&
+   * equalsInterval((XXX) i); }
+   * 
+   * 
+   * equalsInterval also must be overridden.
+   * 
+   * public boolean equalsInterval(IntervalI i) {return ((SimpleFeature)i).start
+   * == start && ((SimpleFeature)i).end == end && ((SimpleFeature)i).description
+   * == this.description; }
+   * 
+   * 
+   * @param o
+   * @return true if equal, including a type check
+   */
+  @Override
+  abstract boolean equals(Object o);
+
+
+
+  /**
+   * Check that two intervals are equal, in terms of end points, descriptions,
+   * or any other distinguishing features.
+   * 
+   * Used in IntervalStore in searches, since it is faster than equals(), as at
+   * that point we know that we have the correct type.
+   * 
+   * @param i
+   * @return true if equal
+   */
+  abstract boolean equalsInterval(IntervalI i);
 
   default boolean overlapsInterval(IntervalI i)
   {
@@ -127,8 +188,5 @@ public interface IntervalI
             forwardStrand ? FORWARD_STRAND : REVERSE_STRAND);
   }
 
-  IntervalI getContainedBy();
-
-  void setContainedBy(IntervalI containedBy);
 
 }