KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.expression;
2
3 import prefuse.data.Schema;
4 import prefuse.data.Tuple;
5 import prefuse.util.TypeLib;
6
7 /**
8  * Literal expression of a numeric value.
9  *
10  * @author <a HREF="http://jheer.org">jeffrey heer</a>
11  */

12 public class NumericLiteral extends Literal {
13
14     private final Number JavaDoc m_number;
15     private final Class JavaDoc m_type;
16     
17     // ------------------------------------------------------------------------
18
// Constructors
19

20     /**
21      * Create a new integer NumericLiteral.
22      * @param x the literal numeric value
23      */

24     public NumericLiteral(int x) {
25         m_number = new Integer JavaDoc(x);
26         m_type = int.class;
27     }
28
29     /**
30      * Create a new long NumericLiteral.
31      * @param x the literal numeric value
32      */

33     public NumericLiteral(long x) {
34         m_number = new Long JavaDoc(x);
35         m_type = long.class;
36     }
37     
38     /**
39      * Create a new float NumericLiteral.
40      * @param x the literal numeric value
41      */

42     public NumericLiteral(float x) {
43         m_number = new Float JavaDoc(x);
44         m_type = float.class;
45     }
46     
47     /**
48      * Create a new double NumericLiteral.
49      * @param x the literal numeric value
50      */

51     public NumericLiteral(double x) {
52         m_number = new Double JavaDoc(x);
53         m_type = double.class;
54     }
55     
56     /**
57      * Create a new NumericLiteral.
58      * @param x the literal numeric value, must be an instance of
59      * {@link java.lang.Number}, otherwise an exception will be thrown.
60      */

61     public NumericLiteral(Object JavaDoc x) {
62         if ( x instanceof Number JavaDoc ) {
63             m_number = (Number JavaDoc)x;
64             m_type = TypeLib.getPrimitiveType(m_number.getClass());
65         } else {
66             throw new IllegalArgumentException JavaDoc("Invalid type!");
67         }
68     }
69
70     // ------------------------------------------------------------------------
71
// Expression Interface
72

73     /**
74      * @see prefuse.data.expression.Expression#getType(prefuse.data.Schema)
75      */

76     public Class JavaDoc getType(Schema s) {
77         return m_type;
78     }
79
80     /**
81      * @see prefuse.data.expression.Expression#get(prefuse.data.Tuple)
82      */

83     public Object JavaDoc get(Tuple t) {
84         return m_number;
85     }
86
87     /**
88      * @see prefuse.data.expression.Expression#getInt(prefuse.data.Tuple)
89      */

90     public int getInt(Tuple t) {
91         return m_number.intValue();
92     }
93
94     /**
95      * @see prefuse.data.expression.Expression#getLong(prefuse.data.Tuple)
96      */

97     public long getLong(Tuple t) {
98         return m_number.longValue();
99     }
100
101     /**
102      * @see prefuse.data.expression.Expression#getFloat(prefuse.data.Tuple)
103      */

104     public float getFloat(Tuple t) {
105         return m_number.floatValue();
106     }
107
108     /**
109      * @see prefuse.data.expression.Expression#getDouble(prefuse.data.Tuple)
110      */

111     public double getDouble(Tuple t) {
112         return m_number.doubleValue();
113     }
114
115     /**
116      * @see java.lang.Object#toString()
117      */

118     public String JavaDoc toString() {
119         return m_number.toString();
120     }
121     
122 } // end of class NumericLiteral
123
Popular Tags