added more types and type validation method
[vamsas.git] / src / uk / ac / vamsas / objects / utils / Properties.java
1 package uk.ac.vamsas.objects.utils;\r
2 \r
3 import java.util.Enumeration;\r
4 import java.util.Vector;\r
5 \r
6 import uk.ac.vamsas.objects.core.Property;\r
7 \r
8 public class Properties {\r
9   public static Property newProperty(String name, String type, String content)\r
10   {\r
11     Property vProperty = new Property();\r
12     vProperty.setName(name);\r
13     if (type != null)\r
14     {\r
15       vProperty.setType(type);\r
16     }\r
17     else\r
18     {\r
19       vProperty.setType(STRINGTYPE);\r
20     }\r
21     vProperty.setContent(content);\r
22     return vProperty;\r
23   }\r
24   final public static String STRINGTYPE="string";\r
25   final public static String FLOATTYPE="float";\r
26   final public static String INTEGERTYPE="integer";\r
27   final public static String BOOLEANTYPE="boolean";\r
28   /**\r
29    * add newprop to properties, or update the value of an existing property with the same exact name and type.\r
30    * 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
31    * @param properties\r
32    * @param newprop \r
33    * @return true if property was added or its value updated. false if no change was made.\r
34    */\r
35   public static boolean addOrReplace(Vector properties, Property newprop)\r
36   {\r
37     if (properties.size()>0)\r
38     {\r
39       Enumeration en = properties.elements();\r
40       while (en.hasMoreElements())\r
41       {\r
42         Object el = en.nextElement();\r
43         if (el instanceof Property)\r
44         {\r
45           Property prop = (Property) el;\r
46           if (prop.getName().equals(newprop.getName()) && \r
47                   prop.getType().equals(newprop.getType()))\r
48           {\r
49             if (prop.getContent().equals(newprop.getContent()))\r
50             {\r
51               return false;\r
52             }  else {\r
53               prop.setContent(newprop.getContent());\r
54               return true;\r
55             }\r
56           }\r
57         } else {\r
58           throw new Error("Implementation Error: properties must be a Vector of uk.ac.vamsas.objects.core.Property objects only.");\r
59         }\r
60       }\r
61     } \r
62     properties.addElement(newprop);\r
63     return true;\r
64   }\r
65   /**\r
66    * validate property p against the known type strings and try to parse the content string accordingly \r
67    * @param p\r
68    * @return true if the content parses as the given type (if it is known)\r
69    * TODO: decide if an isValidType method is also necessary.\r
70    */\r
71   public static boolean isValid(Property p)\r
72   {\r
73     if (p.getType().equalsIgnoreCase(STRINGTYPE))\r
74     {\r
75       return true;\r
76     } else\r
77       if (p.getType().equalsIgnoreCase(BOOLEANTYPE))\r
78       {\r
79         try {\r
80           Boolean bool = new Boolean(p.getContent());\r
81           return true;\r
82         } catch (Exception e)\r
83         {\r
84           return false;\r
85         }\r
86       } else\r
87       if (p.getType().equalsIgnoreCase(FLOATTYPE))\r
88       {\r
89         try {\r
90           Float fv = new Float(p.getContent());\r
91           return true;\r
92         } catch (Exception e)\r
93         {\r
94           return false;\r
95         }\r
96       }\r
97       else\r
98         if( p.getType().equalsIgnoreCase(INTEGERTYPE))\r
99         {\r
100 \r
101           try {\r
102             Integer fv = new Integer(p.getContent());\r
103             return true;\r
104           } catch (Exception e)\r
105           {\r
106             return false;\r
107           } \r
108         }\r
109     return false;\r
110   }\r
111   /**\r
112    * String content test \r
113    * @param p\r
114    * @return true if the property is a string \r
115    */\r
116   public static boolean isString(Property p)\r
117   {\r
118     return isType(p, STRINGTYPE);\r
119   }\r
120   /**\r
121    * Float content test \r
122    * @param p\r
123    * @return true if the property is a string \r
124    */\r
125   public static boolean isFloat(Property p)\r
126   {\r
127     return isType(p, FLOATTYPE);\r
128   }\r
129   /**\r
130    * Integer content test \r
131    * @param p\r
132    * @return true if the property is a string \r
133    */\r
134   public static boolean isInteger(Property p)\r
135   {\r
136     return isType(p, INTEGERTYPE);\r
137   }\r
138   /**\r
139    * Boolean content test \r
140    * @param p\r
141    * @return true if the property is a string \r
142    */\r
143   public static boolean isBoolean(Property p)\r
144   {\r
145     return isType(p, BOOLEANTYPE);\r
146   }\r
147   /**\r
148    * \r
149    * @param p the property to test for type\r
150    * @param typeString one of the string constants in this class\r
151    * @return true if p is of type 'typeString'\r
152    */\r
153   public static boolean isType(Property p, String typeString) {\r
154     return (p==null) ? false : (p.getType().toLowerCase().equals(typeString));\r
155   }\r
156   \r
157 }\r