1 package uk.ac.vamsas.objects.utils;
\r
3 import java.util.Enumeration;
\r
4 import java.util.Vector;
\r
6 import uk.ac.vamsas.objects.core.Property;
\r
8 public class Properties {
\r
9 public static Property newProperty(String name, String type, String content)
\r
11 Property vProperty = new Property();
\r
12 vProperty.setName(name);
\r
15 vProperty.setType(type);
\r
19 vProperty.setType(STRINGTYPE);
\r
21 vProperty.setContent(content);
\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
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
33 * @return true if property was added or its value updated. false if no change was made.
\r
35 public static boolean addOrReplace(Vector properties, Property newprop)
\r
37 if (properties.size()>0)
\r
39 Enumeration en = properties.elements();
\r
40 while (en.hasMoreElements())
\r
42 Object el = en.nextElement();
\r
43 if (el instanceof Property)
\r
45 Property prop = (Property) el;
\r
46 if (prop.getName().equals(newprop.getName()) &&
\r
47 prop.getType().equals(newprop.getType()))
\r
49 if (prop.getContent().equals(newprop.getContent()))
\r
53 prop.setContent(newprop.getContent());
\r
58 throw new Error("Implementation Error: properties must be a Vector of uk.ac.vamsas.objects.core.Property objects only.");
\r
62 properties.addElement(newprop);
\r
66 * validate property p against the known type strings and try to parse the content string accordingly
\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
71 public static boolean isValid(Property p)
\r
73 if (p.getType().equalsIgnoreCase(STRINGTYPE))
\r
77 if (p.getType().equalsIgnoreCase(BOOLEANTYPE))
\r
80 Boolean bool = new Boolean(p.getContent());
\r
82 } catch (Exception e)
\r
87 if (p.getType().equalsIgnoreCase(FLOATTYPE))
\r
90 Float fv = new Float(p.getContent());
\r
92 } catch (Exception e)
\r
98 if( p.getType().equalsIgnoreCase(INTEGERTYPE))
\r
102 Integer fv = new Integer(p.getContent());
\r
104 } catch (Exception e)
\r
112 * String content test
\r
114 * @return true if the property is a string
\r
116 public static boolean isString(Property p)
\r
118 return isType(p, STRINGTYPE);
\r
121 * Float content test
\r
123 * @return true if the property is a string
\r
125 public static boolean isFloat(Property p)
\r
127 return isType(p, FLOATTYPE);
\r
130 * Integer content test
\r
132 * @return true if the property is a string
\r
134 public static boolean isInteger(Property p)
\r
136 return isType(p, INTEGERTYPE);
\r
139 * Boolean content test
\r
141 * @return true if the property is a string
\r
143 public static boolean isBoolean(Property p)
\r
145 return isType(p, BOOLEANTYPE);
\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
153 public static boolean isType(Property p, String typeString) {
\r
154 return (p==null) ? false : (p.getType().toLowerCase().equals(typeString));
\r