| 1 |
/* |
| 2 |
* Value.java |
| 3 |
* |
| 4 |
* Created on January 4, 2001, 11:37 AM |
| 5 |
*/ |
| 6 |
|
| 7 |
package org.freehep.graphicsbase.util; |
| 8 |
|
| 9 |
import java.lang.reflect.Constructor; |
| 10 |
import java.util.Date; |
| 11 |
import java.text.SimpleDateFormat; |
| 12 |
import java.text.ParseException; |
| 13 |
|
| 14 |
/** |
| 15 |
* A class that can represent any Java object or primitive. Unlike the |
| 16 |
* built-in primitive proxies (Double, Integer etc) it is mutable. It is |
| 17 |
* used to allow values to be used without needing overloaded methods for |
| 18 |
* each primitive type, and without the overhead of object creation/deletion. |
| 19 |
* |
| 20 |
* When a value is returned by an Object method it should be assumed to be valid |
| 21 |
* only until the next method call to that Object. The use of Value should be |
| 22 |
* avoided in multi-threaded environments. |
| 23 |
* |
| 24 |
* @author tonyj |
| 25 |
* @version $Id: Value.java 9133 2006-10-13 16:25:43Z turri $ |
| 26 |
*/ |
| 27 |
public class Value { |
| 28 |
|
| 29 |
private int intValue; |
| 30 |
private short shortValue; |
| 31 |
private long longValue; |
| 32 |
private float floatValue; |
| 33 |
private double doubleValue; |
| 34 |
private boolean boolValue; |
| 35 |
private byte byteValue; |
| 36 |
private char charValue; |
| 37 |
private Object obj; |
| 38 |
private Class<?> type; |
| 39 |
|
| 40 |
public final static Class<?> TYPE_INTEGER = Integer.TYPE; |
| 41 |
public final static Class<?> TYPE_SHORT = Short.TYPE; |
| 42 |
public final static Class<?> TYPE_LONG = Long.TYPE; |
| 43 |
public final static Class<?> TYPE_FLOAT = Float.TYPE; |
| 44 |
public final static Class<?> TYPE_DOUBLE = Double.TYPE; |
| 45 |
public final static Class<?> TYPE_BOOLEAN = Boolean.TYPE; |
| 46 |
public final static Class<?> TYPE_BYTE = Byte.TYPE; |
| 47 |
public final static Class<?> TYPE_CHAR = Character.TYPE; |
| 48 |
public final static Class<?> TYPE_STRING = String.class; |
| 49 |
public final static Class<?> TYPE_DATE = Date.class; |
| 50 |
|
| 51 |
public Value() {} |
| 52 |
|
| 53 |
public Value( Value v ) { |
| 54 |
setValue(v); |
| 55 |
} |
| 56 |
|
| 57 |
public Value setValue(Value v) { |
| 58 |
this.type = v.getType(); |
| 59 |
this.intValue = v.intValue; |
| 60 |
this.shortValue = v.shortValue; |
| 61 |
this.longValue = v.longValue; |
| 62 |
this.floatValue = v.floatValue; |
| 63 |
this.doubleValue = v.doubleValue; |
| 64 |
this.boolValue = v.boolValue; |
| 65 |
this.byteValue = v.byteValue; |
| 66 |
this.charValue = v.charValue; |
| 67 |
this.obj = v.obj; |
| 68 |
return this; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get the Value's type |
| 73 |
* @return The Class of this Value. |
| 74 |
* |
| 75 |
*/ |
| 76 |
public Class<?> getType() { |
| 77 |
return type; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Set the Value's internal value to an integer. |
| 82 |
* @param val The integer value. |
| 83 |
* @return The Value object with the given internal value. |
| 84 |
* |
| 85 |
*/ |
| 86 |
public Value set(int val) { intValue = val; type = TYPE_INTEGER; return this; } |
| 87 |
|
| 88 |
/** |
| 89 |
* Set the Value's internal value to a short. |
| 90 |
* @param val The short value. |
| 91 |
* @return The Value object with the given internal value. |
| 92 |
* |
| 93 |
*/ |
| 94 |
public Value set(short val) { shortValue = val; type = TYPE_SHORT; return this; } |
| 95 |
|
| 96 |
/** |
| 97 |
* Set the Value's internal value to a long. |
| 98 |
* @param val The long value. |
| 99 |
* @return The Value object with the given internal value. |
| 100 |
* |
| 101 |
*/ |
| 102 |
public Value set(long val) { longValue = val; type = TYPE_LONG; return this; } |
| 103 |
|
| 104 |
/** |
| 105 |
* Set the Value's internal value to a float. |
| 106 |
* @param val The float value. |
| 107 |
* @return The Value object with the given internal value. |
| 108 |
* |
| 109 |
*/ |
| 110 |
public Value set(float val) { floatValue = val; type = TYPE_FLOAT; return this; } |
| 111 |
|
| 112 |
/** |
| 113 |
* Set the Value's internal value to a double. |
| 114 |
* @param val The double value. |
| 115 |
* @return The Value object with the given internal value. |
| 116 |
* |
| 117 |
*/ |
| 118 |
public Value set(double val) { doubleValue = val; type = TYPE_DOUBLE; return this; } |
| 119 |
|
| 120 |
/** |
| 121 |
* Set the Value's internal value to a boolean. |
| 122 |
* @param val The boolean value. |
| 123 |
* @return The Value object with the given internal value. |
| 124 |
* |
| 125 |
*/ |
| 126 |
public Value set(boolean val){ boolValue = val; type = TYPE_BOOLEAN; return this; } |
| 127 |
|
| 128 |
/** |
| 129 |
* Set the Value's internal value to a byte. |
| 130 |
* @param val The byte value. |
| 131 |
* @return The Value object with the given internal value. |
| 132 |
* |
| 133 |
*/ |
| 134 |
public Value set(byte val) { byteValue = val; type = TYPE_BYTE; return this; } |
| 135 |
|
| 136 |
/** |
| 137 |
* Set the Value's internal value to a char. |
| 138 |
* @param val The char value. |
| 139 |
* @return The Value object with the given internal value. |
| 140 |
* |
| 141 |
*/ |
| 142 |
public Value set(char val) { charValue = val; type = TYPE_CHAR; return this; } |
| 143 |
|
| 144 |
/** |
| 145 |
* Set the Value's internal value to a String. |
| 146 |
* @param val The String value. |
| 147 |
* @return The Value object with the given internal value. |
| 148 |
* |
| 149 |
*/ |
| 150 |
public Value set(String val) { obj = val; type = TYPE_STRING; return this; } |
| 151 |
|
| 152 |
/** |
| 153 |
* Set the Value's internal value to a Date. |
| 154 |
* @param val The Date value. |
| 155 |
* @return The Value object with the given internal value. |
| 156 |
* |
| 157 |
*/ |
| 158 |
public Value set(Date val) { obj = val; type = TYPE_DATE; return this; } |
| 159 |
|
| 160 |
/** |
| 161 |
* Set the Value's internal value to an Object. |
| 162 |
* @param val The Object value. |
| 163 |
* @return The Value object with the given internal value. |
| 164 |
* |
| 165 |
*/ |
| 166 |
public Value set(Object val) { obj = val; type = obj == null ? Object.class : obj.getClass(); return this; } |
| 167 |
|
| 168 |
/** |
| 169 |
* Get the integer value. |
| 170 |
* @return The int value. |
| 171 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 172 |
* |
| 173 |
*/ |
| 174 |
public int getInt() { |
| 175 |
if (type == TYPE_INTEGER) return intValue; |
| 176 |
else if (type == TYPE_SHORT) return shortValue; |
| 177 |
else if (type == TYPE_BYTE) return byteValue; |
| 178 |
else throw new ClassCastException( "getInt cannot be called for type "+type.toString()); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get the short value. |
| 183 |
* @return The short value. |
| 184 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 185 |
* |
| 186 |
*/ |
| 187 |
public short getShort() { |
| 188 |
if (type == TYPE_SHORT) return shortValue; |
| 189 |
else if (type == TYPE_BYTE) return byteValue; |
| 190 |
else throw new ClassCastException( "getShort cannot be called for type "+type.toString()); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get the long value. |
| 195 |
* @return The long value. |
| 196 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 197 |
* |
| 198 |
*/ |
| 199 |
public long getLong() { |
| 200 |
if (type == TYPE_LONG) return longValue; |
| 201 |
else if (type == TYPE_INTEGER) return intValue; |
| 202 |
else if (type == TYPE_SHORT) return shortValue; |
| 203 |
else if (type == TYPE_BYTE) return byteValue; |
| 204 |
else throw new ClassCastException( "getLong cannot be called for type "+type.toString()); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get the float value. |
| 209 |
* @return The float value. |
| 210 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 211 |
* |
| 212 |
*/ |
| 213 |
public float getFloat() { |
| 214 |
if (type == TYPE_FLOAT) return floatValue; |
| 215 |
else if (type == TYPE_INTEGER) return intValue; |
| 216 |
else if (type == TYPE_SHORT) return shortValue; |
| 217 |
else if (type == TYPE_LONG) return longValue; |
| 218 |
else if (type == TYPE_BYTE) return byteValue; |
| 219 |
else throw new ClassCastException( "getFloat cannot be called for type "+type.toString()); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Get the double value. |
| 224 |
* @return The double value. |
| 225 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 226 |
* |
| 227 |
*/ |
| 228 |
public double getDouble() { |
| 229 |
if (type == TYPE_DOUBLE) return doubleValue; |
| 230 |
else if (type == TYPE_INTEGER) return intValue; |
| 231 |
else if (type == TYPE_SHORT) return shortValue; |
| 232 |
else if (type == TYPE_LONG) return longValue; |
| 233 |
else if (type == TYPE_FLOAT) return floatValue; |
| 234 |
else if (type == TYPE_BYTE) return byteValue; |
| 235 |
else if (type == TYPE_DATE) return ((Date)obj).getTime(); |
| 236 |
else throw new ClassCastException( "getDouble cannot be called for type "+type.toString()); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get the boolean value. |
| 241 |
* @return The boolean value. |
| 242 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 243 |
* |
| 244 |
*/ |
| 245 |
public boolean getBoolean() { |
| 246 |
if (type == TYPE_BOOLEAN) return boolValue; |
| 247 |
else throw new ClassCastException( "getBoolean cannot be called for type "+type.toString()); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Get the byte value. |
| 252 |
* @return The byte value. |
| 253 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 254 |
* |
| 255 |
*/ |
| 256 |
public byte getByte() { |
| 257 |
if (type == TYPE_BYTE) return byteValue; |
| 258 |
else throw new ClassCastException( "getByte cannot be called for type "+type.toString()); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Get the char value. |
| 263 |
* @return The char value. |
| 264 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 265 |
* |
| 266 |
*/ |
| 267 |
public char getChar() { |
| 268 |
if (type == TYPE_CHAR) return charValue; |
| 269 |
else throw new ClassCastException( "getChar cannot be called for type "+type.toString()); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Get the String value. |
| 274 |
* @return The String representation of the internal value. |
| 275 |
* |
| 276 |
*/ |
| 277 |
public String getString() { |
| 278 |
if (type == TYPE_STRING) return (String)obj; |
| 279 |
else if (type == TYPE_INTEGER) return String.valueOf(intValue); |
| 280 |
else if (type == TYPE_SHORT) return String.valueOf(shortValue); |
| 281 |
else if (type == TYPE_LONG) return String.valueOf(longValue); |
| 282 |
else if (type == TYPE_FLOAT) return String.valueOf(floatValue); |
| 283 |
else if (type == TYPE_DOUBLE) return String.valueOf(doubleValue); |
| 284 |
else if (type == TYPE_BOOLEAN) return String.valueOf(boolValue); |
| 285 |
else if (type == TYPE_BYTE) return String.valueOf(byteValue); |
| 286 |
else if (type == TYPE_CHAR) return String.valueOf(charValue); |
| 287 |
else if (type == TYPE_DATE) return ((Date)obj).toString(); |
| 288 |
else return obj != null ? obj.toString(): "null"; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Get the Date value. |
| 293 |
* @return The Date value. |
| 294 |
* @exception A ClassCastException is thrown if this Value has incompatible type. |
| 295 |
* |
| 296 |
*/ |
| 297 |
public Date getDate() { |
| 298 |
if (type == TYPE_DATE) return (Date)obj; |
| 299 |
else throw new ClassCastException( "getDate cannot be called for type "+type.toString()); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Get the Object value. |
| 304 |
* @return The Object value. |
| 305 |
* |
| 306 |
*/ |
| 307 |
public Object getObject() { |
| 308 |
if (obj != null) return obj; |
| 309 |
else if (type == TYPE_INTEGER) return new Integer(intValue); |
| 310 |
else if (type == TYPE_SHORT) return new Short(shortValue); |
| 311 |
else if (type == TYPE_LONG) return new Long(longValue); |
| 312 |
else if (type == TYPE_FLOAT) return new Float(floatValue); |
| 313 |
else if (type == TYPE_DOUBLE) return new Double(doubleValue); |
| 314 |
else if (type == TYPE_BOOLEAN) return new Boolean(boolValue); |
| 315 |
else if (type == TYPE_BYTE) return new Byte(byteValue); |
| 316 |
else if (type == TYPE_CHAR) return new Character(charValue); |
| 317 |
else return null; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Get the String value. |
| 322 |
* @return The String representation of the internal value. |
| 323 |
* |
| 324 |
*/ |
| 325 |
public String toString() { |
| 326 |
return getString(); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Returns an external representation of this value |
| 331 |
*/ |
| 332 |
public String toExternal() { |
| 333 |
// FIXME, does not work for arrays... |
| 334 |
return type.getName()+":"+getString(); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Set to value from the external respresentation |
| 339 |
*/ |
| 340 |
public Value fromExternal(String external) throws IllegalArgumentException { |
| 341 |
String[] part = external.split(":", 2); |
| 342 |
if (part.length != 2) |
| 343 |
throw new IllegalArgumentException(getClass()+": External '"+external+ |
| 344 |
"'does not contain ':' to separate type from value."); |
| 345 |
|
| 346 |
if (part[0].equals(TYPE_STRING.getName())) { |
| 347 |
return set(part[1]); |
| 348 |
} else if (part[0].equals(TYPE_SHORT.getName())) { |
| 349 |
return set(Short.parseShort(part[1])); |
| 350 |
} else if (part[0].equals(TYPE_LONG.getName())) { |
| 351 |
return set(Long.parseLong(part[1])); |
| 352 |
} else if (part[0].equals(TYPE_FLOAT.getName())) { |
| 353 |
return set(Float.parseFloat(part[1])); |
| 354 |
} else if (part[0].equals(TYPE_DOUBLE.getName())) { |
| 355 |
return set(Double.parseDouble(part[1])); |
| 356 |
} else if (part[0].equals(TYPE_BOOLEAN.getName())) { |
| 357 |
return set(Boolean.getBoolean(part[1])); |
| 358 |
} else if (part[0].equals(TYPE_BYTE.getName())) { |
| 359 |
return set(Byte.parseByte(part[1])); |
| 360 |
} else if (part[0].equals(TYPE_CHAR.getName())) { |
| 361 |
return set(part[1].charAt(0)); |
| 362 |
} else if (part[0].equals(TYPE_INTEGER.getName())) { |
| 363 |
return set(Integer.parseInt(part[1])); |
| 364 |
} else if (part[0].equals(TYPE_DATE.getName())) { |
| 365 |
try { |
| 366 |
return set(new SimpleDateFormat().parse(part[1])); |
| 367 |
} catch (ParseException e) { |
| 368 |
throw new IllegalArgumentException(e.getMessage()); |
| 369 |
} |
| 370 |
} else if (part[0].equals(Object.class.getName()) && part[1].equals("null")) { |
| 371 |
return set((Object)null); |
| 372 |
} else { |
| 373 |
// FIXME will not work for arrays, which are encoded as "[Lpackagename.classname;" |
| 374 |
try { |
| 375 |
Class<?> cls = Class.forName(part[0]); |
| 376 |
Constructor<?> ctor = cls.getDeclaredConstructor(new Class[] { String.class }); |
| 377 |
ctor.setAccessible(true); |
| 378 |
return set(ctor.newInstance(new Object[] { part[1] })); |
| 379 |
} catch (Exception e) { |
| 380 |
throw new IllegalArgumentException(getClass()+": Cannot reconstruct value from type: "+part[0]+", " |
| 381 |
+"and value "+part[1]+", due to "+e.getMessage()); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
} |