JAL-2480 tidy generic initialisations, SequenceFeatures.varargToTypes
[jalview.git] / src / jalview / datamodel / features / FeatureStore.java
index c6a49db..84e7736 100644 (file)
@@ -1,5 +1,6 @@
 package jalview.datamodel.features;
 
+import jalview.datamodel.ContiguousI;
 import jalview.datamodel.SequenceFeature;
 
 import java.util.ArrayList;
@@ -280,7 +281,7 @@ public class FeatureStore
   {
     if (nestedFeatures == null)
     {
-      nestedFeatures = new NCList<SequenceFeature>(feature);
+      nestedFeatures = new NCList<>(feature);
       return true;
     }
     return nestedFeatures.add(feature, false);
@@ -454,7 +455,7 @@ public class FeatureStore
    */
   public List<SequenceFeature> findOverlappingFeatures(long start, long end)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
 
     findNonNestedFeatures(start, end, result);
 
@@ -632,7 +633,7 @@ public class FeatureStore
     /*
      * add non-nested features (may be all features for many cases)
      */
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
     result.addAll(nonNestedFeatures);
 
     /*
@@ -666,7 +667,7 @@ public class FeatureStore
     {
       return Collections.emptyList();
     }
-    return new ArrayList<SequenceFeature>(contactFeatureStarts);
+    return new ArrayList<>(contactFeatureStarts);
   }
 
   /**
@@ -681,7 +682,7 @@ public class FeatureStore
     {
       return Collections.emptyList();
     }
-    return new ArrayList<SequenceFeature>(nonPositionalFeatures);
+    return new ArrayList<>(nonPositionalFeatures);
   }
 
   /**
@@ -819,27 +820,6 @@ public class FeatureStore
   }
 
   /**
-   * Scans all positional features to check whether the given feature group is
-   * found, and returns true if found, else false
-   * 
-   * @param featureGroup
-   * @return
-   */
-  protected boolean findFeatureGroup(String featureGroup)
-  {
-    for (SequenceFeature sf : getPositionalFeatures())
-    {
-      String group = sf.getFeatureGroup();
-      if (group == featureGroup
-              || (group != null && group.equals(featureGroup)))
-      {
-        return true;
-      }
-    }
-    return false;
-  }
-
-  /**
    * Answers true if this store has no features, else false
    * 
    * @return
@@ -992,7 +972,7 @@ public class FeatureStore
   public List<SequenceFeature> getFeaturesForGroup(boolean positional,
           String group)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
 
     /*
      * if we know features don't include the target group, no need
@@ -1017,4 +997,40 @@ public class FeatureStore
     }
     return result;
   }
+
+  /**
+   * Adds the shift value to the start and end of all positional features.
+   * Returns true if at least one feature was updated, else false.
+   * 
+   * @param shift
+   * @return
+   */
+  public synchronized boolean shiftFeatures(int shift)
+  {
+    /*
+     * Because begin and end are final fields (to ensure the data store's
+     * integrity), we have to delete each feature and re-add it as amended.
+     * (Although a simple shift of all values would preserve data integrity!)
+     */
+    boolean modified = false;
+    for (SequenceFeature sf : getPositionalFeatures())
+    {
+      modified = true;
+      int newBegin = sf.getBegin() + shift;
+      int newEnd = sf.getEnd() + shift;
+
+      /*
+       * sanity check: don't shift left of the first residue
+       */
+      if (newEnd > 0)
+      {
+        newBegin = Math.max(1, newBegin);
+        SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
+                sf.getFeatureGroup(), sf.getScore());
+        addFeature(sf2);
+      }
+      delete(sf);
+    }
+    return modified;
+  }
 }