001: package prefuse.data.expression;
002:
003: import prefuse.data.Schema;
004: import prefuse.data.Tuple;
005: import prefuse.util.TypeLib;
006:
007: /**
008: * Expression supporting basic arithmetic: add, subtract, multiply,
009: * divide, exponentiate (pow), and modulo (%).
010: *
011: * @author <a href="http://jheer.org">jeffrey heer</a>
012: */
013: public class ArithmeticExpression extends BinaryExpression {
014:
015: /** Indicates an addition operation. */
016: public static final int ADD = 0;
017: /** Indicates a subtraction operation. */
018: public static final int SUB = 1;
019: /** Indicates a multiplication operation. */
020: public static final int MUL = 2;
021: /** Indicates a division operation. */
022: public static final int DIV = 3;
023: /** Indicates an exponentiation (pow) operation. */
024: public static final int POW = 4;
025: /** Indicates a modulo operation. */
026: public static final int MOD = 5;
027:
028: private Class m_type;
029:
030: /**
031: * Create a new ArithmeticExpression.
032: * @param operation the operation to perform
033: * @param left the left sub-expression
034: * @param right the right sub-expression
035: */
036: public ArithmeticExpression(int operation, Expression left,
037: Expression right) {
038: super (operation, ADD, MOD, left, right);
039: m_type = null;
040: }
041:
042: /**
043: * @see prefuse.data.expression.Expression#getType(prefuse.data.Schema)
044: */
045: public Class getType(Schema s) {
046: if (m_type == null) {
047: Class lType = m_left.getType(s);
048: Class rType = m_right.getType(s);
049:
050: // determine this class's type
051: m_type = TypeLib.getNumericType(lType, rType);
052: }
053: return m_type;
054: }
055:
056: /**
057: * @see prefuse.data.expression.Expression#get(prefuse.data.Tuple)
058: */
059: public Object get(Tuple t) {
060: Class type = getType(t.getSchema());
061: if (int.class == type || byte.class == type) {
062: return new Integer(getInt(t));
063: } else if (long.class == type) {
064: return new Long(getInt(t));
065: } else if (float.class == type) {
066: return new Float(getFloat(t));
067: } else if (double.class == type) {
068: return new Double(getDouble(t));
069: } else {
070: throw new IllegalStateException();
071: }
072:
073: }
074:
075: /**
076: * @see prefuse.data.expression.Expression#getInt(prefuse.data.Tuple)
077: */
078: public int getInt(Tuple t) {
079: int x = m_left.getInt(t);
080: int y = m_right.getInt(t);
081:
082: // compute return value
083: switch (m_op) {
084: case ADD:
085: return x + y;
086: case SUB:
087: return x - y;
088: case MUL:
089: return x * y;
090: case DIV:
091: return x / y;
092: case POW:
093: return (int) Math.pow(x, y);
094: case MOD:
095: return x % y;
096: }
097: throw new IllegalStateException("Unknown operation type.");
098: }
099:
100: /**
101: * @see prefuse.data.expression.Expression#getLong(prefuse.data.Tuple)
102: */
103: public long getLong(Tuple t) {
104: long x = m_left.getLong(t);
105: long y = m_right.getLong(t);
106:
107: // compute return value
108: switch (m_op) {
109: case ADD:
110: return x + y;
111: case SUB:
112: return x - y;
113: case MUL:
114: return x * y;
115: case DIV:
116: return x / y;
117: case POW:
118: return (long) Math.pow(x, y);
119: case MOD:
120: return x % y;
121: }
122: throw new IllegalStateException("Unknown operation type.");
123: }
124:
125: /**
126: * @see prefuse.data.expression.Expression#getFloat(prefuse.data.Tuple)
127: */
128: public float getFloat(Tuple t) {
129: float x = m_left.getFloat(t);
130: float y = m_right.getFloat(t);
131:
132: // compute return value
133: switch (m_op) {
134: case ADD:
135: return x + y;
136: case SUB:
137: return x - y;
138: case MUL:
139: return x * y;
140: case DIV:
141: return x / y;
142: case POW:
143: return (float) Math.pow(x, y);
144: case MOD:
145: return (float) Math.IEEEremainder(x, y);
146: }
147: throw new IllegalStateException("Unknown operation type.");
148: }
149:
150: /**
151: * @see prefuse.data.expression.Expression#getDouble(prefuse.data.Tuple)
152: */
153: public double getDouble(Tuple t) {
154: double x = m_left.getDouble(t);
155: double y = m_right.getDouble(t);
156:
157: // compute return value
158: switch (m_op) {
159: case ADD:
160: return x + y;
161: case SUB:
162: return x - y;
163: case MUL:
164: return x * y;
165: case DIV:
166: return x / y;
167: case POW:
168: return Math.pow(x, y);
169: case MOD:
170: return Math.IEEEremainder(x, y);
171: }
172: throw new IllegalStateException("Unknown operation type.");
173: }
174:
175: /**
176: * @see java.lang.Object#toString()
177: */
178: public String toString() {
179: char op = '?';
180: switch (m_op) {
181: case ADD:
182: op = '+';
183: break;
184: case SUB:
185: op = '-';
186: break;
187: case MUL:
188: op = '*';
189: break;
190: case DIV:
191: op = '/';
192: break;
193: case POW:
194: op = '^';
195: break;
196: case MOD:
197: op = '%';
198: break;
199: }
200: return '(' + m_left.toString() + ' ' + op + ' '
201: + m_right.toString() + ')';
202: }
203:
204: } // end of class ArithmeticExpression
|