1 package org.sapia.regis.impl; 2 3 import java.io.Serializable ; 4 import java.util.LinkedList ; 5 import java.util.List ; 6 import java.util.StringTokenizer ; 7 8 import org.sapia.regis.Property; 9 import org.sapia.regis.type.BuiltinType; 10 import org.sapia.regis.type.BuiltinTypes; 11 12 public class PropertyImpl implements Property, Serializable { 13 14 static final long serialVersionUID = 1L; 15 16 private static final String ON = "on"; 17 private static final String TRUE = "true"; 18 private static final String YES = "yes"; 19 20 protected String _name, _value; 21 22 public PropertyImpl(String name, String value){ 23 _name = name; 24 _value = value; 25 } 26 27 public String getKey(){ 28 return _name; 29 } 30 31 public boolean asBoolean() { 32 checkNull(); 33 return _value.equalsIgnoreCase(ON) || 34 _value.equalsIgnoreCase(TRUE) || 35 _value.equalsIgnoreCase(YES); 36 } 37 38 public double asDouble() { 39 checkNull(); 40 return Double.parseDouble(_value); 41 } 42 43 public float asFloat() { 44 checkNull(); 45 return Float.parseFloat(_value); 46 } 47 48 public int asInt() { 49 checkNull(); 50 return Integer.parseInt(_value); 51 } 52 53 public long asLong() { 54 checkNull(); 55 return Long.parseLong(_value); 56 } 57 58 public String asString() { 59 checkNull(); 60 return _value; 61 } 62 63 public List asList() { 64 return asList(BuiltinTypes.STRING_TYPE); 65 } 66 67 public List asList(BuiltinType type) { 68 checkNull(); 69 List toReturn = new LinkedList (); 70 StringTokenizer tokenizer = new StringTokenizer (_value); 71 while(tokenizer.hasMoreTokens()){ 72 toReturn.add(type.parse(tokenizer.nextToken())); 73 } 74 return toReturn; 75 } 76 77 public String getValue() { 78 return _value; 79 } 80 81 public boolean isNull() { 82 return _value == null; 83 } 84 85 protected void checkNull(){ 86 if(_value == null){ 87 throw new IllegalStateException ("Could not convert; value is null for property: " + _name); 88 } 89 } 90 91 public String toString(){ 92 return new StringBuffer ("[") 93 .append("key=").append(_name).append(",") 94 .append("value=").append(_value).append("]") 95 .toString(); 96 } 97 98 } 99 | Popular Tags |