View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.filter;
21  
22  import org.apache.hadoop.hbase.util.Bytes;
23  import org.apache.hadoop.io.Writable;
24  
25  import java.io.DataInput;
26  import java.io.DataOutput;
27  import java.io.IOException;
28  
29  /** Base class, combines Comparable<byte []> and Writable. */
30  public abstract class WritableByteArrayComparable implements Writable, Comparable<byte[]> {
31  
32    byte[] value;
33  
34    /**
35     * Nullary constructor, for Writable
36     */
37    public WritableByteArrayComparable() { }
38  
39    /**
40     * Constructor.
41     * @param value the value to compare against
42     */
43    public WritableByteArrayComparable(byte [] value) {
44      this.value = value;
45    }
46  
47    public byte[] getValue() {
48      return value;
49    }
50  
51    @Override
52    public void readFields(DataInput in) throws IOException {
53      value = Bytes.readByteArray(in);
54    }
55  
56    @Override
57    public void write(DataOutput out) throws IOException {
58      Bytes.writeByteArray(out, value);
59    }
60  
61    @Override
62    public int compareTo(byte [] value) {
63      return compareTo(value, 0, value.length);
64    }
65  
66    /**
67     * Special compareTo method for subclasses, to avoid
68     * copying byte[] unnecessarily.
69     * @param value byte[] to compare
70     * @param offset offset into value
71     * @param length number of bytes to compare
72     * @return a negative integer, zero, or a positive integer as this object
73     *         is less than, equal to, or greater than the specified object.
74     */
75    public abstract int compareTo(byte [] value, int offset, int length);
76  }