001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.filter;
021
022import java.nio.ByteBuffer;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.hadoop.hbase.exceptions.DeserializationException;
026import org.apache.hadoop.hbase.shaded.protobuf.generated.ComparatorProtos;
027
028import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
029
030/**
031 * A binary comparator which lexicographically compares against the specified
032 * byte array using {@link org.apache.hadoop.hbase.util.Bytes#compareTo(byte[], byte[])}.
033 */
034@InterfaceAudience.Public
035@SuppressWarnings("ComparableType") // Should this move to Comparator usage?
036public class NullComparator extends ByteArrayComparable {
037
038  public NullComparator() {
039    super(new byte[0]);
040  }
041
042  @Override
043  public int compareTo(byte[] value) {
044    return value != null ? 1 : 0;
045  }
046
047  @Override
048  @edu.umd.cs.findbugs.annotations.SuppressWarnings (value="EQ_UNUSUAL", justification="")
049  public boolean equals(Object obj) {
050    return obj == null;
051  }
052
053  @Override
054  public int hashCode() {
055    return 0;
056  }
057
058  @Override
059  public int compareTo(byte[] value, int offset, int length) {
060    return compareTo(value);
061  }
062
063  @Override
064  public int compareTo(ByteBuffer value, int offset, int length) {
065    return value != null ? 1 : 0;
066  }
067
068  /**
069   * @return The comparator serialized using pb
070   */
071  @Override
072  public byte [] toByteArray() {
073    ComparatorProtos.NullComparator.Builder builder =
074      ComparatorProtos.NullComparator.newBuilder();
075    return builder.build().toByteArray();
076  }
077
078  /**
079   * @param pbBytes A pb serialized {@link NullComparator} instance
080   * @return An instance of {@link NullComparator} made from <code>bytes</code>
081   * @throws DeserializationException
082   * @see #toByteArray
083   */
084  public static NullComparator parseFrom(final byte [] pbBytes)
085  throws DeserializationException {
086    try {
087      // Just parse.  Don't use what we parse since on end we are returning new NullComparator.
088      ComparatorProtos.NullComparator.parseFrom(pbBytes);
089    } catch (InvalidProtocolBufferException e) {
090      throw new DeserializationException(e);
091    }
092    return new NullComparator();
093  }
094
095  /**
096   * @param other
097   * @return true if and only if the fields of the comparator that are serialized
098   * are equal to the corresponding fields in other.  Used for testing.
099   */
100  @Override
101  boolean areSerializedFieldsEqual(ByteArrayComparable other) {
102    if (other == this) return true;
103    if (!(other instanceof NullComparator)) return false;
104
105    return super.areSerializedFieldsEqual(other);
106  }
107}