1 package prefuse.data.expression; 2 3 import prefuse.data.Schema; 4 import prefuse.data.Tuple; 5 import prefuse.util.TypeLib; 6 7 13 public class ArithmeticExpression extends BinaryExpression { 14 15 16 public static final int ADD = 0; 17 18 public static final int SUB = 1; 19 20 public static final int MUL = 2; 21 22 public static final int DIV = 3; 23 24 public static final int POW = 4; 25 26 public static final int MOD = 5; 27 28 private Class m_type; 29 30 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 46 public Class getType(Schema s) { 47 if ( m_type == null ) { 48 Class lType = m_left.getType(s); 49 Class rType = m_right.getType(s); 50 51 m_type = TypeLib.getNumericType(lType, rType); 53 } 54 return m_type; 55 } 56 57 60 public Object get(Tuple t) { 61 Class type = getType(t.getSchema()); 62 if ( int.class == type || byte.class == type ) { 63 return new Integer (getInt(t)); 64 } else if ( long.class == type ) { 65 return new Long (getInt(t)); 66 } else if ( float.class == type ) { 67 return new Float (getFloat(t)); 68 } else if ( double.class == type ) { 69 return new Double (getDouble(t)); 70 } else { 71 throw new IllegalStateException (); 72 } 73 74 } 75 76 79 public int getInt(Tuple t) { 80 int x = m_left.getInt(t); 81 int y = m_right.getInt(t); 82 83 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 ("Unknown operation type."); 99 } 100 101 104 public long getLong(Tuple t) { 105 long x = m_left.getLong(t); 106 long y = m_right.getLong(t); 107 108 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 ("Unknown operation type."); 124 } 125 126 129 public float getFloat(Tuple t) { 130 float x = m_left.getFloat(t); 131 float y = m_right.getFloat(t); 132 133 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 ("Unknown operation type."); 149 } 150 151 154 public double getDouble(Tuple t) { 155 double x = m_left.getDouble(t); 156 double y = m_right.getDouble(t); 157 158 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 ("Unknown operation type."); 174 } 175 176 179 public String 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 } | Popular Tags |