JAL-3397 final update
[jalview.git] / src / intervalstore / impl / IntervalStore.java
index 8634ad4..9faae7f 100644 (file)
@@ -216,7 +216,8 @@ public class IntervalStore<T extends IntervalI>
        * find the first stored interval which doesn't precede the new one
        */
       int insertPosition = BinarySearcher.findFirst(nonNested,
-              val -> val.getBegin() >= entry.getBegin());
+              entry.getBegin(),
+              BinarySearcher.fbegin);
       /*
        * fail if we detect interval enclosure 
        * - of the new interval by the one before or after it
@@ -224,7 +225,8 @@ public class IntervalStore<T extends IntervalI>
        */
       if (insertPosition > 0)
       {
-        if (nonNested.get(insertPosition - 1).properlyContainsInterval(entry))
+        if (nonNested.get(insertPosition - 1)
+                .properlyContainsInterval(entry))
         {
           return false;
         }
@@ -257,7 +259,7 @@ public class IntervalStore<T extends IntervalI>
 
     if (nested != null)
     {
-      result.addAll(nested.findOverlaps(from, to));
+      nested.findOverlaps(from, to, result);
     }
 
     return result;
@@ -269,7 +271,7 @@ public class IntervalStore<T extends IntervalI>
     String pp = nonNested.toString();
     if (nested != null)
     {
-      pp += System.lineSeparator() + nested.prettyPrint();
+      pp += '\n' + nested.prettyPrint();
     }
     return pp;
   }
@@ -358,13 +360,14 @@ public class IntervalStore<T extends IntervalI>
      * start position is not less than the target range start
      * (NB inequality test ensures the first match if any is found)
      */
-    int startIndex = BinarySearcher.findFirst(nonNested,
-            val -> val.getBegin() >= entry.getBegin());
+    int from = entry.getBegin();
+    int startIndex = BinarySearcher.findFirst(nonNested, from,
+            BinarySearcher.fbegin);
 
     /*
      * traverse intervals to look for a match
      */
-    int from = entry.getBegin();
+
     int i = startIndex;
     int size = nonNested.size();
     while (i < size)
@@ -429,13 +432,14 @@ public class IntervalStore<T extends IntervalI>
     /*
      * locate the first entry in the list which does not precede the interval
      */
-    int pos = BinarySearcher.findFirst(intervals,
-            val -> val.getBegin() >= interval.getBegin());
+    int from = interval.getBegin();
+    int pos = BinarySearcher.findFirst(intervals, from,
+            BinarySearcher.fbegin);
     int len = intervals.size();
     while (pos < len)
     {
       T sf = intervals.get(pos);
-      if (sf.getBegin() > interval.getBegin())
+      if (sf.getBegin() > from)
       {
         return false; // no match found
       }
@@ -482,23 +486,19 @@ public class IntervalStore<T extends IntervalI>
      * find the first interval whose end position is
      * after the target range start
      */
-    int startIndex = BinarySearcher.findFirst(nonNested,
-            val -> val.getEnd() >= from);
-
-    final int startIndex1 = startIndex;
-    int i = startIndex1;
-    while (i < nonNested.size())
+    int startIndex = BinarySearcher.findFirst(nonNested, (int) from,
+            BinarySearcher.fend);
+    for (int i = startIndex, n = nonNested.size(); i < n; i++)
     {
       T sf = nonNested.get(i);
       if (sf.getBegin() > to)
       {
         break;
       }
-      if (sf.getBegin() <= to && sf.getEnd() >= from)
+      if (sf.getEnd() >= from)
       {
         result.add(sf);
       }
-      i++;
     }
   }
 
@@ -508,7 +508,8 @@ public class IntervalStore<T extends IntervalI>
     String s = nonNested.toString();
     if (nested != null)
     {
-      s = s + System.lineSeparator() + nested.toString();
+      s = s + '\n'// + System.lineSeparator()
+              + nested.toString();
     }
     return s;
   }
@@ -516,28 +517,39 @@ public class IntervalStore<T extends IntervalI>
   @Override
   public int getWidth()
   {
-    // TODO Auto-generated method stub
-    return 0;
+    return (nonNested == null ? 0 : nonNested.size())
+            + (nested == null ? 0 : nested.size());
   }
 
   @Override
   public List<T> findOverlaps(long start, long end, List<T> result)
   {
-    // TODO Auto-generated method stub
-    return null;
+    return findOverlaps(start, end);
   }
 
   @Override
   public boolean revalidate()
   {
-    // TODO Auto-generated method stub
-    return false;
+    // not applicable
+    return true;
   }
 
   @Override
   public IntervalI get(int i)
   {
-    // TODO Auto-generated method stub
+    // not supported (but could be)
     return null;
   }
+
+  @Override
+  public boolean canCheckForDuplicates()
+  {
+    return false;
+  }
+
+  @Override
+  public boolean add(T interval, boolean checkForDuplicate)
+  {
+    return add(interval);
+  }
 }