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
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\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
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
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
22 package uk.ac.vamsas.objects.utils;
\r
24 import java.util.Enumeration;
\r
25 import java.util.Vector;
\r
27 import uk.ac.vamsas.objects.core.Property;
\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
34 vProperty.setType(type);
\r
36 vProperty.setType(STRINGTYPE);
\r
38 vProperty.setContent(content);
\r
42 final public static String STRINGTYPE = "string";
\r
44 final public static String FLOATTYPE = "float";
\r
46 final public static String INTEGERTYPE = "integer";
\r
48 final public static String BOOLEANTYPE = "boolean";
\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
58 * @return true if property was added or its value updated. false if no change
\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
73 prop.setContent(newprop.getContent());
\r
79 "Implementation Error: properties must be a Vector of uk.ac.vamsas.objects.core.Property objects only.");
\r
83 properties.addElement(newprop);
\r
88 * validate property p against the known type strings and try to parse the
\r
89 * content string accordingly
\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
95 public static boolean isValid(Property p) {
\r
96 if (p.getType().equalsIgnoreCase(STRINGTYPE)) {
\r
98 } else if (p.getType().equalsIgnoreCase(BOOLEANTYPE)) {
\r
100 Boolean bool = new Boolean(p.getContent());
\r
102 } catch (Exception e) {
\r
105 } else if (p.getType().equalsIgnoreCase(FLOATTYPE)) {
\r
107 Float fv = new Float(p.getContent());
\r
109 } catch (Exception e) {
\r
112 } else if (p.getType().equalsIgnoreCase(INTEGERTYPE)) {
\r
115 Integer fv = new Integer(p.getContent());
\r
117 } catch (Exception e) {
\r
125 * String content test
\r
128 * @return true if the property is a string
\r
130 public static boolean isString(Property p) {
\r
131 return isType(p, STRINGTYPE);
\r
135 * Float content test
\r
138 * @return true if the property is a string
\r
140 public static boolean isFloat(Property p) {
\r
141 return isType(p, FLOATTYPE);
\r
145 * Integer content test
\r
148 * @return true if the property is a string
\r
150 public static boolean isInteger(Property p) {
\r
151 return isType(p, INTEGERTYPE);
\r
155 * Boolean content test
\r
158 * @return true if the property is a string
\r
160 public static boolean isBoolean(Property p) {
\r
161 return isType(p, BOOLEANTYPE);
\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
172 public static boolean isType(Property p, String typeString) {
\r
173 return (p == null) ? false : (p.getType().toLowerCase().equals(typeString));
\r