JAL-2446 added getFeatureGroups
[jalview.git] / src / jalview / datamodel / features / SequenceFeatures.java
index c947407..a61eff2 100644 (file)
@@ -5,8 +5,10 @@ import jalview.datamodel.SequenceFeature;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * A class that stores sequence features in a way that supports efficient
@@ -34,11 +36,14 @@ public class SequenceFeatures
   }
 
   /**
-   * Add one sequence feature to the store
+   * Adds one sequence feature to the store, and returns true, unless the
+   * feature is already contained in the store, in which case this method
+   * returns false. Containment is determined by SequenceFeature.equals()
+   * comparison.
    * 
    * @param sf
    */
-  public void add(SequenceFeature sf)
+  public boolean add(SequenceFeature sf)
   {
     String type = sf.getType();
 
@@ -46,7 +51,7 @@ public class SequenceFeatures
     {
       featureStore.put(type, new FeatureStore());
     }
-    featureStore.get(type).addFeature(sf);
+    return featureStore.get(type).addFeature(sf);
   }
 
   /**
@@ -167,4 +172,57 @@ public class SequenceFeatures
     }
     return result;
   }
+
+  /**
+   * Deletes the given feature from the store, returning true if it was found
+   * (and deleted), else false. This method makes no assumption that the feature
+   * is in the 'expected' place in the store, in case it has been modified since
+   * it was added.
+   * 
+   * @param sf
+   */
+  public boolean delete(SequenceFeature sf)
+  {
+    for (FeatureStore featureSet : featureStore.values())
+    {
+      if (featureSet.delete(sf))
+      {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  /**
+   * Answers true if this store contains at least one feature, else false
+   * 
+   * @return
+   */
+  public boolean hasFeatures()
+  {
+    for (FeatureStore featureSet : featureStore.values())
+    {
+      if (!featureSet.isEmpty())
+      {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  /**
+   * Returns a set of the distinct feature groups present in the collection. The
+   * set may include null.
+   * 
+   * @return
+   */
+  public Set<String> getFeatureGroups()
+  {
+    Set<String> groups = new HashSet<String>();
+    for (FeatureStore featureSet : featureStore.values())
+    {
+      groups.addAll(featureSet.getFeatureGroups());
+    }
+    return groups;
+  }
 }