KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > impl > PropertyImpl


1 package org.sapia.regis.impl;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
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 JavaDoc{
13   
14   static final long serialVersionUID = 1L;
15   
16   private static final String JavaDoc ON = "on";
17   private static final String JavaDoc TRUE = "true";
18   private static final String JavaDoc YES = "yes";
19   
20   protected String JavaDoc _name, _value;
21   
22   public PropertyImpl(String JavaDoc name, String JavaDoc value){
23     _name = name;
24     _value = value;
25   }
26   
27   public String JavaDoc 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 JavaDoc asString() {
59     checkNull();
60     return _value;
61   }
62   
63   public List JavaDoc asList() {
64    return asList(BuiltinTypes.STRING_TYPE);
65   }
66   
67   public List JavaDoc asList(BuiltinType type) {
68     checkNull();
69     List JavaDoc toReturn = new LinkedList JavaDoc();
70     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(_value);
71     while(tokenizer.hasMoreTokens()){
72       toReturn.add(type.parse(tokenizer.nextToken()));
73     }
74     return toReturn;
75   }
76
77   public String JavaDoc 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 JavaDoc("Could not convert; value is null for property: " + _name);
88     }
89   }
90   
91   public String JavaDoc toString(){
92     return new StringBuffer JavaDoc("[")
93       .append("key=").append(_name).append(",")
94       .append("value=").append(_value).append("]")
95       .toString();
96   }
97
98 }
99
Popular Tags