JAL-2808 remember attribute keys by feature type
[jalview.git] / src / jalview / datamodel / features / FeatureAttributes.java
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);
+  }
+}