JAL-2492 use SequenceFeatures.getNonPositionalFeatures()
[jalview.git] / src / jalview / datamodel / features / SequenceFeatures.java
index c6af3ea..5fa9a3c 100644 (file)
@@ -4,12 +4,13 @@ import jalview.datamodel.SequenceFeature;
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.TreeMap;
 
 /**
  * A class that stores sequence features in a way that supports efficient
@@ -33,7 +34,12 @@ public class SequenceFeatures implements SequenceFeaturesI
    */
   public SequenceFeatures()
   {
-    featureStore = new HashMap<String, FeatureStore>();
+    /*
+     * use a TreeMap so that features are returned in alphabetical order of type
+     * wrap as a synchronized map for add and delete operations
+     */
+    featureStore = Collections
+            .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
   }
 
   /**
@@ -43,6 +49,11 @@ public class SequenceFeatures implements SequenceFeaturesI
   public boolean add(SequenceFeature sf)
   {
     String type = sf.getType();
+    if (type == null)
+    {
+      System.err.println("Feature type may not be null: " + sf.toString());
+      return false;
+    }
 
     if (featureStore.get(type) == null)
     {
@@ -155,8 +166,21 @@ public class SequenceFeatures implements SequenceFeaturesI
    */
   protected Iterable<String> varargToTypes(String... type)
   {
-    return type == null || type.length == 0 ? featureStore
-            .keySet() : Arrays.asList(type);
+    if (type == null || type.length == 0)
+    {
+      /*
+       * no vararg parameter supplied
+       */
+      return featureStore.keySet();
+    }
+
+    /*
+     * 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
+     */
+    List<String> types = new ArrayList<String>(Arrays.asList(type));
+    types.remove(null);
+    return types;
   }
 
   /**