package uk.ac.vamsas.objects.utils;\r
\r
+import java.util.Enumeration;\r
+import java.util.Vector;\r
+\r
import uk.ac.vamsas.objects.core.Property;\r
\r
public class Properties {\r
vProperty.setContent(content);\r
return vProperty;\r
}\r
- public static String STRINGTYPE="string";\r
- public static String FLOATTYPE="float";\r
- public static String INTEGERTYPE="integer";\r
- \r
- public boolean isString(Property p)\r
+ final public static String STRINGTYPE="string";\r
+ final public static String FLOATTYPE="float";\r
+ final public static String INTEGERTYPE="integer";\r
+ final public static String BOOLEANTYPE="boolean";\r
+ /**\r
+ * add newprop to properties, or update the value of an existing property with the same exact name and type.\r
+ * Note - this routine will stop after encounting the first occurance of a Property with the same name and type, no others will be affected. \r
+ * @param properties\r
+ * @param newprop \r
+ * @return true if property was added or its value updated. false if no change was made.\r
+ */\r
+ public static boolean addOrReplace(Vector properties, Property newprop)\r
{\r
- return isType(p, STRINGTYPE);\r
+ if (properties.size()>0)\r
+ {\r
+ Enumeration en = properties.elements();\r
+ while (en.hasMoreElements())\r
+ {\r
+ Object el = en.nextElement();\r
+ if (el instanceof Property)\r
+ {\r
+ Property prop = (Property) el;\r
+ if (prop.getName().equals(newprop.getName()) && \r
+ prop.getType().equals(newprop.getType()))\r
+ {\r
+ if (prop.getContent().equals(newprop.getContent()))\r
+ {\r
+ return false;\r
+ } else {\r
+ prop.setContent(newprop.getContent());\r
+ return true;\r
+ }\r
+ }\r
+ } else {\r
+ throw new Error("Implementation Error: properties must be a Vector of uk.ac.vamsas.objects.core.Property objects only.");\r
+ }\r
+ }\r
+ } \r
+ properties.addElement(newprop);\r
+ return true;\r
}\r
+ /**\r
+ * validate property p against the known type strings and try to parse the content string accordingly \r
+ * @param p\r
+ * @return true if the content parses as the given type (if it is known)\r
+ * TODO: decide if an isValidType method is also necessary.\r
+ */\r
+ public static boolean isValid(Property p)\r
+ {\r
+ if (p.getType().equalsIgnoreCase(STRINGTYPE))\r
+ {\r
+ return true;\r
+ } else\r
+ if (p.getType().equalsIgnoreCase(BOOLEANTYPE))\r
+ {\r
+ try {\r
+ Boolean bool = new Boolean(p.getContent());\r
+ return true;\r
+ } catch (Exception e)\r
+ {\r
+ return false;\r
+ }\r
+ } else\r
+ if (p.getType().equalsIgnoreCase(FLOATTYPE))\r
+ {\r
+ try {\r
+ Float fv = new Float(p.getContent());\r
+ return true;\r
+ } catch (Exception e)\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+ else\r
+ if( p.getType().equalsIgnoreCase(INTEGERTYPE))\r
+ {\r
\r
+ try {\r
+ Integer fv = new Integer(p.getContent());\r
+ return true;\r
+ } catch (Exception e)\r
+ {\r
+ return false;\r
+ } \r
+ }\r
+ return false;\r
+ }\r
+ /**\r
+ * String content test \r
+ * @param p\r
+ * @return true if the property is a string \r
+ */\r
+ public static boolean isString(Property p)\r
+ {\r
+ return isType(p, STRINGTYPE);\r
+ }\r
+ /**\r
+ * Float content test \r
+ * @param p\r
+ * @return true if the property is a string \r
+ */\r
+ public static boolean isFloat(Property p)\r
+ {\r
+ return isType(p, FLOATTYPE);\r
+ }\r
+ /**\r
+ * Integer content test \r
+ * @param p\r
+ * @return true if the property is a string \r
+ */\r
+ public static boolean isInteger(Property p)\r
+ {\r
+ return isType(p, INTEGERTYPE);\r
+ }\r
+ /**\r
+ * Boolean content test \r
+ * @param p\r
+ * @return true if the property is a string \r
+ */\r
+ public static boolean isBoolean(Property p)\r
+ {\r
+ return isType(p, BOOLEANTYPE);\r
+ }\r
/**\r
* \r
* @param p the property to test for type\r
* @param typeString one of the string constants in this class\r
* @return true if p is of type 'typeString'\r
*/\r
- public boolean isType(Property p, String typeString) {\r
+ public static boolean isType(Property p, String typeString) {\r
return (p==null) ? false : (p.getType().toLowerCase().equals(typeString));\r
}\r
\r