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.classification.InterfaceAudience;
22  import org.apache.hadoop.classification.InterfaceStability;
23  import org.apache.hadoop.hbase.exceptions.DeserializationException;
24  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
25  import org.apache.hadoop.hbase.util.Bytes;
26  
27  import com.google.protobuf.ZeroCopyLiteralByteString;
28  
29  
30  /** Base class for byte array comparators */
31  @InterfaceAudience.Public
32  @InterfaceStability.Stable
33  public abstract class ByteArrayComparable implements Comparable<byte[]> {
34  
35    byte[] value;
36  
37    /**
38     * Constructor.
39     * @param value the value to compare against
40     */
41    public ByteArrayComparable(byte [] value) {
42      this.value = value;
43    }
44  
45    public byte[] getValue() {
46      return value;
47    }
48  
49    /**
50     * @return The comparator serialized using pb
51     */
52    public abstract byte [] toByteArray();
53  
54    ComparatorProtos.ByteArrayComparable convert() {
55      ComparatorProtos.ByteArrayComparable.Builder builder =
56        ComparatorProtos.ByteArrayComparable.newBuilder();
57      if (value != null) builder.setValue(ZeroCopyLiteralByteString.wrap(value));
58      return builder.build();
59    }
60  
61    /**
62     * @param pbBytes A pb serialized {@link ByteArrayComparable} instance
63     * @return An instance of {@link ByteArrayComparable} made from <code>bytes</code>
64     * @throws DeserializationException
65     * @see #toByteArray
66     */
67    public static ByteArrayComparable parseFrom(final byte [] pbBytes)
68    throws DeserializationException {
69      throw new DeserializationException(
70        "parseFrom called on base ByteArrayComparable, but should be called on derived type");
71    }
72  
73    /**
74     * @param other
75     * @return true if and only if the fields of the comparator that are serialized
76     * are equal to the corresponding fields in other.  Used for testing.
77     */
78    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
79      if (other == this) return true;
80  
81      return Bytes.equals(this.getValue(), other.getValue());
82    }
83  
84    @Override
85    public int compareTo(byte [] value) {
86      return compareTo(value, 0, value.length);
87    }
88  
89    /**
90     * Special compareTo method for subclasses, to avoid
91     * copying byte[] unnecessarily.
92     * @param value byte[] to compare
93     * @param offset offset into value
94     * @param length number of bytes to compare
95     * @return a negative integer, zero, or a positive integer as this object
96     *         is less than, equal to, or greater than the specified object.
97     */
98    public abstract int compareTo(byte [] value, int offset, int length);
99  }