KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > generic > data > GenericValue


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.generic.data.GenericValue
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.generic.data;
20
21 import de.gulden.framework.amoda.model.metadata.NamedValue;
22 import java.awt.*;
23 import java.io.*;
24 import java.lang.*;
25 import java.util.*;
26
27 /**
28  * Class GenericValue.
29  *
30  * @author Jens Gulden
31  * @version snapshot-beautyj-1.1
32  */

33 public class GenericValue implements NamedValue {
34
35     // ------------------------------------------------------------------------
36
// --- fields ---
37
// ------------------------------------------------------------------------
38

39     public static String JavaDoc[][] BOOLEAN_LITERALS = {{"true","yes","on"},{"false","no","off"}};
40
41     protected Class JavaDoc type;
42
43     protected Object JavaDoc object;
44
45     protected String JavaDoc name;
46
47
48     // ------------------------------------------------------------------------
49
// --- constructors ---
50
// ------------------------------------------------------------------------
51

52     public GenericValue() {
53         // your code here
54
}
55
56     public GenericValue(Object JavaDoc value) {
57         this();
58         setType(value.getClass());
59         set(value);
60     }
61
62
63     // ------------------------------------------------------------------------
64
// --- methods ---
65
// ------------------------------------------------------------------------
66

67     public String JavaDoc getString() {
68         return toString(get());
69     }
70
71     public int getInt() {
72         if (object instanceof Number JavaDoc) {
73             return ((Number JavaDoc)object).intValue();
74         } else if (object instanceof String JavaDoc) {
75             try {
76                 return Integer.valueOf((String JavaDoc)object).intValue();
77             } catch (NumberFormatException JavaDoc nfe) {
78                 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert "+((String JavaDoc)object)+" to int");
79             }
80         } else if (object==null) {
81             return 0;
82         } else {
83             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to int");
84         }
85     }
86
87     public float getFloat() {
88         if (object instanceof Number JavaDoc) {
89             return ((Number JavaDoc)object).floatValue();
90         } else if (object instanceof String JavaDoc) {
91             try {
92                 return Float.valueOf((String JavaDoc)object).floatValue();
93             } catch (NumberFormatException JavaDoc nfe) {
94                 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert "+((String JavaDoc)object)+" to float");
95             }
96         } else if (object==null) {
97             return 0.0f;
98         } else {
99             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to float");
100         }
101     }
102
103     public double getDouble() {
104         if (object instanceof Number JavaDoc) {
105             return ((Number JavaDoc)object).doubleValue();
106         } else if (object instanceof String JavaDoc) {
107             try {
108                 return Double.valueOf((String JavaDoc)object).doubleValue();
109             } catch (NumberFormatException JavaDoc nfe) {
110                 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert "+((String JavaDoc)object)+" to double");
111             }
112         } else if (object==null) {
113             return 0.0;
114         } else {
115             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to double");
116         }
117     }
118
119     public boolean getBoolean() {
120         if (object instanceof Boolean JavaDoc) {
121             return ((Boolean JavaDoc)object).booleanValue();
122         } else if (object instanceof String JavaDoc) {
123             String JavaDoc boolString=(String JavaDoc)object;
124             return isBooleanLiteral(true,boolString);
125         } else if (object instanceof Number JavaDoc) {
126             return (((Number JavaDoc)object).doubleValue()!=0.0);
127         } else if (object==null) {
128             return false;
129         } else {
130             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to boolean");
131         }
132     }
133
134     public boolean isTypeAvailable(Class JavaDoc type) {
135         // ...**** quick and drity ****************************************************
136
if ((object!=null)&&(type.isAssignableFrom(object.getClass()))) {
137             return true;
138         } else if (type.isAssignableFrom(this.type)) {
139             return true;
140         } else if (String JavaDoc.class.isAssignableFrom(type)) {
141             return true; // everything can be converted to string somehow
142
} else if (Integer JavaDoc.class.isAssignableFrom(type)||Double JavaDoc.class.isAssignableFrom(type)) {
143             return Number JavaDoc.class.isAssignableFrom(this.type);
144         } else if (Boolean JavaDoc.class.isAssignableFrom(type)) {
145             return Boolean JavaDoc.class.isAssignableFrom(this.type)
146                 || Number JavaDoc.class.isAssignableFrom(this.type)
147                 || String JavaDoc.class.isAssignableFrom(this.type);
148         } else {
149             return false;
150         }
151     }
152
153     public void parseString(String JavaDoc s) {
154         if (type!=null) { // type already known
155
try {
156                 if (String JavaDoc.class.isAssignableFrom(type)) {
157                     object=s;
158                 } else if (Boolean JavaDoc.class.isAssignableFrom(type)) {
159                     object=isBooleanLiteral(true,s.trim()) ? Boolean.TRUE : Boolean.FALSE; // ### Fix: make backward-compatible to Jdk1.2. Jens, 2002-10-27
160
} else if (Integer JavaDoc.class.isAssignableFrom(type)) {
161                     object=Integer.valueOf(s.trim());
162                 } else if (Float JavaDoc.class.isAssignableFrom(type)) {
163                     object=Float.valueOf(s.trim());
164                 } else if (Double JavaDoc.class.isAssignableFrom(type)) {
165                     object=Double.valueOf(s.trim());
166                 } else if (Class JavaDoc.class.isAssignableFrom(type)) {
167                     try {
168                         object=Class.forName(s.trim());
169                     } catch (ClassNotFoundException JavaDoc cnfe) {
170                         throw new de.gulden.framework.amoda.model.data.IllegalTypeError("string '"+s.trim()+"' is not a valid class name, class not found");
171                     }
172                 } else if (File.class.isAssignableFrom(type)) {
173                     object=(new File(s.trim()));//.getCanonicalFile(); // test validity - disabled, allow empty string as path
174
} else if (Color.class.isAssignableFrom(type)) {
175                     object=de.gulden.util.Toolbox.parseColor(s.trim());
176                     if (object==null) {
177                         throw new de.gulden.framework.amoda.model.data.IllegalTypeError("string '"+s+"' is not a valid color");
178                     }
179                 } else {
180                     throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot handle type '"+type.getName()+"' for parsing '"+s+"'");
181                 }
182             } catch (NumberFormatException JavaDoc nfe) {
183                 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot parse string '"+s+"' as value for type "+type.getName());
184             }
185         } else { // find out type from string
186
// boolean?
187
if (s.equalsIgnoreCase("yes")||s.equalsIgnoreCase("true")||s.equalsIgnoreCase("on")) {
188                 set(Boolean.TRUE);
189                 return;
190             } else if (s.equalsIgnoreCase("no")||s.equalsIgnoreCase("false")||s.equalsIgnoreCase("off")) {
191                 set(Boolean.FALSE);
192                 return;
193             }
194             // number?
195
try {
196                 double d=Double.parseDouble(s);
197                 double dRound=Math.round(d);
198                 if (d==dRound) { // integer
199
set(new Integer JavaDoc((int)dRound));
200                 } else { // real double
201
set(new Double JavaDoc(d));
202                 }
203                 return;
204             } catch (NumberFormatException JavaDoc nfe) {
205                 /*// file?
206                 java.io.File file=new java.io.File(s);
207                 if (file.exists()) {
208                     set(file);
209                 } else {
210                     // no special type found: use as plain string
211                     set(s);
212                 }*/

213                 set(s); // treat as String type
214
}
215         }
216     }
217
218     public String JavaDoc toString(Object JavaDoc o) {
219         if (object instanceof Color) {
220             return de.gulden.util.Toolbox.toString((Color)object);
221         } else if (object instanceof File) {
222             return ((File)object).getAbsolutePath();
223         } else if (o!=null) {
224             return o.toString();
225         } else {
226             return null;
227         }
228     }
229
230     public File getFile() {
231         if (object instanceof File) {
232             return ((File)object);
233         } else if (object instanceof String JavaDoc) {
234             File f=new File((String JavaDoc)object);
235             try {
236                 f=f.getCanonicalFile();
237                 return f;
238             } catch (java.io.IOException JavaDoc ioe) {
239                 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to File");
240             }
241         } else if (object==null) {
242             return null;
243         } else {
244             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to File");
245         }
246     }
247
248     public Color getColor() {
249         if (object instanceof java.awt.Color JavaDoc) {
250             return ((Color)object);
251         } else if (object instanceof String JavaDoc) {
252             Color c=de.gulden.util.Toolbox.parseColor((String JavaDoc)object);
253             if (c!=null) {
254                 return c;
255             } else {
256                 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert '"+((String JavaDoc)object)+" to Color");
257             }
258         } else if (object==null) {
259             return null;
260         } else {
261             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to Color");
262         }
263     }
264
265     public Class JavaDoc getType() {
266         return type;
267     }
268
269     public void setType(Class JavaDoc _type) {
270         type = _type;
271     }
272
273     public Object JavaDoc get() {
274         return object;
275     }
276
277     public Object JavaDoc set(Object JavaDoc _object) {
278         Object JavaDoc old = object;
279            if ((type==null)&&(_object!=null)) {
280                type=_object.getClass();
281            }
282            object = _object;
283            return old;
284     }
285
286     public String JavaDoc getName() {
287         return name;
288     }
289
290     public void setName(String JavaDoc _name) {
291         name = _name;
292     }
293
294     public Class JavaDoc getClassType() {
295         if (object instanceof Class JavaDoc) {
296             return ((Class JavaDoc)object);
297         } else if (object instanceof String JavaDoc) {
298             try {
299                 Class JavaDoc c = Class.forName((String JavaDoc)object);
300                 return c;
301             } catch (ClassNotFoundException JavaDoc cnfe) {
302                 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert '"+((String JavaDoc)object)+" to Class");
303             }
304         } else if (object==null) {
305             return null;
306         } else {
307             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to Class");
308         }
309     }
310
311     public Object JavaDoc getClassInstance() {
312         Class JavaDoc cl = getClassType();
313         try {
314             return cl.newInstance();
315         } catch (InstantiationException JavaDoc ie) {
316             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot create instance of "+cl.getName()+": "+ie.getMessage());
317         } catch (IllegalAccessException JavaDoc iae) {
318             throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot create instance of "+cl.getName()+": "+iae.getMessage());
319         }
320     }
321
322
323     // ------------------------------------------------------------------------
324
// --- static method ---
325
// ------------------------------------------------------------------------
326

327     public static boolean isBooleanLiteral(boolean booleanValue, String JavaDoc s) {
328         int b=booleanValue?0:1;
329         for (int i=0;i<BOOLEAN_LITERALS[b].length;i++) {
330             if (s.equalsIgnoreCase(BOOLEAN_LITERALS[b][i])) {
331                 return true;
332             }
333         }
334         return false;
335     }
336
337 } // end GenericValue
338
Popular Tags