JAL-2808 spike updated with latest
[jalview.git] / src / jalview / datamodel / features / FeatureAttributes.java
1 package jalview.datamodel.features;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.TreeSet;
10
11 /**
12  * A singleton class to hold the set of attributes known for each feature type
13  */
14 public class FeatureAttributes
15 {
16   private static FeatureAttributes instance = new FeatureAttributes();
17
18   private Map<String, Set<String>> attributes;
19
20   /**
21    * Answers the singleton instance of this class
22    * 
23    * @return
24    */
25   public static FeatureAttributes getInstance()
26   {
27     return instance;
28   }
29
30   private FeatureAttributes()
31   {
32     attributes = new HashMap<>();
33   }
34
35   /**
36    * Answers the attributes known for the given feature type, in alphabetical
37    * order (not case sensitive), or an empty set if no attributes are known
38    * 
39    * @param featureType
40    * @return
41    */
42   public List<String> getAttributes(String featureType)
43   {
44     if (!attributes.containsKey(featureType))
45     {
46       return Collections.<String> emptyList();
47     }
48
49     return new ArrayList<>(attributes.get(featureType));
50   }
51
52   /**
53    * Answers true if at least one attribute is known for the given feature type,
54    * else false
55    * 
56    * @param featureType
57    * @return
58    */
59   public boolean hasAttributes(String featureType)
60   {
61
62     if (attributes.containsKey(featureType))
63     {
64       if (!attributes.get(featureType).isEmpty())
65       {
66         return true;
67       }
68     }
69     return false;
70   }
71
72   /**
73    * Records the given attribute name for the given feature type
74    * 
75    * @param featureType
76    * @param attName
77    */
78   public void addAttribute(String featureType, String attName)
79   {
80     if (featureType == null || attName == null)
81     {
82       return;
83     }
84
85     if (!attributes.containsKey(featureType))
86     {
87       attributes.put(featureType, new TreeSet<String>(
88               String.CASE_INSENSITIVE_ORDER));
89     }
90
91     attributes.get(featureType).add(attName);
92   }
93 }