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      public byte [] toByteArray() {
52          ComparatorProtos.LongComparator.Builder builder =
53                  ComparatorProtos.LongComparator.newBuilder();
54          builder.setComparable(super.convert());
55          return builder.build().toByteArray();
56      }
57  
58      /**
59       * @param pbBytes A pb serialized {@link BinaryComparator} instance
60       * @return An instance of {@link BinaryComparator} made from <code>bytes</code>
61       * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
62       * @see #toByteArray
63       */
64      public static LongComparator parseFrom(final byte [] pbBytes)
65              throws DeserializationException {
66          ComparatorProtos.LongComparator proto;
67          try {
68              proto = ComparatorProtos.LongComparator.parseFrom(pbBytes);
69          } catch (InvalidProtocolBufferException e) {
70              throw new DeserializationException(e);
71          }
72          return new LongComparator(Bytes.toLong(proto.getComparable().getValue().toByteArray()));
73      }
74  
75      /**
76       * @param other
77       * @return true if and only if the fields of the comparator that are serialized
78       * are equal to the corresponding fields in other.  Used for testing.
79       */
80      boolean areSerializedFieldsEqual(LongComparator other) {
81          if (other == this) return true;
82          if (!(other instanceof LongComparator)) return false;
83  
84          return super.areSerializedFieldsEqual(other);
85      }
86  }