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   * Parse the serialized representation of {@link NullComparator}
075   * @param pbBytes A pb serialized {@link NullComparator} instance
076   * @return An instance of {@link NullComparator} made from <code>bytes</code>
077   * @throws DeserializationException if an error occurred
078   * @see #toByteArray
079   */
080  public static NullComparator parseFrom(final byte[] pbBytes) throws DeserializationException {
081    try {
082      // Just parse. Don't use what we parse since on end we are returning new NullComparator.
083      ComparatorProtos.NullComparator.parseFrom(pbBytes);
084    } catch (InvalidProtocolBufferException e) {
085      throw new DeserializationException(e);
086    }
087    return new NullComparator();
088  }
089
090  /**
091   * Returns true if and only if the fields of the comparator that are serialized are equal to the
092   * corresponding fields in other. Used for testing.
093   */
094  @Override
095  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
096    if (other == this) {
097      return true;
098    }
099    if (!(other instanceof NullComparator)) {
100      return false;
101    }
102    return super.areSerializedFieldsEqual(other);
103  }
104}