JAL-3383 tidy up NCList and alternatives
[jalview.git] / src / jalview / datamodel / features / FeatureStore.java
index 5c0336a..fff7127 100644 (file)
@@ -21,7 +21,6 @@
 package jalview.datamodel.features;
 
 import jalview.datamodel.SequenceFeature;
-import jalview.util.Platform;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -36,12 +35,30 @@ import intervalstore.impl.BinarySearcher.Compare;
 
 public class FeatureStore
 {
+  public enum IntervalStoreType
+  {
+    /**
+     * original NCList-based IntervalStore
+     */
+    INTERVAL_STORE_NCLIST,
+
+    /**
+     * linked-list IntervalStore
+     */
+    INTERVAL_STORE_LINKED_LIST,
+
+    /**
+     * NCList as array buffer IntervalStore
+     */
+    INTERVAL_STORE_NCARRAY
+  }
+
   /*
-   * track last start for quick insertion of ordered features
+   * track largest start for quick insertion of ordered features
    */
-  protected int lastStart = -1;
+  protected int maxStart = -1;
 
-  protected int lastContactStart = -1;
+  protected int maxContactStart = -1;
 
   /*
    * Non-positional features have no (zero) start/end position.
@@ -91,38 +108,6 @@ public class FeatureStore
 
   float nonPositionalMaxScore;
 
-  public final static int INTERVAL_STORE_DEFAULT = -1;
-
-  /**
-   * original NCList-based IntervalStore
-   */
-  public final static int INTERVAL_STORE_NCLIST_OBJECT = 0;
-
-  /**
-   * linked-list IntervalStore
-   */
-  public final static int INTERVAL_STORE_LINKED_LIST = 1;
-
-  /**
-   * NCList as array buffer IntervalStore
-   */
-  public final static int INTERVAL_STORE_NCARRAY = 3;
-
-  static final int intervalStoreJavaOption = INTERVAL_STORE_NCLIST_OBJECT;
-
-  private final static boolean useJSOption = false;
-
-  private final static boolean isJSLinkedTest = true;
-
-  static final int intervalStoreJSOption = (!useJSOption
-          ? intervalStoreJavaOption
-          : isJSLinkedTest
-          ? INTERVAL_STORE_LINKED_LIST
-          : INTERVAL_STORE_NCARRAY);
-
-  // TODO: compare performance in real situations using
-  // INTERVAL_STORE_LINKED_LIST;
-
   /**
    * Answers the 'length' of the feature, counting 0 for non-positional features
    * and 1 for contact features
@@ -152,7 +137,7 @@ public class FeatureStore
    * @param feature
    * @return
    */
-  public boolean listContains(List<SequenceFeature> list,
+  public static boolean listContains(List<SequenceFeature> list,
           SequenceFeature feature)
   {
     if (list == null || feature == null)
@@ -223,23 +208,20 @@ public class FeatureStore
   }
 
   /**
-   * standard constructor
+   * Constructor that defaults to using NCList IntervalStore
    */
   public FeatureStore()
   {
-    this(INTERVAL_STORE_DEFAULT);
+    this(IntervalStoreType.INTERVAL_STORE_NCLIST);
   }
 
   /**
-   * constructor for testing only
+   * Constructor that allows an alternative IntervalStore implementation to be
+   * chosen
    */
-  public FeatureStore(int intervalStoreType)
+  public FeatureStore(IntervalStoreType intervalStoreType)
   {
-    features =
-            // Platform.isJS()
-            // ? new intervalstore.nonc.IntervalStore<>(true)
-            // : new intervalstore.impl.IntervalStore<>();
-            getIntervalStore(intervalStoreType);
+    features = getIntervalStore(intervalStoreType);
     positionalFeatureGroups = new HashSet<>();
     nonPositionalFeatureGroups = new HashSet<>();
     positionalMinScore = Float.NaN;
@@ -247,24 +229,21 @@ public class FeatureStore
     nonPositionalMinScore = Float.NaN;
     nonPositionalMaxScore = Float.NaN;
 
-    // we only construct nonPositionalFeatures, contactFeatures if we need to
+    // only construct nonPositionalFeatures or contactFeatures if needed
   }
 
-  private IntervalStoreI<SequenceFeature> getIntervalStore(int type)
+  private IntervalStoreI<SequenceFeature> getIntervalStore(
+          IntervalStoreType type)
   {
-    switch (type != INTERVAL_STORE_DEFAULT ? type : //
-            Platform.isJS() //
-                    ? intervalStoreJSOption
-                    : intervalStoreJavaOption)
+    switch (type)
     {
     default:
-    case INTERVAL_STORE_NCLIST_OBJECT:
+    case INTERVAL_STORE_NCLIST:
       return new intervalstore.impl.IntervalStore<>();
     case INTERVAL_STORE_NCARRAY:
-      // TODO: Couldn't figure out how to get rid of this warning
-      return new intervalstore.nonc.IntervalStoreImpl<>();
+      return new intervalstore.nonc.IntervalStore<>();
     case INTERVAL_STORE_LINKED_LIST:
-      return new intervalstore.nonc.IntervalStore0Impl<>();
+      return new intervalstore.nonc.IntervalStore0<>();
     }
   }
 
@@ -319,9 +298,9 @@ public class FeatureStore
         return false;
       }
       positionalFeatureGroups.add(feature.getFeatureGroup());
-      if (feature.begin > lastContactStart)
+      if (feature.begin > maxContactStart)
       {
-        lastContactStart = feature.begin;
+        maxContactStart = feature.begin;
       }
       addContactFeature(feature);
     }
@@ -341,9 +320,9 @@ public class FeatureStore
         return false;
       }
       positionalFeatureGroups.add(feature.getFeatureGroup());
-      if (feature.begin > lastStart)
+      if (feature.begin > maxStart)
       {
-        lastStart = feature.begin;
+        maxStart = feature.begin;
       }
     }
 
@@ -437,12 +416,11 @@ public class FeatureStore
     }
 
     return containsPositionalFeature(feature);
-
   }
 
   private boolean containsPositionalFeature(SequenceFeature feature)
   {
-    return features == null || feature.begin > lastStart ? false
+    return features == null || feature.begin > maxStart ? false
             : features.contains(feature);
   }
 
@@ -455,7 +433,7 @@ public class FeatureStore
    */
   private boolean containsContactFeature(SequenceFeature feature)
   {
-    return contactFeatureStarts != null && feature.begin <= lastContactStart
+    return contactFeatureStarts != null && feature.begin <= maxContactStart
             && listContains(contactFeatureStarts, feature);
   }