JAL-2808 remember attribute keys by feature type
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Oct 2017 16:54:55 +0000 (16:54 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Oct 2017 16:54:55 +0000 (16:54 +0000)
src/jalview/datamodel/SequenceFeature.java
src/jalview/datamodel/features/FeatureAttributes.java [new file with mode: 0644]

index 8f82a1a..5029da5 100755 (executable)
@@ -21,6 +21,7 @@
 package jalview.datamodel;
 
 import jalview.datamodel.features.FeatureAttributeType;
+import jalview.datamodel.features.FeatureAttributes;
 import jalview.datamodel.features.FeatureLocationI;
 import jalview.datamodel.features.FeatureSourceI;
 import jalview.datamodel.features.FeatureSources;
@@ -424,6 +425,7 @@ public class SequenceFeature implements FeatureLocationI
       }
 
       otherDetails.put(key, value);
+      FeatureAttributes.getInstance().addAttribute(this.type, key);
     }
   }
 
diff --git a/src/jalview/datamodel/features/FeatureAttributes.java b/src/jalview/datamodel/features/FeatureAttributes.java
new file mode 100644 (file)
index 0000000..7990f6b
--- /dev/null
@@ -0,0 +1,71 @@
+package jalview.datamodel.features;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+/**
+ * A singleton class to hold the set of attributes known for each feature type
+ */
+public class FeatureAttributes
+{
+  private static FeatureAttributes instance = new FeatureAttributes();
+
+  private Map<String, Set<String>> attributes;
+
+  /**
+   * Answers the singleton instance of this class
+   * 
+   * @return
+   */
+  public static FeatureAttributes getInstance()
+  {
+    return instance;
+  }
+
+  private FeatureAttributes()
+  {
+    attributes = new HashMap<>();
+  }
+
+  /**
+   * Answers the attributes known for the given feature type, in alphabetical
+   * order (not case sensitive), or an empty set if no attributes are known
+   * 
+   * @param featureType
+   * @return
+   */
+  public Iterable<String> getAttributes(String featureType)
+  {
+    if (!attributes.containsKey(featureType))
+    {
+      return Collections.emptySet();
+    }
+
+    return attributes.get(featureType);
+  }
+
+  /**
+   * Records the given attribute name for the given feature type
+   * 
+   * @param featureType
+   * @param attName
+   */
+  public void addAttribute(String featureType, String attName)
+  {
+    if (featureType == null || attName == null)
+    {
+      return;
+    }
+
+    if (!attributes.containsKey(featureType))
+    {
+      attributes.put(featureType, new TreeSet<String>(
+              String.CASE_INSENSITIVE_ORDER));
+    }
+
+    attributes.get(featureType).add(attName);
+  }
+}