View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.filter;
21  
22  import java.nio.ByteBuffer;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
28  
29  import com.google.protobuf.InvalidProtocolBufferException;
30  
31  /**
32   * A binary comparator which lexicographically compares against the specified
33   * byte array using {@link org.apache.hadoop.hbase.util.Bytes#compareTo(byte[], byte[])}.
34   */
35  @InterfaceAudience.Public
36  @InterfaceStability.Stable
37  public class NullComparator extends ByteArrayComparable {
38  
39    public NullComparator() {
40      super(new byte[0]);
41    }
42  
43    @Override
44    public int compareTo(byte[] value) {
45      return value != null ? 1 : 0;
46    }
47  
48    @Override
49    @edu.umd.cs.findbugs.annotations.SuppressWarnings (value="EQ_UNUSUAL", justification="")
50    public boolean equals(Object obj) {
51      return obj == null;
52    }
53  
54    @Override
55    public int hashCode() {
56      return 0;
57    }
58  
59    @Override
60    public int compareTo(byte[] value, int offset, int length) {
61      return compareTo(value);
62    }
63  
64    @Override
65    public int compareTo(ByteBuffer value, int offset, int length) {
66      return value != null ? 1 : 0;
67    }
68  
69    /**
70     * @return The comparator serialized using pb
71     */
72    public byte [] toByteArray() {
73      ComparatorProtos.NullComparator.Builder builder =
74        ComparatorProtos.NullComparator.newBuilder();
75      return builder.build().toByteArray();
76    }
77  
78    /**
79     * @param pbBytes A pb serialized {@link NullComparator} instance
80     * @return An instance of {@link NullComparator} made from <code>bytes</code>
81     * @throws DeserializationException
82     * @see #toByteArray
83     */
84    public static NullComparator parseFrom(final byte [] pbBytes)
85    throws DeserializationException {
86      try {
87        // Just parse.  Don't use what we parse since on end we are returning new NullComparator.
88        ComparatorProtos.NullComparator.parseFrom(pbBytes);
89      } catch (InvalidProtocolBufferException e) {
90        throw new DeserializationException(e);
91      }
92      return new NullComparator();
93    }
94  
95    /**
96     * @param other
97     * @return true if and only if the fields of the comparator that are serialized
98     * are equal to the corresponding fields in other.  Used for testing.
99     */
100   boolean areSerializedFieldsEqual(ByteArrayComparable other) {
101     if (other == this) return true;
102     if (!(other instanceof NullComparator)) return false;
103 
104     return super.areSerializedFieldsEqual(other);
105   }
106 }