01: package prefuse.data.expression;
02:
03: import prefuse.data.Tuple;
04: import prefuse.util.TypeLib;
05:
06: /**
07: * Abstarct base class for a Literal Expression that evaluates to a
08: * constant value.
09: *
10: * @author <a href="http://jheer.org">jeffrey heer</a>
11: */
12: public abstract class Literal extends AbstractExpression {
13:
14: /**
15: * Evaluate the given tuple and data field and return the
16: * result as a new Literal instance.
17: * @param t the Tuple
18: * @param field the data field to lookup
19: * @return a new Literal expression containing the
20: * value of the Tuple's data field
21: */
22: public static Literal getLiteral(Tuple t, String field) {
23: Class type = t.getColumnType(field);
24: if (type == int.class) {
25: return new NumericLiteral(t.getInt(field));
26: } else if (type == long.class) {
27: return new NumericLiteral(t.getLong(field));
28: } else if (type == float.class) {
29: return new NumericLiteral(t.getFloat(field));
30: } else if (type == double.class) {
31: return new NumericLiteral(t.getDouble(field));
32: } else if (type == boolean.class) {
33: return new BooleanLiteral(t.getBoolean(field));
34: } else {
35: return new ObjectLiteral(t.get(field));
36: }
37: }
38:
39: /**
40: * Return the given object as a new Literal instance.
41: * @param val the object value
42: * @return a new Literal expression containing the
43: * object value. The type is assumed to be the
44: * value's concrete runtime type.
45: */
46: public static Literal getLiteral(Object val) {
47: return getLiteral(val, val.getClass());
48: }
49:
50: /**
51: * Return the given object as a new Literal instance.
52: * @param val the object value
53: * @param type the type the literal should take
54: * @return a new Literal expression containing the
55: * object value
56: */
57: public static Literal getLiteral(Object val, Class type) {
58: if (TypeLib.isNumericType(type)) {
59: return new NumericLiteral(val);
60: } else if (type == boolean.class) {
61: return new BooleanLiteral(((Boolean) val).booleanValue());
62: } else {
63: if (type.isInstance(val)) {
64: return new ObjectLiteral(val);
65: } else {
66: throw new IllegalArgumentException("Object does "
67: + "not match the provided Class type.");
68: }
69: }
70: }
71:
72: } // end of abstarct class Literal
|