applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / utils / Properties.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.objects.utils;\r
23 \r
24 import java.util.Enumeration;\r
25 import java.util.Vector;\r
26 \r
27 import uk.ac.vamsas.objects.core.Property;\r
28 \r
29 public class Properties {\r
30   public static Property newProperty(String name, String type, String content) {\r
31     Property vProperty = new Property();\r
32     vProperty.setName(name);\r
33     if (type != null) {\r
34       vProperty.setType(type);\r
35     } else {\r
36       vProperty.setType(STRINGTYPE);\r
37     }\r
38     vProperty.setContent(content);\r
39     return vProperty;\r
40   }\r
41 \r
42   final public static String STRINGTYPE = "string";\r
43 \r
44   final public static String FLOATTYPE = "float";\r
45 \r
46   final public static String INTEGERTYPE = "integer";\r
47 \r
48   final public static String BOOLEANTYPE = "boolean";\r
49 \r
50   /**\r
51    * add newprop to properties, or update the value of an existing property with\r
52    * the same exact name and type. Note - this routine will stop after\r
53    * encounting the first occurance of a Property with the same name and type,\r
54    * no others will be affected.\r
55    * \r
56    * @param properties\r
57    * @param newprop\r
58    * @return true if property was added or its value updated. false if no change\r
59    *         was made.\r
60    */\r
61   public static boolean addOrReplace(Vector properties, Property newprop) {\r
62     if (properties.size() > 0) {\r
63       Enumeration en = properties.elements();\r
64       while (en.hasMoreElements()) {\r
65         Object el = en.nextElement();\r
66         if (el instanceof Property) {\r
67           Property prop = (Property) el;\r
68           if (prop.getName().equals(newprop.getName())\r
69               && prop.getType().equals(newprop.getType())) {\r
70             if (prop.getContent().equals(newprop.getContent())) {\r
71               return false;\r
72             } else {\r
73               prop.setContent(newprop.getContent());\r
74               return true;\r
75             }\r
76           }\r
77         } else {\r
78           throw new Error(\r
79               "Implementation Error: properties must be a Vector of uk.ac.vamsas.objects.core.Property objects only.");\r
80         }\r
81       }\r
82     }\r
83     properties.addElement(newprop);\r
84     return true;\r
85   }\r
86 \r
87   /**\r
88    * validate property p against the known type strings and try to parse the\r
89    * content string accordingly\r
90    * \r
91    * @param p\r
92    * @return true if the content parses as the given type (if it is known) TODO:\r
93    *         decide if an isValidType method is also necessary.\r
94    */\r
95   public static boolean isValid(Property p) {\r
96     if (p.getType().equalsIgnoreCase(STRINGTYPE)) {\r
97       return true;\r
98     } else if (p.getType().equalsIgnoreCase(BOOLEANTYPE)) {\r
99       try {\r
100         Boolean bool = new Boolean(p.getContent());\r
101         return true;\r
102       } catch (Exception e) {\r
103         return false;\r
104       }\r
105     } else if (p.getType().equalsIgnoreCase(FLOATTYPE)) {\r
106       try {\r
107         Float fv = new Float(p.getContent());\r
108         return true;\r
109       } catch (Exception e) {\r
110         return false;\r
111       }\r
112     } else if (p.getType().equalsIgnoreCase(INTEGERTYPE)) {\r
113 \r
114       try {\r
115         Integer fv = new Integer(p.getContent());\r
116         return true;\r
117       } catch (Exception e) {\r
118         return false;\r
119       }\r
120     }\r
121     return false;\r
122   }\r
123 \r
124   /**\r
125    * String content test\r
126    * \r
127    * @param p\r
128    * @return true if the property is a string\r
129    */\r
130   public static boolean isString(Property p) {\r
131     return isType(p, STRINGTYPE);\r
132   }\r
133 \r
134   /**\r
135    * Float content test\r
136    * \r
137    * @param p\r
138    * @return true if the property is a string\r
139    */\r
140   public static boolean isFloat(Property p) {\r
141     return isType(p, FLOATTYPE);\r
142   }\r
143 \r
144   /**\r
145    * Integer content test\r
146    * \r
147    * @param p\r
148    * @return true if the property is a string\r
149    */\r
150   public static boolean isInteger(Property p) {\r
151     return isType(p, INTEGERTYPE);\r
152   }\r
153 \r
154   /**\r
155    * Boolean content test\r
156    * \r
157    * @param p\r
158    * @return true if the property is a string\r
159    */\r
160   public static boolean isBoolean(Property p) {\r
161     return isType(p, BOOLEANTYPE);\r
162   }\r
163 \r
164   /**\r
165    * \r
166    * @param p\r
167    *          the property to test for type\r
168    * @param typeString\r
169    *          one of the string constants in this class\r
170    * @return true if p is of type 'typeString'\r
171    */\r
172   public static boolean isType(Property p, String typeString) {\r
173     return (p == null) ? false : (p.getType().toLowerCase().equals(typeString));\r
174   }\r
175 \r
176 }\r