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 com.google.protobuf.InvalidProtocolBufferException;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.exceptions.DeserializationException;
26  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
27  import org.apache.hadoop.hbase.protobuf.generated.ComparatorProtos;
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    public boolean equals(Object obj) {
48      return obj == null;
49    }
50  
51    @Override
52    public int hashCode() {
53      return 0;
54    }
55  
56    @Override
57    public int compareTo(byte[] value, int offset, int length) {
58      throw new UnsupportedOperationException();
59    }
60  
61    /**
62     * @return The comparator serialized using pb
63     */
64    public byte [] toByteArray() {
65      ComparatorProtos.NullComparator.Builder builder =
66        ComparatorProtos.NullComparator.newBuilder();
67      return builder.build().toByteArray();
68    }
69  
70    /**
71     * @param pbBytes A pb serialized {@link NullComparator} instance
72     * @return An instance of {@link NullComparator} made from <code>bytes</code>
73     * @throws DeserializationException
74     * @see #toByteArray
75     */
76    public static NullComparator parseFrom(final byte [] pbBytes)
77    throws DeserializationException {
78      try {
79        @SuppressWarnings("unused")
80        ComparatorProtos.NullComparator proto = ComparatorProtos.NullComparator.parseFrom(pbBytes);
81      } catch (InvalidProtocolBufferException e) {
82        throw new DeserializationException(e);
83      }
84      return new NullComparator();
85    }
86  
87    /**
88     * @param other
89     * @return true if and only if the fields of the comparator that are serialized
90     * are equal to the corresponding fields in other.  Used for testing.
91     */
92    boolean areSerializedFieldsEqual(ByteArrayComparable other) {
93      if (other == this) return true;
94      if (!(other instanceof NullComparator)) return false;
95  
96      return super.areSerializedFieldsEqual(other);
97    }
98  }