From 1da81c3ea56d8d977caab67a87da6f7c311795c6 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 12 Nov 2008 15:51:01 +0000 Subject: [PATCH] added more types and type validation method git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@489 be28352e-c001-0410-b1a7-c7978e42abec --- src/uk/ac/vamsas/objects/utils/Properties.java | 131 ++++++++++++++++++++++-- 1 file changed, 124 insertions(+), 7 deletions(-) diff --git a/src/uk/ac/vamsas/objects/utils/Properties.java b/src/uk/ac/vamsas/objects/utils/Properties.java index c7d9e30..4d5c874 100644 --- a/src/uk/ac/vamsas/objects/utils/Properties.java +++ b/src/uk/ac/vamsas/objects/utils/Properties.java @@ -1,5 +1,8 @@ package uk.ac.vamsas.objects.utils; +import java.util.Enumeration; +import java.util.Vector; + import uk.ac.vamsas.objects.core.Property; public class Properties { @@ -18,22 +21,136 @@ public class Properties { vProperty.setContent(content); return vProperty; } - public static String STRINGTYPE="string"; - public static String FLOATTYPE="float"; - public static String INTEGERTYPE="integer"; - - public boolean isString(Property p) + final public static String STRINGTYPE="string"; + final public static String FLOATTYPE="float"; + final public static String INTEGERTYPE="integer"; + final public static String BOOLEANTYPE="boolean"; + /** + * add newprop to properties, or update the value of an existing property with the same exact name and type. + * Note - this routine will stop after encounting the first occurance of a Property with the same name and type, no others will be affected. + * @param properties + * @param newprop + * @return true if property was added or its value updated. false if no change was made. + */ + public static boolean addOrReplace(Vector properties, Property newprop) { - return isType(p, STRINGTYPE); + if (properties.size()>0) + { + Enumeration en = properties.elements(); + while (en.hasMoreElements()) + { + Object el = en.nextElement(); + if (el instanceof Property) + { + Property prop = (Property) el; + if (prop.getName().equals(newprop.getName()) && + prop.getType().equals(newprop.getType())) + { + if (prop.getContent().equals(newprop.getContent())) + { + return false; + } else { + prop.setContent(newprop.getContent()); + return true; + } + } + } else { + throw new Error("Implementation Error: properties must be a Vector of uk.ac.vamsas.objects.core.Property objects only."); + } + } + } + properties.addElement(newprop); + return true; } + /** + * validate property p against the known type strings and try to parse the content string accordingly + * @param p + * @return true if the content parses as the given type (if it is known) + * TODO: decide if an isValidType method is also necessary. + */ + public static boolean isValid(Property p) + { + if (p.getType().equalsIgnoreCase(STRINGTYPE)) + { + return true; + } else + if (p.getType().equalsIgnoreCase(BOOLEANTYPE)) + { + try { + Boolean bool = new Boolean(p.getContent()); + return true; + } catch (Exception e) + { + return false; + } + } else + if (p.getType().equalsIgnoreCase(FLOATTYPE)) + { + try { + Float fv = new Float(p.getContent()); + return true; + } catch (Exception e) + { + return false; + } + } + else + if( p.getType().equalsIgnoreCase(INTEGERTYPE)) + { + try { + Integer fv = new Integer(p.getContent()); + return true; + } catch (Exception e) + { + return false; + } + } + return false; + } + /** + * String content test + * @param p + * @return true if the property is a string + */ + public static boolean isString(Property p) + { + return isType(p, STRINGTYPE); + } + /** + * Float content test + * @param p + * @return true if the property is a string + */ + public static boolean isFloat(Property p) + { + return isType(p, FLOATTYPE); + } + /** + * Integer content test + * @param p + * @return true if the property is a string + */ + public static boolean isInteger(Property p) + { + return isType(p, INTEGERTYPE); + } + /** + * Boolean content test + * @param p + * @return true if the property is a string + */ + public static boolean isBoolean(Property p) + { + return isType(p, BOOLEANTYPE); + } /** * * @param p the property to test for type * @param typeString one of the string constants in this class * @return true if p is of type 'typeString' */ - public boolean isType(Property p, String typeString) { + public static boolean isType(Property p, String typeString) { return (p==null) ? false : (p.getType().toLowerCase().equals(typeString)); } -- 1.7.10.2