+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.datamodel.features;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.TreeMap;
+import jalview.bin.ApplicationSingletonProvider;
+import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
+
/**
* A singleton class to hold the set of attributes known for each feature type
*/
-public class FeatureAttributes
+public class FeatureAttributes implements ApplicationSingletonI
{
public enum Datatype
{
Character, Number, Mixed
}
- private static FeatureAttributes instance = new FeatureAttributes();
+ public static FeatureAttributes getInstance()
+ {
+ return (FeatureAttributes) ApplicationSingletonProvider
+ .getInstance(FeatureAttributes.class);
+ }
+
+ private FeatureAttributes()
+ {
+ attributes = new HashMap<>();
+ }
/*
* map, by feature type, of a map, by attribute name, of
if (value != null)
{
value = value.trim();
+ if (value.isEmpty())
+ {
+ return;
+ }
/*
* Parse numeric value unless we have previously
* seen text data for this attribute type
*/
- if (type == null || type == Datatype.Number)
+ if ((type == null && couldBeNumber(value))
+ || type == Datatype.Number)
{
try
{
float f = Float.valueOf(value);
- min = hasValue ? Float.min(min, f) : f;
- max = hasValue ? Float.max(max, f) : f;
+ min = hasValue ? Math.min(min, f) : f;
+ max = hasValue ? Math.max(max, f) : f;
hasValue = true;
type = (type == null || type == Datatype.Number)
? Datatype.Number
hasValue = false;
}
}
+ else
+ {
+ /*
+ * if not a number, and not seen before...
+ */
+ type = Datatype.Character;
+ min = 0f;
+ max = 0f;
+ hasValue = false;
+ }
}
}
/**
- * Answers the description of the attribute, if recorded and unique, or null if either no, or more than description is recorded
+ * Answers the description of the attribute, if recorded and unique, or null
+ * if either no, or more than description is recorded
+ *
* @return
*/
public String getDescription()
}
/**
- * Answers the singleton instance of this class
- *
- * @return
- */
- public static FeatureAttributes getInstance()
- {
- return instance;
- }
-
- private FeatureAttributes()
- {
- attributes = new HashMap<>();
- }
-
- /**
* Answers the attribute names known for the given feature type, in
* alphabetical order (not case sensitive), or an empty set if no attributes
* are known. An attribute name is typically 'simple' e.g. "AC", but may be
}
/**
+ * A partial check that the string is numeric - only checks the first
+ * character. Returns true if the first character is a digit, or if it is '.',
+ * '+' or '-' and not the only character. Otherwise returns false (including
+ * for an empty string). Note this is not a complete check as it returns true
+ * for (e.g.) "1A".
+ *
+ * @param f
+ * @return
+ */
+ public static boolean couldBeNumber(String f)
+ {
+ int len = f.length();
+ if (len == 0)
+ {
+ return false;
+ }
+ char ch = f.charAt(0);
+ switch (ch)
+ {
+ case '.':
+ case '+':
+ case '-':
+ return len > 1;
+ }
+ return (ch <= '9' && ch >= '0');
+ }
+
+ /**
* Answers true if at least one attribute is known for the given feature type,
* else false
*
{
return;
}
-
+
Map<String[], AttributeData> atts = attributes.get(featureType);
if (atts == null)
{
}
return null;
}
+
+ /**
+ * Resets all attribute metadata
+ */
+ public void clear()
+ {
+ attributes.clear();
+ }
+
+ /**
+ * Resets attribute metadata for one feature type
+ *
+ * @param featureType
+ */
+ public void clear(String featureType)
+ {
+ Map<String[], AttributeData> map = attributes.get(featureType);
+ if (map != null)
+ {
+ map.clear();
+ }
+
+ }
}