View Javadoc

1   /*
2    * Copyright (c) 2010.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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 NumberCalculation extends GenericCalculation implements NumberValue {
33  
34      public NumberCalculation(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 }