View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.filter;
20  
21  import com.google.protobuf.InvalidProtocolBufferException;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.exceptions.DeserializationException;
26  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  /**
30   * A long comparator which numerical compares against the specified byte array
31   */
32  @InterfaceAudience.Public
33  @InterfaceStability.Stable
34  public class LongComparator extends ByteArrayComparable {
35      private Long longValue;
36  
37      public LongComparator(long value) {
38        super(Bytes.toBytes(value));
39        this.longValue = value;
40      }
41  
42      @Override
43      public int compareTo(byte[] value, int offset, int length) {
44        Long that = Bytes.toLong(value, offset, length);
45        return this.longValue.compareTo(that);
46      }
47  
48      /**
49       * @return The comparator serialized using pb
50       */
51      @Override
52      public byte [] toByteArray() {
53          ComparatorProtos.LongComparator.Builder builder =
54                  ComparatorProtos.LongComparator.newBuilder();
55          builder.setComparable(super.convert());
56          return builder.build().toByteArray();
57      }
58  
59      /**
60       * @param pbBytes A pb serialized {@link LongComparator} instance
61       * @return An instance of {@link LongComparator} made from <code>bytes</code>
62       * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
63       * @see #toByteArray
64       */
65      public static LongComparator parseFrom(final byte [] pbBytes)
66              throws DeserializationException {
67          ComparatorProtos.LongComparator proto;
68          try {
69              proto = ComparatorProtos.LongComparator.parseFrom(pbBytes);
70          } catch (InvalidProtocolBufferException e) {
71              throw new DeserializationException(e);
72          }
73          return new LongComparator(Bytes.toLong(proto.getComparable().getValue().toByteArray()));
74      }
75  
76      /**
77       * @param other
78       * @return true if and only if the fields of the comparator that are serialized
79       * are equal to the corresponding fields in other.  Used for testing.
80       */
81      boolean areSerializedFieldsEqual(LongComparator other) {
82          if (other == this) return true;
83          return super.areSerializedFieldsEqual(other);
84      }
85  }