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    @Override
66    public byte [] toByteArray() {
67      ComparatorProtos.NullComparator.Builder builder =
68        ComparatorProtos.NullComparator.newBuilder();
69      return builder.build().toByteArray();
70    }
71  
72    /**
73     * @param pbBytes A pb serialized {@link NullComparator} instance
74     * @return An instance of {@link NullComparator} made from <code>bytes</code>
75     * @throws DeserializationException
76     * @see #toByteArray
77     */
78    public static NullComparator parseFrom(final byte [] pbBytes)
79    throws DeserializationException {
80      try {
81        // Just parse.  Don't use what we parse since on end we are returning new NullComparator.
82        ComparatorProtos.NullComparator.parseFrom(pbBytes);
83      } catch (InvalidProtocolBufferException e) {
84        throw new DeserializationException(e);
85      }
86      return new NullComparator();
87    }
88  
89    /**
90     * @param other
91     * @return true if and only if the fields of the comparator that are serialized
92     * are equal to the corresponding fields in other.  Used for testing.
93     */
94    @Override
95    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
96      if (other == this) return true;
97      if (!(other instanceof NullComparator)) return false;
98  
99      return super.areSerializedFieldsEqual(other);
100   }
101 }