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 org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
26  
27  import com.google.protobuf.InvalidProtocolBufferException;
28  
29  /**
30   * A binary comparator which lexicographically compares against the specified
31   * byte array using {@link org.apache.hadoop.hbase.util.Bytes#compareTo(byte[], byte[])}.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Stable
35  public class NullComparator extends ByteArrayComparable {
36  
37    public NullComparator() {
38      super(new byte[0]);
39    }
40  
41    @Override
42    public int compareTo(byte[] value) {
43      return value != null ? 1 : 0;
44    }
45  
46    @Override
47    @edu.umd.cs.findbugs.annotations.SuppressWarnings (value="EQ_UNUSUAL", justification="")
48    public boolean equals(Object obj) {
49      return obj == null;
50    }
51  
52    @Override
53    public int hashCode() {
54      return 0;
55    }
56  
57    @Override
58    public int compareTo(byte[] value, int offset, int length) {
59      return compareTo(value);
60    }
61  
62    /**
63     * @return The comparator serialized using pb
64     */
65    public byte [] toByteArray() {
66      ComparatorProtos.NullComparator.Builder builder =
67        ComparatorProtos.NullComparator.newBuilder();
68      return builder.build().toByteArray();
69    }
70  
71    /**
72     * @param pbBytes A pb serialized {@link NullComparator} instance
73     * @return An instance of {@link NullComparator} made from <code>bytes</code>
74     * @throws DeserializationException
75     * @see #toByteArray
76     */
77    public static NullComparator parseFrom(final byte [] pbBytes)
78    throws DeserializationException {
79      try {
80        // Just parse.  Don't use what we parse since on end we are returning new NullComparator.
81        ComparatorProtos.NullComparator.parseFrom(pbBytes);
82      } catch (InvalidProtocolBufferException e) {
83        throw new DeserializationException(e);
84      }
85      return new NullComparator();
86    }
87  
88    /**
89     * @param other
90     * @return true if and only if the fields of the comparator that are serialized
91     * are equal to the corresponding fields in other.  Used for testing.
92     */
93    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
94      if (other == this) return true;
95      if (!(other instanceof NullComparator)) return false;
96  
97      return super.areSerializedFieldsEqual(other);
98    }
99  }