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.yetus.audience.InterfaceAudience;
023
024import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
025
026import org.apache.hadoop.hbase.shaded.protobuf.generated.ComparatorProtos;
027
028/**
029 * A binary comparator which lexicographically compares against the specified byte array using
030 * {@link org.apache.hadoop.hbase.util.Bytes#compareTo(byte[], byte[])}.
031 */
032@InterfaceAudience.Public
033@SuppressWarnings("ComparableType") // Should this move to Comparator usage?
034public class NullComparator extends ByteArrayComparable {
035
036  public NullComparator() {
037    super(new byte[0]);
038  }
039
040  @Override
041  public int compareTo(byte[] value) {
042    return value != null ? 1 : 0;
043  }
044
045  @Override
046  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "EQ_UNUSUAL", justification = "")
047  public boolean equals(Object obj) {
048    return obj == null;
049  }
050
051  @Override
052  public int hashCode() {
053    return 0;
054  }
055
056  @Override
057  public int compareTo(byte[] value, int offset, int length) {
058    return compareTo(value);
059  }
060
061  @Override
062  public int compareTo(ByteBuffer value, int offset, int length) {
063    return value != null ? 1 : 0;
064  }
065
066  /** Returns The comparator serialized using pb */
067  @Override
068  public byte[] toByteArray() {
069    ComparatorProtos.NullComparator.Builder builder = ComparatorProtos.NullComparator.newBuilder();
070    return builder.build().toByteArray();
071  }
072
073  /**
074   * @param pbBytes A pb serialized {@link NullComparator} instance
075   * @return An instance of {@link NullComparator} made from <code>bytes</code>
076   * @see #toByteArray
077   */
078  public static NullComparator parseFrom(final byte[] pbBytes) throws DeserializationException {
079    try {
080      // Just parse. Don't use what we parse since on end we are returning new NullComparator.
081      ComparatorProtos.NullComparator.parseFrom(pbBytes);
082    } catch (InvalidProtocolBufferException e) {
083      throw new DeserializationException(e);
084    }
085    return new NullComparator();
086  }
087
088  /**
089   * @return true if and only if the fields of the comparator that are serialized are equal to the
090   *         corresponding fields in other. Used for testing.
091   */
092  @Override
093  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
094    if (other == this) return true;
095    if (!(other instanceof NullComparator)) return false;
096
097    return super.areSerializedFieldsEqual(other);
098  }
099}