View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.filter;
20  
21  import org.apache.hadoop.hbase.util.ByteStringer;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  
29  /** Base class for byte array comparators */
30  @InterfaceAudience.Public
31  @InterfaceStability.Stable
32  public abstract class ByteArrayComparable implements Comparable<byte[]> {
33  
34    byte[] value;
35  
36    /**
37     * Constructor.
38     * @param value the value to compare against
39     */
40    public ByteArrayComparable(byte [] value) {
41      this.value = value;
42    }
43  
44    public byte[] getValue() {
45      return value;
46    }
47  
48    /**
49     * @return The comparator serialized using pb
50     */
51    public abstract byte [] toByteArray();
52  
53    ComparatorProtos.ByteArrayComparable convert() {
54      ComparatorProtos.ByteArrayComparable.Builder builder =
55        ComparatorProtos.ByteArrayComparable.newBuilder();
56      if (value != null) builder.setValue(ByteStringer.wrap(value));
57      return builder.build();
58    }
59  
60    /**
61     * @param pbBytes A pb serialized {@link ByteArrayComparable} instance
62     * @return An instance of {@link ByteArrayComparable} made from <code>bytes</code>
63     * @throws DeserializationException
64     * @see #toByteArray
65     */
66    public static ByteArrayComparable parseFrom(final byte [] pbBytes)
67    throws DeserializationException {
68      throw new DeserializationException(
69        "parseFrom called on base ByteArrayComparable, but should be called on derived type");
70    }
71  
72    /**
73     * @param other
74     * @return true if and only if the fields of the comparator that are serialized
75     * are equal to the corresponding fields in other.  Used for testing.
76     */
77    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
78      if (other == this) return true;
79  
80      return Bytes.equals(this.getValue(), other.getValue());
81    }
82  
83    @Override
84    public int compareTo(byte [] value) {
85      return compareTo(value, 0, value.length);
86    }
87  
88    /**
89     * Special compareTo method for subclasses, to avoid
90     * copying byte[] unnecessarily.
91     * @param value byte[] to compare
92     * @param offset offset into value
93     * @param length number of bytes to compare
94     * @return a negative integer, zero, or a positive integer as this object
95     *         is less than, equal to, or greater than the specified object.
96     */
97    public abstract int compareTo(byte [] value, int offset, int length);
98  }