1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.expreval.expr.calculation;
22
23 import org.apache.expreval.client.NullColumnValueException;
24 import org.apache.expreval.client.ResultMissingColumnException;
25 import org.apache.expreval.expr.ExpressionType;
26 import org.apache.expreval.expr.Operator;
27 import org.apache.expreval.expr.node.GenericValue;
28 import org.apache.expreval.expr.node.NumberValue;
29 import org.apache.hadoop.hbase.hbql.client.HBqlException;
30 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
31
32 public class NumericCalculation extends GenericCalculation implements NumberValue {
33
34 public NumericCalculation(final GenericValue arg0, final Operator operator, final GenericValue arg1) {
35 super(ExpressionType.NUMBERCALCULATION, arg0, operator, arg1);
36 }
37
38 public Class<? extends GenericValue> validateTypes(final GenericValue parentExpr,
39 final boolean allowCollections) throws HBqlException {
40 return this.validateNumericTypes();
41 }
42
43 public Number getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
44 ResultMissingColumnException,
45 NullColumnValueException {
46
47 final Object obj0 = this.getExprArg(0).getValue(conn, object);
48 final Object obj1 = this.getExprArg(1).getValue(conn, object);
49
50 this.validateNumericArgTypes(obj0, obj1);
51
52 if (!this.useDecimal()) {
53
54 final long val0 = ((Number)obj0).longValue();
55 final long val1 = ((Number)obj1).longValue();
56
57 final long result;
58
59 switch (this.getOperator()) {
60 case PLUS:
61 result = val0 + val1;
62 break;
63 case MINUS:
64 result = val0 - val1;
65 break;
66 case MULT:
67 result = val0 * val1;
68 break;
69 case DIV:
70 result = val0 / val1;
71 break;
72 case MOD:
73 result = val0 % val1;
74 break;
75 case NEGATIVE:
76 result = val0 * -1;
77 break;
78 default:
79 throw new HBqlException("Invalid operator: " + this.getOperator());
80 }
81
82 return this.getValueWithCast(result);
83 }
84 else {
85
86 final double val0 = ((Number)obj0).doubleValue();
87 final double val1 = ((Number)obj1).doubleValue();
88
89 final double result;
90
91 switch (this.getOperator()) {
92 case PLUS:
93 result = val0 + val1;
94 break;
95 case MINUS:
96 result = val0 - val1;
97 break;
98 case MULT:
99 result = val0 * val1;
100 break;
101 case DIV:
102 result = val0 / val1;
103 break;
104 case MOD:
105 result = val0 % val1;
106 break;
107 case NEGATIVE:
108 result = val0 * -1;
109 break;
110 default:
111 throw new HBqlException("Invalid operator: " + this.getOperator());
112 }
113
114 return this.getValueWithCast(result);
115 }
116 }
117 }