KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.expression;
2
3 import prefuse.data.Schema;
4 import prefuse.data.Tuple;
5 import prefuse.util.TypeLib;
6
7 /**
8  * Expression supporting basic arithmetic: add, subtract, multiply,
9  * divide, exponentiate (pow), and modulo (%).
10  *
11  * @author <a HREF="http://jheer.org">jeffrey heer</a>
12  */

13 public class ArithmeticExpression extends BinaryExpression {
14
15     /** Indicates an addition operation. */
16     public static final int ADD = 0;
17     /** Indicates a subtraction operation. */
18     public static final int SUB = 1;
19     /** Indicates a multiplication operation. */
20     public static final int MUL = 2;
21     /** Indicates a division operation. */
22     public static final int DIV = 3;
23     /** Indicates an exponentiation (pow) operation. */
24     public static final int POW = 4;
25     /** Indicates a modulo operation. */
26     public static final int MOD = 5;
27
28     private Class JavaDoc m_type;
29     
30     /**
31      * Create a new ArithmeticExpression.
32      * @param operation the operation to perform
33      * @param left the left sub-expression
34      * @param right the right sub-expression
35      */

36     public ArithmeticExpression(int operation,
37             Expression left, Expression right)
38     {
39         super(operation, ADD, MOD, left, right);
40         m_type = null;
41     }
42     
43     /**
44      * @see prefuse.data.expression.Expression#getType(prefuse.data.Schema)
45      */

46     public Class JavaDoc getType(Schema s) {
47         if ( m_type == null ) {
48             Class JavaDoc lType = m_left.getType(s);
49             Class JavaDoc rType = m_right.getType(s);
50         
51             // determine this class's type
52
m_type = TypeLib.getNumericType(lType, rType);
53         }
54         return m_type;
55     }
56
57     /**
58      * @see prefuse.data.expression.Expression#get(prefuse.data.Tuple)
59      */

60     public Object JavaDoc get(Tuple t) {
61         Class JavaDoc type = getType(t.getSchema());
62         if ( int.class == type || byte.class == type ) {
63             return new Integer JavaDoc(getInt(t));
64         } else if ( long.class == type ) {
65             return new Long JavaDoc(getInt(t));
66         } else if ( float.class == type ) {
67             return new Float JavaDoc(getFloat(t));
68         } else if ( double.class == type ) {
69             return new Double JavaDoc(getDouble(t));
70         } else {
71             throw new IllegalStateException JavaDoc();
72         }
73         
74     }
75
76     /**
77      * @see prefuse.data.expression.Expression#getInt(prefuse.data.Tuple)
78      */

79     public int getInt(Tuple t) {
80         int x = m_left.getInt(t);
81         int y = m_right.getInt(t);
82         
83         // compute return value
84
switch ( m_op ) {
85         case ADD:
86             return x+y;
87         case SUB:
88             return x-y;
89         case MUL:
90             return x*y;
91         case DIV:
92             return x/y;
93         case POW:
94             return (int)Math.pow(x,y);
95         case MOD:
96             return x%y;
97         }
98         throw new IllegalStateException JavaDoc("Unknown operation type.");
99     }
100
101     /**
102      * @see prefuse.data.expression.Expression#getLong(prefuse.data.Tuple)
103      */

104     public long getLong(Tuple t) {
105         long x = m_left.getLong(t);
106         long y = m_right.getLong(t);
107         
108         // compute return value
109
switch ( m_op ) {
110         case ADD:
111             return x+y;
112         case SUB:
113             return x-y;
114         case MUL:
115             return x*y;
116         case DIV:
117             return x/y;
118         case POW:
119             return (long)Math.pow(x,y);
120         case MOD:
121             return x%y;
122         }
123         throw new IllegalStateException JavaDoc("Unknown operation type.");
124     }
125
126     /**
127      * @see prefuse.data.expression.Expression#getFloat(prefuse.data.Tuple)
128      */

129     public float getFloat(Tuple t) {
130         float x = m_left.getFloat(t);
131         float y = m_right.getFloat(t);
132         
133         // compute return value
134
switch ( m_op ) {
135         case ADD:
136             return x+y;
137         case SUB:
138             return x-y;
139         case MUL:
140             return x*y;
141         case DIV:
142             return x/y;
143         case POW:
144             return (float)Math.pow(x,y);
145         case MOD:
146             return (float)Math.IEEEremainder(x,y);
147         }
148         throw new IllegalStateException JavaDoc("Unknown operation type.");
149     }
150
151     /**
152      * @see prefuse.data.expression.Expression#getDouble(prefuse.data.Tuple)
153      */

154     public double getDouble(Tuple t) {
155         double x = m_left.getDouble(t);
156         double y = m_right.getDouble(t);
157         
158         // compute return value
159
switch ( m_op ) {
160         case ADD:
161             return x+y;
162         case SUB:
163             return x-y;
164         case MUL:
165             return x*y;
166         case DIV:
167             return x/y;
168         case POW:
169             return Math.pow(x,y);
170         case MOD:
171             return Math.IEEEremainder(x,y);
172         }
173         throw new IllegalStateException JavaDoc("Unknown operation type.");
174     }
175
176     /**
177      * @see java.lang.Object#toString()
178      */

179     public String JavaDoc toString() {
180         char op = '?';
181         switch ( m_op ) {
182         case ADD:
183             op = '+';
184             break;
185         case SUB:
186             op = '-';
187             break;
188         case MUL:
189             op = '*';
190             break;
191         case DIV:
192             op = '/';
193             break;
194         case POW:
195             op = '^';
196             break;
197         case MOD:
198             op = '%';
199             break;
200         }
201         return '('+m_left.toString()+' '+op+' '+m_right.toString()+')';
202     }
203     
204 } // end of class ArithmeticExpression
205
Popular Tags