1 package jalview.datamodel.features;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import java.util.TreeMap;
11 * A singleton class to hold the set of attributes known for each feature type
13 public class FeatureAttributes
15 private static FeatureAttributes instance = new FeatureAttributes();
17 private Map<String, Map<String, AttributeData>> attributes;
19 private class AttributeData
22 * description(s) for this attribute, if known
23 * (different feature source might have differing descriptions)
25 List<String> description;
28 * minimum value (of any numeric values recorded)
33 * maximum value (of any numeric values recorded)
38 * Note one instance of this attribute, recording unique, non-null names,
39 * and the min/max of any numerical values
44 void addInstance(String desc, String value)
48 if (description == null)
50 description = new ArrayList<>();
52 if (!description.contains(desc))
54 description.add(desc);
60 float f = Float.valueOf(value);
61 min = Float.min(min, f);
62 max = Float.max(max, f);
63 } catch (NumberFormatException e)
65 // ok, wasn't a number
72 * Answers the description of the attribute, if recorded and unique, or null if either no, or more than description is recorded
75 public String getDescription()
77 if (description != null && description.size() == 1)
79 return description.get(0);
86 * Answers the singleton instance of this class
90 public static FeatureAttributes getInstance()
95 private FeatureAttributes()
97 attributes = new HashMap<>();
101 * Answers the attributes known for the given feature type, in alphabetical
102 * order (not case sensitive), or an empty set if no attributes are known
107 public List<String> getAttributes(String featureType)
109 if (!attributes.containsKey(featureType))
111 return Collections.<String> emptyList();
114 return new ArrayList<>(attributes.get(featureType).keySet());
118 * Answers true if at least one attribute is known for the given feature type,
124 public boolean hasAttributes(String featureType)
126 if (attributes.containsKey(featureType))
128 if (!attributes.get(featureType).isEmpty())
137 * Records the given attribute name and description for the given feature
138 * type, and updates the min-max for any numeric value
145 public void addAttribute(String featureType, String attName,
146 String description, String value)
148 if (featureType == null || attName == null)
153 Map<String, AttributeData> atts = attributes.get(featureType);
156 atts = new TreeMap<String, AttributeData>(
157 String.CASE_INSENSITIVE_ORDER);
158 attributes.put(featureType, atts);
160 AttributeData attData = atts.get(attName);
163 attData = new AttributeData();
164 atts.put(attName, attData);
166 attData.addInstance(description, value);
170 * Answers the description of the given attribute for the given feature type,
171 * if known and unique, else null
177 public String getDescription(String featureType, String attName)
180 Map<String, AttributeData> atts = attributes.get(featureType);
183 AttributeData attData = atts.get(attName);
186 desc = attData.getDescription();