KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > type > Value


1 package org.sapia.regis.type;
2
3 /**
4  * Wraps an object value corresponding to a <code>BuiltinType</code> (the Value/BuiltinType
5  * relation is analoguous to the Object/Primitive type relation).
6  *
7  * @author yduchesne
8  *
9  */

10 public class Value {
11   
12   private BuiltinType type;
13   private Object JavaDoc value;
14   
15   private Value(BuiltinType type, Object JavaDoc value){
16     this.value = value;
17     this.type = type;
18   }
19   
20   /**
21    * @return the <code>BuiltinType</code> of this instance.
22    */

23   public BuiltinType getType(){
24     return type;
25   }
26
27   /**
28    * Sets this instance's value, if the given object corresponds to this
29    * instance's <code>BuiltinType</code>.
30    *
31    * @param o an <code>Object</code>
32    */

33   public void set(Object JavaDoc o){
34     if(o == null){
35       this.value = o;
36     }
37     else if(type.isAssignable(o)){
38       this.value = o;
39     }
40     else{
41       throw new IllegalArgumentException JavaDoc("Invalid value: " + o + " for type: " + type.getName());
42     }
43   }
44   
45   /**
46    * Sets this instance's value, if the given string can be parsed to a
47    * value that corresponds to this instance's <code>BuiltinType</code>.
48    *
49    * @param o a <code>String</code>.
50    * @return the <code>Object</code> value that was parsed from the given string
51    * and internally set to this instance.
52    */

53   public Object JavaDoc set(String JavaDoc o){
54     if(o == null){
55       this.value = o;
56     }
57     else if(type.isAssignable(o)){
58       this.value = o;
59     }
60     else{
61       this.value = this.type.parse(o);
62     }
63     return this.value;
64   }
65
66   /**
67    * Sets the internal value of this instance to <code>null</code>.
68    */

69   public void nullify(){
70     this.value = null;
71   }
72   
73   /**
74    * @return the internal value of this instance.
75    */

76   public Object JavaDoc get(){
77     return value;
78   }
79   
80   /**
81    * @param type the <code>Builtin</code> type to which the returned value
82    * will correspond.
83    * @return a <code>Value</code>.
84    */

85   public static Value newInstance(BuiltinType type){
86     return new Value(type, null);
87   }
88   
89   /**
90    * @param type the <code>Builtin</code> type to which the returned value
91    * will correspond.
92    * @param value a string value that corresponds to the passed in built-in type.
93    * @return a <code>Value</code>.
94    */

95   public static Value newInstance(BuiltinType type, String JavaDoc value){
96     if(value == null){
97       return new Value(type, null);
98     }
99     else{
100       return new Value(type, type.parse(value));
101     }
102   }
103   
104   /**
105    * @param type the <code>Builtin</code> type to which the returned value
106    * will correspond.
107    * @param value an <code>Object</code> value that corresponds to the passed in built-in type.
108    * @return a <code>Value</code>.
109    */

110   public static Value newInstance(BuiltinType type, Object JavaDoc value){
111     if(value == null){
112       return new Value(type, null);
113     }
114     else if(type.isAssignable(value)){
115       return new Value(type, value);
116     }
117     else{
118       throw new IllegalArgumentException JavaDoc("Invalid value: " + value + " for type: " + type.getName());
119     }
120   }
121   
122 }
123
Popular Tags