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  import org.apache.hadoop.hbase.exceptions.DeserializationException;
23  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
24  import org.apache.hadoop.hbase.util.Bytes;
25  
26  /**
27   * A long comparator which numerical compares against the specified byte array
28   */
29  
30  public class LongComparator extends ByteArrayComparable {
31      private Long longValue;
32  
33      public LongComparator(long value) {
34        super(Bytes.toBytes(value));
35        this.longValue = value;
36      }
37  
38      @Override
39      public int compareTo(byte[] value, int offset, int length) {
40        Long that = Bytes.toLong(value, offset, length);
41        return this.longValue.compareTo(that);
42      }
43  
44      /**
45       * @return The comparator serialized using pb
46       */
47      public byte [] toByteArray() {
48          ComparatorProtos.LongComparator.Builder builder =
49                  ComparatorProtos.LongComparator.newBuilder();
50          builder.setComparable(super.convert());
51          return builder.build().toByteArray();
52      }
53  
54      /**
55       * @param pbBytes A pb serialized {@link BinaryComparator} instance
56       * @return An instance of {@link BinaryComparator} made from <code>bytes</code>
57       * @throws org.apache.hadoop.hbase.exceptions.DeserializationException
58       * @see #toByteArray
59       */
60      public static LongComparator parseFrom(final byte [] pbBytes)
61              throws DeserializationException {
62          ComparatorProtos.LongComparator proto;
63          try {
64              proto = ComparatorProtos.LongComparator.parseFrom(pbBytes);
65          } catch (InvalidProtocolBufferException e) {
66              throw new DeserializationException(e);
67          }
68          return new LongComparator(Bytes.toLong(proto.getComparable().getValue().toByteArray()));
69      }
70  
71      /**
72       * @param other
73       * @return true if and only if the fields of the comparator that are serialized
74       * are equal to the corresponding fields in other.  Used for testing.
75       */
76      boolean areSerializedFieldsEqual(LongComparator other) {
77          if (other == this) return true;
78          if (!(other instanceof LongComparator)) return false;
79  
80          return super.areSerializedFieldsEqual(other);
81      }
82  }