7990f6b2f1c7ef637707282c176682cea8ddcb90
[jalview.git] / src / jalview / datamodel / features / FeatureAttributes.java
1 package jalview.datamodel.features;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Set;
7 import java.util.TreeSet;
8
9 /**
10  * A singleton class to hold the set of attributes known for each feature type
11  */
12 public class FeatureAttributes
13 {
14   private static FeatureAttributes instance = new FeatureAttributes();
15
16   private Map<String, Set<String>> attributes;
17
18   /**
19    * Answers the singleton instance of this class
20    * 
21    * @return
22    */
23   public static FeatureAttributes getInstance()
24   {
25     return instance;
26   }
27
28   private FeatureAttributes()
29   {
30     attributes = new HashMap<>();
31   }
32
33   /**
34    * Answers the attributes known for the given feature type, in alphabetical
35    * order (not case sensitive), or an empty set if no attributes are known
36    * 
37    * @param featureType
38    * @return
39    */
40   public Iterable<String> getAttributes(String featureType)
41   {
42     if (!attributes.containsKey(featureType))
43     {
44       return Collections.emptySet();
45     }
46
47     return attributes.get(featureType);
48   }
49
50   /**
51    * Records the given attribute name for the given feature type
52    * 
53    * @param featureType
54    * @param attName
55    */
56   public void addAttribute(String featureType, String attName)
57   {
58     if (featureType == null || attName == null)
59     {
60       return;
61     }
62
63     if (!attributes.containsKey(featureType))
64     {
65       attributes.put(featureType, new TreeSet<String>(
66               String.CASE_INSENSITIVE_ORDER));
67     }
68
69     attributes.get(featureType).add(attName);
70   }
71 }