KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.regis.type;
2
3 import java.io.File JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.sapia.regis.Node;
9
10 /**
11  * This class holds the various predefined built-in types.
12  *
13  * @see org.sapia.regis.type.BuiltinType
14  *
15  * @author yduchesne
16  *
17  */

18 public class BuiltinTypes {
19   
20   private static Map JavaDoc types = new HashMap JavaDoc();
21
22   /**
23    * Corresponds to the "string" type (<code>java.lang.String</code>).
24    */

25   public static final BuiltinType STRING_TYPE =
26     new BuiltinType(){
27        public String JavaDoc getName() {return "string";}
28        public Object JavaDoc parse(String JavaDoc value) {return value;}
29        public String JavaDoc toString(Object JavaDoc value) {return (String JavaDoc)value;}
30        public boolean isAssignable(Object JavaDoc value) {return value instanceof String JavaDoc;}
31        public boolean isAssignable(Class JavaDoc clazz) {return clazz.equals(String JavaDoc.class);}
32        public Object JavaDoc getProperty(String JavaDoc propName, Node node) {return node.getProperty(propName).asString();}
33     };
34     
35   /**
36    * Corresponds to the "file" type (<code>java.io.File</code>).
37    */

38   public static final BuiltinType FILE_TYPE =
39     new BuiltinType(){
40        public String JavaDoc getName() {return "file";}
41        public Object JavaDoc parse(String JavaDoc value) {return new File JavaDoc(value);}
42        public String JavaDoc toString(Object JavaDoc value) {return ((File JavaDoc)value).getAbsolutePath();}
43        public boolean isAssignable(Object JavaDoc value) {return value instanceof File JavaDoc;}
44        public boolean isAssignable(Class JavaDoc clazz) {return clazz.equals(File JavaDoc.class);}
45        public Object JavaDoc getProperty(String JavaDoc propName, Node node) {return new File JavaDoc(node.getProperty(propName).asString());}
46     };
47   
48     /**
49      * Corresponds to the "boolean" type (<code>java.lang.Boolean</code>).
50      */

51   public static final BuiltinType BOOLEAN_TYPE =
52     new BuiltinType(){
53        public String JavaDoc getName() {return "boolean";}
54        public Object JavaDoc parse(String JavaDoc value) {return new Boolean JavaDoc(value);}
55        public String JavaDoc toString(Object JavaDoc value) {return ((Boolean JavaDoc)value).toString();}
56        public boolean isAssignable(Object JavaDoc value) {return value instanceof Boolean JavaDoc;}
57        public boolean isAssignable(Class JavaDoc clazz) {return clazz.equals(Boolean JavaDoc.class) || clazz.equals(boolean.class);}
58        public Object JavaDoc getProperty(String JavaDoc propName, Node node) {return new Boolean JavaDoc(node.getProperty(propName).asBoolean());}
59     };
60     
61     /**
62      * Corresponds to the "int" type (<code>java.lang.Integer</code>).
63      */

64   public static final BuiltinType INT_TYPE =
65     new BuiltinType(){
66        public String JavaDoc getName() {return "int";}
67        public Object JavaDoc parse(String JavaDoc value) {return new Integer JavaDoc(value);}
68        public String JavaDoc toString(Object JavaDoc value) {return ((Integer JavaDoc)value).toString();}
69        public boolean isAssignable(Object JavaDoc value) {return value instanceof Integer JavaDoc;}
70        public boolean isAssignable(Class JavaDoc clazz) {return clazz.equals(Integer JavaDoc.class) || clazz.equals(int.class);}
71        public Object JavaDoc getProperty(String JavaDoc propName, Node node) {return new Integer JavaDoc(node.getProperty(propName).asInt());}
72     };
73     
74     /**
75      * Corresponds to the "long" type (<code>java.lang.Long</code>).
76      */

77   public static final BuiltinType LONG_TYPE =
78     new BuiltinType(){
79        public String JavaDoc getName() {return "long";}
80        public Object JavaDoc parse(String JavaDoc value) {return new Long JavaDoc(value);}
81        public String JavaDoc toString(Object JavaDoc value) {return ((Long JavaDoc)value).toString();}
82        public boolean isAssignable(Object JavaDoc value) {return value instanceof Long JavaDoc;}
83        public boolean isAssignable(Class JavaDoc clazz) {return clazz.equals(Long JavaDoc.class) || clazz.equals(long.class);}
84        public Object JavaDoc getProperty(String JavaDoc propName, Node node) {return new Long JavaDoc(node.getProperty(propName).asLong());}
85     };
86
87     /**
88      * Corresponds to the "float" type (<code>java.lang.Float</code>).
89      */

90   public static final BuiltinType FLOAT_TYPE =
91     new BuiltinType(){
92        public String JavaDoc getName() {return "float";}
93        public Object JavaDoc parse(String JavaDoc value) {return new Float JavaDoc(value);}
94        public String JavaDoc toString(Object JavaDoc value) {return ((Float JavaDoc)value).toString();}
95        public boolean isAssignable(Object JavaDoc value) {return value instanceof Float JavaDoc;}
96        public boolean isAssignable(Class JavaDoc clazz) {return clazz.equals(Float JavaDoc.class) || clazz.equals(float.class);}
97        public Object JavaDoc getProperty(String JavaDoc propName, Node node) {return new Float JavaDoc(node.getProperty(propName).asFloat());}
98     };
99     
100     /**
101      * Corresponds to the "double" type (<code>java.lang.Double</code>).
102      */

103   public static final BuiltinType DOUBLE_TYPE =
104     new BuiltinType(){
105        public String JavaDoc getName() {return "double";}
106        public Object JavaDoc parse(String JavaDoc value) {return new Double JavaDoc(value);}
107        public String JavaDoc toString(Object JavaDoc value) {return ((Double JavaDoc)value).toString();}
108        public boolean isAssignable(Object JavaDoc value) {return value instanceof Double JavaDoc;}
109        public boolean isAssignable(Class JavaDoc clazz) {return clazz.equals(Double JavaDoc.class) || clazz.equals(double.class);}
110        public Object JavaDoc getProperty(String JavaDoc propName, Node node) {return new Double JavaDoc(node.getProperty(propName).asDouble());}
111     };
112
113   static{
114     types.put(BOOLEAN_TYPE.getName(), BOOLEAN_TYPE);
115     types.put(INT_TYPE.getName(), INT_TYPE);
116     types.put(LONG_TYPE.getName(), LONG_TYPE);
117     types.put(FLOAT_TYPE.getName(), FLOAT_TYPE);
118     types.put(DOUBLE_TYPE.getName(), DOUBLE_TYPE);
119     types.put(STRING_TYPE.getName(), STRING_TYPE);
120   }
121   
122   /**
123    * @param name a type name
124    * @return the <code>BuiltinType</code> corresponding to the given name.
125    */

126   public static BuiltinType getTypeFor(String JavaDoc name){
127     BuiltinType type = (BuiltinType)types.get(name);
128     if(type == null){
129       throw new IllegalStateException JavaDoc("Invalid type name: " + name);
130     }
131     return type;
132   }
133   
134   /**
135    * @param value an <code>Object</code>
136    * @return the <code>BuiltinType</code> of the given value, or
137    * <code>null</code> if the value does not correspond to a built-in
138    * type.
139    */

140   public static BuiltinType getTypeFor(Object JavaDoc value){
141     Iterator JavaDoc itr = types.values().iterator();
142     while(itr.hasNext()){
143       BuiltinType type = (BuiltinType)itr.next();
144       if(type.isAssignable(value)){
145         return type;
146       }
147     }
148     return null;
149   }
150   
151   /**
152    * @param value a <code>Class</code>
153    * @return the <code>BuiltinType</code> corresponding to the given class, or
154    * <code>null</code> if the class does not correspond to a built-in
155    * type.
156    */

157   public static BuiltinType getTypeFor(Class JavaDoc clazz){
158     Iterator JavaDoc itr = types.values().iterator();
159     while(itr.hasNext()){
160       BuiltinType type = (BuiltinType)itr.next();
161       if(type.isAssignable(clazz)){
162         return type;
163       }
164     }
165     return null;
166   }
167 }
168
Popular Tags