JAL-2808 capture feature attributes datatype (Character/Number/Mixed) for more intell...
[jalview.git] / src / jalview / datamodel / features / FeatureAttributes.java
index 7221d62..e359b62 100644 (file)
@@ -14,6 +14,11 @@ import java.util.TreeMap;
  */
 public class FeatureAttributes
 {
+  public enum Datatype
+  {
+    Character, Number, Mixed
+  }
+
   private static FeatureAttributes instance = new FeatureAttributes();
 
   /*
@@ -79,6 +84,8 @@ public class FeatureAttributes
      */
     boolean hasValue = false;
 
+    Datatype type;
+
     /**
      * Note one instance of this attribute, recording unique, non-null names,
      * and the min/max of any numerical values
@@ -95,12 +102,17 @@ public class FeatureAttributes
         try
         {
           float f = Float.valueOf(value);
-          min = Float.min(min, f);
-          max = Float.max(max, f);
+          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)
         {
-          // ok, wasn't a number, ignore for min-max purposes
+          // not a number, ignore for min-max purposes
+          type = (type == null || type == Datatype.Character)
+                  ? Datatype.Character
+                  : Datatype.Mixed;
         }
       }
     }
@@ -118,6 +130,11 @@ public class FeatureAttributes
       return null;
     }
 
+    public Datatype getType()
+    {
+      return type;
+    }
+
     /**
      * Adds the given description to the list of known descriptions (without
      * duplication)
@@ -318,4 +335,26 @@ public class FeatureAttributes
     }
     attData.addDescription(description);
   }
+
+  /**
+   * Answers the datatype of the feature, which is one of Character, Number or
+   * Mixed (or null if not known), as discovered from values recorded.
+   * 
+   * @param featureType
+   * @param attName
+   * @return
+   */
+  public Datatype getDatatype(String featureType, String... attName)
+  {
+    Map<String[], AttributeData> atts = attributes.get(featureType);
+    if (atts != null)
+    {
+      AttributeData attData = atts.get(attName);
+      if (attData != null)
+      {
+        return attData.getType();
+      }
+    }
+    return null;
+  }
 }