001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import java.nio.ByteBuffer;
021import org.apache.hadoop.hbase.exceptions.DeserializationException;
022import org.apache.hadoop.hbase.util.ByteBufferUtils;
023import org.apache.hadoop.hbase.util.Bytes;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/** Base class for byte array comparators */
027@InterfaceAudience.Public
028// TODO Now we are deviating a lot from the actual Comparable<byte[]> that this implements, by
029// adding special compareTo methods. We have to clean it. Deprecate this class and replace it
030// with a more generic one which says it compares bytes (not necessary a byte array only)
031// BytesComparable implements Comparable<Byte> will work?
032@SuppressWarnings("ComparableType") // Should this move to Comparator usage?
033public abstract class ByteArrayComparable implements Comparable<byte[]> {
034
035  byte[] value;
036
037  /**
038   * Constructor.
039   * @param value the value to compare against
040   */
041  public ByteArrayComparable(byte[] value) {
042    this.value = value;
043  }
044
045  public byte[] getValue() {
046    return value;
047  }
048
049  /**
050   * @return The comparator serialized using pb
051   */
052  public abstract byte[] toByteArray();
053
054  /**
055   * @param pbBytes A pb serialized {@link ByteArrayComparable} instance
056   * @return An instance of {@link ByteArrayComparable} made from <code>bytes</code> n * @see
057   *         #toByteArray
058   */
059  public static ByteArrayComparable parseFrom(final byte[] pbBytes)
060    throws DeserializationException {
061    throw new DeserializationException(
062      "parseFrom called on base ByteArrayComparable, but should be called on derived type");
063  }
064
065  /**
066   * n * @return true if and only if the fields of the comparator that are serialized are equal to
067   * the corresponding fields in other. Used for testing.
068   */
069  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
070    if (other == this) return true;
071
072    return Bytes.equals(this.getValue(), other.getValue());
073  }
074
075  @Override
076  public int compareTo(byte[] value) {
077    return compareTo(value, 0, value.length);
078  }
079
080  /**
081   * Special compareTo method for subclasses, to avoid copying byte[] unnecessarily.
082   * @param value  byte[] to compare
083   * @param offset offset into value
084   * @param length number of bytes to compare
085   * @return a negative integer, zero, or a positive integer as this object is less than, equal to,
086   *         or greater than the specified object.
087   */
088  public abstract int compareTo(byte[] value, int offset, int length);
089
090  /**
091   * Special compareTo method for subclasses, to avoid copying bytes unnecessarily.
092   * @param value  bytes to compare within a ByteBuffer
093   * @param offset offset into value
094   * @param length number of bytes to compare
095   * @return a negative integer, zero, or a positive integer as this object is less than, equal to,
096   *         or greater than the specified object.
097   */
098  public int compareTo(ByteBuffer value, int offset, int length) {
099    // For BC, providing a default implementation here which is doing a bytes copy to a temp byte[]
100    // and calling compareTo(byte[]). Make sure to override this method in subclasses to avoid
101    // copying bytes unnecessarily.
102    byte[] temp = new byte[length];
103    ByteBufferUtils.copyFromBufferToArray(temp, value, offset, 0, length);
104    return compareTo(temp);
105  }
106}