JAL-3383 JAL-3253-applet
[jalview.git] / src / jalview / datamodel / features / FeatureStoreImpl.java
index 62832d7..d1d2670 100644 (file)
@@ -46,6 +46,43 @@ public class FeatureStoreImpl extends FeatureStore
   }
 
   /**
+   * Add a contact feature to the lists that hold them ordered by start (first
+   * contact) and by end (second contact) position, ensuring the lists remain
+   * ordered, and returns true. This method allows duplicate features to be
+   * added, so test before calling to avoid this.
+   * 
+   * @param feature
+   * @return
+   */
+  @Override
+  protected synchronized boolean addContactFeature(SequenceFeature feature)
+  {
+    if (contactFeatureStarts == null)
+    {
+      contactFeatureStarts = new ArrayList<>();
+      contactFeatureEnds = new ArrayList<>();
+    }
+
+    /*
+     * insert into list sorted by start (first contact position):
+     * binary search the sorted list to find the insertion point
+     */
+    int insertPosition = findFirstBeginStatic(contactFeatureStarts,
+            feature.getBegin());
+    contactFeatureStarts.add(insertPosition, feature);
+
+    /*
+     * insert into list sorted by end (second contact position):
+     * binary search the sorted list to find the insertion point
+     */
+    insertPosition = findFirstEndStatic(contactFeatureEnds,
+            feature.getEnd());
+    contactFeatureEnds.add(insertPosition, feature);
+
+    return true;
+  }
+
+  /**
    * Adds contact features to the result list where either the second or the
    * first contact position lies within the target range
    * 
@@ -81,8 +118,7 @@ public class FeatureStoreImpl extends FeatureStore
      * find the first contact feature (if any) 
      * whose end point is not before the target range
      */
-    int index = BinarySearcher.findFirst(contactFeatureEnds,
-            f -> f.getEnd() >= from);
+    int index = findFirstEndStatic(contactFeatureEnds, from);
 
     int n = contactFeatureEnds.size();
     while (index < n)
@@ -136,8 +172,7 @@ public class FeatureStoreImpl extends FeatureStore
   private void findContactStartOverlaps(long from, long to,
           List<SequenceFeature> result)
   {
-    int index = BinarySearcher.findFirst(contactFeatureStarts,
-            f -> f.getBegin() >= from);
+    int index = findFirstBegin(contactFeatureStarts, from);
 
     while (index < contactFeatureStarts.size())
     {
@@ -195,8 +230,30 @@ public class FeatureStoreImpl extends FeatureStore
   {
     result.addAll(((IntervalStoreI<SequenceFeature>) features)
             .findOverlaps(start, end));
-    // TODO Auto-generated method stub
+  }
+
+  @Override
+  protected int findFirstBegin(List<SequenceFeature> list, long pos)
+  {
+    return findFirstBeginStatic(list, pos);
+  }
 
+  private static int findFirstBeginStatic(List<SequenceFeature> list,
+          long pos)
+  {
+    return BinarySearcher.findFirst(list, f -> f.getBegin() >= pos);
+  }
+
+  @Override
+  protected int findFirstEnd(List<SequenceFeature> list, long pos)
+  {
+    return findFirstEndStatic(list, pos);
+  }
+
+  private static int findFirstEndStatic(List<SequenceFeature> list,
+          long pos)
+  {
+    return BinarySearcher.findFirst(list, f -> f.getEnd() >= pos);
   }
 
 }