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 /** Returns The comparator serialized using pb */ 076 @Override 077 public byte[] toByteArray() { 078 ComparatorProtos.BigDecimalComparator.Builder builder = 079 ComparatorProtos.BigDecimalComparator.newBuilder(); 080 builder.setComparable(ProtobufUtil.toByteArrayComparable(this.value)); 081 return builder.build().toByteArray(); 082 } 083 084 /** 085 * Parse a serialized representation of {@link BigDecimalComparator} 086 * @param pbBytes A pb serialized {@link BigDecimalComparator} instance 087 * @return An instance of {@link BigDecimalComparator} made from <code>bytes</code> 088 * @throws DeserializationException if an error occurred 089 * @see #toByteArray 090 */ 091 public static BigDecimalComparator parseFrom(final byte[] pbBytes) 092 throws DeserializationException { 093 ComparatorProtos.BigDecimalComparator proto; 094 try { 095 proto = ComparatorProtos.BigDecimalComparator.parseFrom(pbBytes); 096 } catch (InvalidProtocolBufferException e) { 097 throw new DeserializationException(e); 098 } 099 return new BigDecimalComparator( 100 Bytes.toBigDecimal(proto.getComparable().getValue().toByteArray())); 101 } 102 103 /** 104 * Returns true if and only if the fields of the comparator that are serialized are equal to the 105 * corresponding fields in other. Used for testing. 106 */ 107 @SuppressWarnings("ReferenceEquality") 108 boolean areSerializedFieldsEqual(BigDecimalComparator other) { 109 if (other == this) { 110 return true; 111 } 112 return super.areSerializedFieldsEqual(other); 113 } 114}