001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.filter; 019 020import java.math.BigDecimal; 021import java.nio.ByteBuffer; 022import java.util.Objects; 023import org.apache.hadoop.hbase.exceptions.DeserializationException; 024import org.apache.hadoop.hbase.util.ByteBufferUtils; 025import org.apache.hadoop.hbase.util.Bytes; 026import org.apache.yetus.audience.InterfaceAudience; 027 028import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException; 029 030import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 031import org.apache.hadoop.hbase.shaded.protobuf.generated.ComparatorProtos; 032 033/** 034 * A BigDecimal comparator which numerical compares against the specified byte array 035 */ 036@InterfaceAudience.Public 037@SuppressWarnings("ComparableType") // Should this move to Comparator usage? 038public class BigDecimalComparator extends ByteArrayComparable { 039 private BigDecimal bigDecimal; 040 041 public BigDecimalComparator(BigDecimal value) { 042 super(Bytes.toBytes(value)); 043 this.bigDecimal = value; 044 } 045 046 @Override 047 public boolean equals(Object obj) { 048 if (obj == null || !(obj instanceof BigDecimalComparator)) { 049 return false; 050 } 051 if (this == obj) { 052 return true; 053 } 054 BigDecimalComparator bdc = (BigDecimalComparator) obj; 055 return this.bigDecimal.equals(bdc.bigDecimal); 056 } 057 058 @Override 059 public int hashCode() { 060 return Objects.hash(this.bigDecimal); 061 } 062 063 @Override 064 public int compareTo(byte[] value, int offset, int length) { 065 BigDecimal that = Bytes.toBigDecimal(value, offset, length); 066 return this.bigDecimal.compareTo(that); 067 } 068 069 @Override 070 public int compareTo(ByteBuffer value, int offset, int length) { 071 BigDecimal that = ByteBufferUtils.toBigDecimal(value, offset, length); 072 return this.bigDecimal.compareTo(that); 073 } 074 075 /** 076 * @return The comparator serialized using pb 077 */ 078 @Override 079 public byte[] toByteArray() { 080 ComparatorProtos.BigDecimalComparator.Builder builder = 081 ComparatorProtos.BigDecimalComparator.newBuilder(); 082 builder.setComparable(ProtobufUtil.toByteArrayComparable(this.value)); 083 return builder.build().toByteArray(); 084 } 085 086 /** 087 * @param pbBytes A pb serialized {@link BigDecimalComparator} instance 088 * @return An instance of {@link BigDecimalComparator} made from <code>bytes</code> 089 * @throws DeserializationException A deserialization exception 090 * @see #toByteArray 091 */ 092 public static BigDecimalComparator parseFrom(final byte[] pbBytes) 093 throws DeserializationException { 094 ComparatorProtos.BigDecimalComparator proto; 095 try { 096 proto = ComparatorProtos.BigDecimalComparator.parseFrom(pbBytes); 097 } catch (InvalidProtocolBufferException e) { 098 throw new DeserializationException(e); 099 } 100 return new BigDecimalComparator( 101 Bytes.toBigDecimal(proto.getComparable().getValue().toByteArray())); 102 } 103 104 /** 105 * @param other the other comparator 106 * @return true if and only if the fields of the comparator that are serialized are equal to the 107 * corresponding fields in other. Used for testing. 108 */ 109 boolean areSerializedFieldsEqual(BigDecimalComparator other) { 110 if (other == this) { 111 return true; 112 } 113 return super.areSerializedFieldsEqual(other); 114 } 115}