JAL-3253 temporary branch SwingJS upgrade with testNG fixes Java 8
[jalview.git] / src / jalview / datamodel / features / FeatureAttributes.java
index e359b62..f51fe48 100644 (file)
@@ -1,5 +1,8 @@
 package jalview.datamodel.features;
 
+import jalview.bin.ApplicationSingletonProvider;
+import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
@@ -12,14 +15,23 @@ import java.util.TreeMap;
 /**
  * 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
@@ -87,8 +99,8 @@ public class FeatureAttributes
     Datatype type;
 
     /**
-     * Note one instance of this attribute, recording unique, non-null names,
-     * and the min/max of any numerical values
+     * Note one instance of this attribute, recording unique, non-null
+     * descriptions, and the min/max of any numerical values
      * 
      * @param desc
      * @param value
@@ -99,20 +111,35 @@ public class FeatureAttributes
 
       if (value != null)
       {
-        try
-        {
-          float f = Float.valueOf(value);
-          min = hasValue ? Float.min(min, f) : f;
-          max = hasValue ? Float.max(max, f) : f;
-          hasValue = true;
-          type = (type == null || type == Datatype.Number) ? Datatype.Number
-                  : Datatype.Mixed;
-        } catch (NumberFormatException e)
+        value = value.trim();
+
+        /*
+         * Parse numeric value unless we have previously
+         * seen text data for this attribute type
+         */
+        if (type == null || type == Datatype.Number)
         {
-          // not a number, ignore for min-max purposes
-          type = (type == null || type == Datatype.Character)
-                  ? Datatype.Character
-                  : Datatype.Mixed;
+          try
+          {
+            float f = Float.valueOf(value);
+            min = hasValue ? Math.min(min, f) : f;
+            max = hasValue ? Math.max(max, f) : f;
+            hasValue = true;
+            type = (type == null || type == Datatype.Number)
+                    ? Datatype.Number
+                    : Datatype.Mixed;
+          } catch (NumberFormatException e)
+          {
+            /*
+             * non-numeric data: treat attribute as Character (or Mixed)
+             */
+            type = (type == null || type == Datatype.Character)
+                    ? Datatype.Character
+                    : Datatype.Mixed;
+            min = 0f;
+            max = 0f;
+            hasValue = false;
+          }
         }
       }
     }
@@ -158,21 +185,6 @@ public class FeatureAttributes
   }
 
   /**
-   * 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
@@ -284,9 +296,8 @@ public class FeatureAttributes
 
   /**
    * Answers the [min, max] value range of the given attribute for the given
-   * feature type, if known, else null. Attributes which only have text values
-   * would normally return null, however text values which happen to be numeric
-   * could result in a 'min-max' range.
+   * feature type, if known, else null. Attributes with a mixture of text and
+   * numeric values are considered text (do not return a min-max range).
    * 
    * @param featureType
    * @param attName