KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > expression > Literal


1 package prefuse.data.expression;
2
3 import prefuse.data.Tuple;
4 import prefuse.util.TypeLib;
5
6 /**
7  * Abstarct base class for a Literal Expression that evaluates to a
8  * constant value.
9  *
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 JavaDoc field) {
23         Class JavaDoc type = t.getColumnType(field);
24         if ( type == int.class )
25         {
26             return new NumericLiteral(t.getInt(field));
27         }
28         else if ( type == long.class )
29         {
30             return new NumericLiteral(t.getLong(field));
31         }
32         else if ( type == float.class )
33         {
34             return new NumericLiteral(t.getFloat(field));
35         }
36         else if ( type == double.class )
37         {
38             return new NumericLiteral(t.getDouble(field));
39         }
40         else if ( type == boolean.class )
41         {
42             return new BooleanLiteral(t.getBoolean(field));
43         }
44         else
45         {
46             return new ObjectLiteral(t.get(field));
47         }
48     }
49     
50     /**
51      * Return the given object as a new Literal instance.
52      * @param val the object value
53      * @return a new Literal expression containing the
54      * object value. The type is assumed to be the
55      * value's concrete runtime type.
56      */

57     public static Literal getLiteral(Object JavaDoc val) {
58         return getLiteral(val, val.getClass());
59     }
60     
61     /**
62      * Return the given object as a new Literal instance.
63      * @param val the object value
64      * @param type the type the literal should take
65      * @return a new Literal expression containing the
66      * object value
67      */

68     public static Literal getLiteral(Object JavaDoc val, Class JavaDoc type) {
69         if ( TypeLib.isNumericType(type) )
70         {
71             return new NumericLiteral(val);
72         }
73         else if ( type == boolean.class )
74         {
75             return new BooleanLiteral(((Boolean JavaDoc)val).booleanValue());
76         }
77         else
78         {
79             if ( type.isInstance(val) ) {
80                 return new ObjectLiteral(val);
81             } else {
82                 throw new IllegalArgumentException JavaDoc("Object does "
83                         + "not match the provided Class type.");
84             }
85         }
86     }
87     
88 } // end of abstarct class Literal
89
Popular Tags