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  /** Returns The comparator serialized using pb */
050  public abstract byte[] toByteArray();
051
052  /**
053   * Parse a serialized representation of {@link ByteArrayComparable}
054   * @param pbBytes A pb serialized {@link ByteArrayComparable} instance
055   * @return An instance of {@link ByteArrayComparable} made from <code>bytes</code>
056   * @see #toByteArray
057   */
058  @SuppressWarnings("DoNotCallSuggester")
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   * Return true if and only if the fields of the comparator that are serialized are equal to the
067   * corresponding fields in other.
068   */
069  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
070    if (other == this) {
071      return true;
072    }
073    return Bytes.equals(this.getValue(), other.getValue());
074  }
075
076  @Override
077  public int compareTo(byte[] value) {
078    return compareTo(value, 0, value.length);
079  }
080
081  /**
082   * Special compareTo method for subclasses, to avoid copying byte[] unnecessarily.
083   * @param value  byte[] to compare
084   * @param offset offset into value
085   * @param length number of bytes to compare
086   * @return a negative integer, zero, or a positive integer as this object is less than, equal to,
087   *         or greater than the specified object.
088   */
089  public abstract int compareTo(byte[] value, int offset, int length);
090
091  /**
092   * Special compareTo method for subclasses, to avoid copying bytes unnecessarily.
093   * @param value  bytes to compare within a ByteBuffer
094   * @param offset offset into value
095   * @param length number of bytes to compare
096   * @return a negative integer, zero, or a positive integer as this object is less than, equal to,
097   *         or greater than the specified object.
098   */
099  public int compareTo(ByteBuffer value, int offset, int length) {
100    // For BC, providing a default implementation here which is doing a bytes copy to a temp byte[]
101    // and calling compareTo(byte[]). Make sure to override this method in subclasses to avoid
102    // copying bytes unnecessarily.
103    byte[] temp = new byte[length];
104    ByteBufferUtils.copyFromBufferToArray(temp, value, offset, 0, length);
105    return compareTo(temp);
106  }
107}