JAL-2480 tidy generic initialisations, SequenceFeatures.varargToTypes
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 21 Jun 2017 15:26:56 +0000 (16:26 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 21 Jun 2017 15:26:56 +0000 (16:26 +0100)
src/jalview/datamodel/features/FeatureStore.java
src/jalview/datamodel/features/NCList.java
src/jalview/datamodel/features/NCNode.java
src/jalview/datamodel/features/SequenceFeatures.java
test/jalview/datamodel/SequenceTest.java
test/jalview/datamodel/features/SequenceFeaturesTest.java

index 3e80966..84e7736 100644 (file)
@@ -281,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);
@@ -455,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);
 
@@ -633,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);
 
     /*
@@ -667,7 +667,7 @@ public class FeatureStore
     {
       return Collections.emptyList();
     }
-    return new ArrayList<SequenceFeature>(contactFeatureStarts);
+    return new ArrayList<>(contactFeatureStarts);
   }
 
   /**
@@ -682,7 +682,7 @@ public class FeatureStore
     {
       return Collections.emptyList();
     }
-    return new ArrayList<SequenceFeature>(nonPositionalFeatures);
+    return new ArrayList<>(nonPositionalFeatures);
   }
 
   /**
@@ -972,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
index a6a23e7..b8160d3 100644 (file)
@@ -77,7 +77,7 @@ public class NCList<T extends ContiguousI>
   public NCList(T entry)
   {
     this();
-    subranges.add(new NCNode<T>(entry));
+    subranges.add(new NCNode<>(entry));
     size = 1;
   }
 
@@ -95,7 +95,7 @@ public class NCList<T extends ContiguousI>
    */
   protected List<Range> buildSubranges(List<T> ranges)
   {
-    List<Range> sublists = new ArrayList<Range>();
+    List<Range> sublists = new ArrayList<>();
     
     if (ranges.isEmpty())
     {
@@ -174,7 +174,7 @@ public class NCList<T extends ContiguousI>
       /*
        * all subranges precede this one - add it on the end
        */
-      subranges.add(new NCNode<T>(entry));
+      subranges.add(new NCNode<>(entry));
       return true;
     }
 
@@ -196,7 +196,7 @@ public class NCList<T extends ContiguousI>
         /*
          * new entry lies between subranges j-1 j
          */
-        subranges.add(j, new NCNode<T>(entry));
+        subranges.add(j, new NCNode<>(entry));
         return true;
       }
 
@@ -244,7 +244,7 @@ public class NCList<T extends ContiguousI>
              * entry spans two subranges but doesn't enclose any
              * so just add it 
              */
-            subranges.add(j, new NCNode<T>(entry));
+            subranges.add(j, new NCNode<>(entry));
             return true;
           }
         }
@@ -265,7 +265,7 @@ public class NCList<T extends ContiguousI>
     }
     else
     {
-      subranges.add(new NCNode<T>(entry));
+      subranges.add(new NCNode<>(entry));
     }
 
     return true;
@@ -323,9 +323,9 @@ public class NCList<T extends ContiguousI>
   protected synchronized void addEnclosingRange(T entry, final int i,
           final int j)
   {
-    NCList<T> newNCList = new NCList<T>();
+    NCList<T> newNCList = new NCList<>();
     newNCList.addNodes(subranges.subList(i, j + 1));
-    NCNode<T> newNode = new NCNode<T>(entry, newNCList);
+    NCNode<T> newNode = new NCNode<>(entry, newNCList);
     for (int k = j; k >= i; k--)
     {
       subranges.remove(k);
@@ -354,7 +354,7 @@ public class NCList<T extends ContiguousI>
    */
   public List<T> findOverlaps(long from, long to)
   {
-    List<T> result = new ArrayList<T>();
+    List<T> result = new ArrayList<>();
 
     findOverlaps(from, to, result);
     
@@ -554,7 +554,7 @@ public class NCList<T extends ContiguousI>
    */
   public List<T> getEntries()
   {
-    List<T> result = new ArrayList<T>();
+    List<T> result = new ArrayList<>();
     getEntries(result);
     return result;
   }
index c1be242..007f3b1 100644 (file)
@@ -42,7 +42,7 @@ class NCNode<V extends ContiguousI> implements ContiguousI
    */
   NCNode(V range)
   {
-    List<V> ranges = new ArrayList<V>();
+    List<V> ranges = new ArrayList<>();
     ranges.add(range);
     build(ranges);
   }
index 8f6d496..52da8c7 100644 (file)
@@ -112,15 +112,11 @@ public class SequenceFeatures implements SequenceFeaturesI
   public List<SequenceFeature> findFeatures(int from, int to,
           String... type)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
 
-    for (String featureType : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      FeatureStore features = featureStore.get(featureType);
-      if (features != null)
-      {
-        result.addAll(features.findOverlappingFeatures(from, to));
-      }
+      result.addAll(featureSet.findOverlappingFeatures(from, to));
     }
 
     return result;
@@ -132,7 +128,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   @Override
   public List<SequenceFeature> getAllFeatures(String... type)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
 
     result.addAll(getPositionalFeatures(type));
 
@@ -149,7 +145,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     if (ontologyTerm == null || ontologyTerm.length == 0)
     {
-      return new ArrayList<SequenceFeature>();
+      return new ArrayList<>();
     }
 
     Set<String> featureTypes = getFeatureTypes(ontologyTerm);
@@ -165,13 +161,9 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     int result = 0;
 
-    for (String featureType : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      FeatureStore featureSet = featureStore.get(featureType);
-      if (featureSet != null)
-      {
-        result += featureSet.getFeatureCount(positional);
-      }
+      result += featureSet.getFeatureCount(positional);
     }
     return result;
   }
@@ -184,16 +176,11 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     int result = 0;
 
-    for (String featureType : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      FeatureStore featureSet = featureStore.get(featureType);
-      if (featureSet != null)
-      {
-        result += featureSet.getTotalFeatureLength();
-      }
+      result += featureSet.getTotalFeatureLength();
     }
     return result;
-
   }
 
   /**
@@ -202,45 +189,41 @@ public class SequenceFeatures implements SequenceFeaturesI
   @Override
   public List<SequenceFeature> getPositionalFeatures(String... type)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
 
-    for (String featureType : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      FeatureStore featureSet = featureStore.get(featureType);
-      if (featureSet != null)
-      {
-        result.addAll(featureSet.getPositionalFeatures());
-      }
+      result.addAll(featureSet.getPositionalFeatures());
     }
     return result;
   }
 
   /**
    * A convenience method that converts a vararg for feature types to an
-   * Iterable, replacing the value with the stored feature types if it is null
-   * or empty
+   * Iterable over matched feature sets in key order
    * 
    * @param type
    * @return
    */
-  protected Iterable<String> varargToTypes(String... type)
+  protected Iterable<FeatureStore> varargToTypes(String... type)
   {
     if (type == null || type.length == 0)
     {
       /*
-       * no vararg parameter supplied
+       * no vararg parameter supplied - return all
        */
-      return featureStore.keySet();
+      return featureStore.values();
     }
 
-    /*
-     * else make a copy of the list, and remove any null value just in case,
-     * as it would cause errors looking up the features Map
-     * sort in alphabetical order for consistent output behaviour
-     */
-    List<String> types = new ArrayList<String>(Arrays.asList(type));
-    types.remove(null);
-    Collections.sort(types);
+    List<FeatureStore> types = new ArrayList<>();
+    List<String> args = Arrays.asList(type);
+    for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
+    {
+      if (args.contains(featureType.getKey()))
+      {
+        types.add(featureType.getValue());
+      }
+    }
     return types;
   }
 
@@ -250,15 +233,11 @@ public class SequenceFeatures implements SequenceFeaturesI
   @Override
   public List<SequenceFeature> getContactFeatures(String... type)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
 
-    for (String featureType : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      FeatureStore featureSet = featureStore.get(featureType);
-      if (featureSet != null)
-      {
-        result.addAll(featureSet.getContactFeatures());
-      }
+      result.addAll(featureSet.getContactFeatures());
     }
     return result;
   }
@@ -269,15 +248,11 @@ public class SequenceFeatures implements SequenceFeaturesI
   @Override
   public List<SequenceFeature> getNonPositionalFeatures(String... type)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> result = new ArrayList<>();
 
-    for (String featureType : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      FeatureStore featureSet = featureStore.get(featureType);
-      if (featureSet != null)
-      {
-        result.addAll(featureSet.getNonPositionalFeatures());
-      }
+      result.addAll(featureSet.getNonPositionalFeatures());
     }
     return result;
   }
@@ -321,17 +296,11 @@ public class SequenceFeatures implements SequenceFeaturesI
   public Set<String> getFeatureGroups(boolean positionalFeatures,
           String... type)
   {
-    Set<String> groups = new HashSet<String>();
+    Set<String> groups = new HashSet<>();
 
-    Iterable<String> types = varargToTypes(type);
-
-    for (String featureType : types)
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      FeatureStore featureSet = featureStore.get(featureType);
-      if (featureSet != null)
-      {
-        groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
-      }
+      groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
     }
 
     return groups;
@@ -344,7 +313,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
           String... groups)
   {
-    Set<String> result = new HashSet<String>();
+    Set<String> result = new HashSet<>();
 
     for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
     {
@@ -372,7 +341,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   @Override
   public Set<String> getFeatureTypes(String... soTerm)
   {
-    Set<String> types = new HashSet<String>();
+    Set<String> types = new HashSet<>();
     for (Entry<String, FeatureStore> entry : featureStore.entrySet())
     {
       String type = entry.getKey();
@@ -459,20 +428,12 @@ public class SequenceFeatures implements SequenceFeaturesI
   public List<SequenceFeature> getFeaturesForGroup(boolean positional,
           String group, String... type)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-    Iterable<String> types = varargToTypes(type);
-
-    for (String featureType : types)
+    List<SequenceFeature> result = new ArrayList<>();
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      /*
-       * check whether the feature type is present, and also
-       * whether it has features for the specified group
-       */
-      FeatureStore features = featureStore.get(featureType);
-      if (features != null
-              && features.getFeatureGroups(positional).contains(group))
+      if (featureSet.getFeatureGroups(positional).contains(group))
       {
-        result.addAll(features.getFeaturesForGroup(positional, group));
+        result.addAll(featureSet.getFeaturesForGroup(positional, group));
       }
     }
     return result;
index 2496a5b..e40d1a5 100644 (file)
@@ -440,11 +440,14 @@ public class SequenceTest
     sq.sequenceChanged();
     assertEquals(12, sq.findPosition(8));
     cursor = (SequenceCursor) PA.getValue(sq, "cursor");
-    assertEquals("test:Pos12:Col9:startCol3:endCol10:tok1",
+    // sequenceChanged() invalidates cursor.lastResidueColumn
+    cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+    assertEquals("test:Pos12:Col9:startCol3:endCol0:tok1",
             cursor.toString());
     // findPosition with cursor accepts base 1 column values
     assertEquals(13, ((Sequence) sq).findPosition(10, cursor));
-    assertEquals(13, sq.findPosition(9));
+    assertEquals(13, sq.findPosition(9)); // F13
+    // lastResidueColumn has now been found and saved in cursor
     assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1",
             PA.getValue(sq, "cursor").toString());
   }
index b7b52a7..a144f03 100644 (file)
@@ -10,8 +10,11 @@ import jalview.datamodel.SequenceFeature;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
+import junit.extensions.PA;
+
 import org.testng.annotations.Test;
 
 public class SequenceFeaturesTest
@@ -886,12 +889,15 @@ public class SequenceFeaturesTest
      * no type specified - get all types stored
      * they are returned in keyset (alphabetical) order
      */
-    Iterable<String> types = sf.varargToTypes();
-    Iterator<String> iterator = types.iterator();
+    Map<String, FeatureStore> featureStores = (Map<String, FeatureStore>) PA
+            .getValue(sf, "featureStore");
+
+    Iterable<FeatureStore> types = sf.varargToTypes();
+    Iterator<FeatureStore> iterator = types.iterator();
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Cath");
+    assertSame(iterator.next(), featureStores.get("Cath"));
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Metal");
+    assertSame(iterator.next(), featureStores.get("Metal"));
     assertFalse(iterator.hasNext());
 
     /*
@@ -901,9 +907,9 @@ public class SequenceFeaturesTest
     types = sf.varargToTypes(new String[] {});
     iterator = types.iterator();
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Cath");
+    assertSame(iterator.next(), featureStores.get("Cath"));
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Metal");
+    assertSame(iterator.next(), featureStores.get("Metal"));
     assertFalse(iterator.hasNext());
 
     /*
@@ -919,9 +925,9 @@ public class SequenceFeaturesTest
     types = sf.varargToTypes((String[]) null);
     iterator = types.iterator();
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Cath");
+    assertSame(iterator.next(), featureStores.get("Cath"));
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Metal");
+    assertSame(iterator.next(), featureStores.get("Metal"));
     assertFalse(iterator.hasNext());
 
     /*
@@ -930,29 +936,27 @@ public class SequenceFeaturesTest
     types = sf.varargToTypes("Metal");
     iterator = types.iterator();
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Metal");
+    assertSame(iterator.next(), featureStores.get("Metal"));
     assertFalse(iterator.hasNext());
 
     /*
      * two types specified - get sorted alphabetically
      */
-    types = sf.varargToTypes("Metal", "Helix");
+    types = sf.varargToTypes("Metal", "Cath");
     iterator = types.iterator();
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Helix");
+    assertSame(iterator.next(), featureStores.get("Cath"));
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Metal");
+    assertSame(iterator.next(), featureStores.get("Metal"));
     assertFalse(iterator.hasNext());
 
     /*
-     * null type included - should get removed
+     * null type included - should be ignored
      */
     types = sf.varargToTypes("Metal", null, "Helix");
     iterator = types.iterator();
     assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Helix");
-    assertTrue(iterator.hasNext());
-    assertEquals(iterator.next(), "Metal");
+    assertSame(iterator.next(), featureStores.get("Metal"));
     assertFalse(iterator.hasNext());
   }