View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client.coprocessor;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  import java.math.BigDecimal;
25  import java.math.RoundingMode;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.hbase.coprocessor.ColumnInterpreter;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  /**
33   * ColumnInterpreter for doing Aggregation's with BigDecimal columns. 
34   * This class is required at the RegionServer also.
35   *
36   */
37  public class BigDecimalColumnInterpreter implements ColumnInterpreter<BigDecimal, BigDecimal> {
38    private static final Log log = LogFactory.getLog(BigDecimalColumnInterpreter.class);
39  
40    @Override
41    public void readFields(DataInput arg0) throws IOException {
42    }
43  
44    @Override
45    public void write(DataOutput arg0) throws IOException {
46    }
47  
48    @Override
49    public BigDecimal getValue(byte[] family, byte[] qualifier, KeyValue kv)
50        throws IOException {
51      if ((kv == null || kv.getValue() == null)) return null;
52      return Bytes.toBigDecimal(kv.getValue()).setScale(2, RoundingMode.HALF_EVEN);
53    }
54  
55    @Override
56    public BigDecimal add(BigDecimal val1, BigDecimal val2) {
57      if ((((val1 == null) ? 1 : 0) ^ ((val2 == null) ? 1 : 0)) != 0) {
58        return ((val1 == null) ? val2 : val1);
59      }
60      if (val1 == null) return null;
61      return val1.add(val2).setScale(2, RoundingMode.HALF_EVEN);
62    }
63  
64    @Override
65    public BigDecimal getMaxValue() {
66      return BigDecimal.valueOf(Double.MAX_VALUE);
67    }
68  
69    @Override
70    public BigDecimal getMinValue() {
71      return BigDecimal.valueOf(Double.MIN_VALUE);
72    }
73  
74    @Override
75    public BigDecimal multiply(BigDecimal val1, BigDecimal val2) {
76      return (((val1 == null) || (val2 == null)) ? null : val1.multiply(val2).setScale(2,
77        RoundingMode.HALF_EVEN));
78    }
79  
80    @Override
81    public BigDecimal increment(BigDecimal val) {
82      return ((val == null) ? null : val.add(BigDecimal.ONE));
83    }
84  
85    @Override
86    public BigDecimal castToReturnType(BigDecimal val) {
87      return val;
88    }
89  
90    @Override
91    public int compare(BigDecimal val1, BigDecimal val2) {
92      if ((((val1 == null) ? 1 : 0) ^ ((val2 == null) ? 1 : 0)) != 0) {
93        return ((val1 == null) ? -1 : 1);
94      }
95      if (val1 == null) return 0;
96      return val1.compareTo(val2);
97    }
98  
99    @Override
100   public double divideForAvg(BigDecimal val1, Long paramLong) {
101     return (((paramLong == null) || (val1 == null)) ? (Double.NaN) :
102       val1.doubleValue() / paramLong.doubleValue());
103   }
104 }