001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.io;
020    
021    import org.apache.hadoop.classification.InterfaceAudience;
022    import org.apache.hadoop.classification.InterfaceStability;
023    
024    /**
025     * Interface supported by {@link org.apache.hadoop.io.WritableComparable}
026     * types supporting ordering/permutation by a representative set of bytes.
027     */
028    @InterfaceAudience.Public
029    @InterfaceStability.Stable
030    public abstract class BinaryComparable implements Comparable<BinaryComparable> {
031    
032      /**
033       * Return n st bytes 0..n-1 from {#getBytes()} are valid.
034       */
035      public abstract int getLength();
036    
037      /**
038       * Return representative byte array for this instance.
039       */
040      public abstract byte[] getBytes();
041    
042      /**
043       * Compare bytes from {#getBytes()}.
044       * @see org.apache.hadoop.io.WritableComparator#compareBytes(byte[],int,int,byte[],int,int)
045       */
046      @Override
047      public int compareTo(BinaryComparable other) {
048        if (this == other)
049          return 0;
050        return WritableComparator.compareBytes(getBytes(), 0, getLength(),
051                 other.getBytes(), 0, other.getLength());
052      }
053    
054      /**
055       * Compare bytes from {#getBytes()} to those provided.
056       */
057      public int compareTo(byte[] other, int off, int len) {
058        return WritableComparator.compareBytes(getBytes(), 0, getLength(),
059                 other, off, len);
060      }
061    
062      /**
063       * Return true if bytes from {#getBytes()} match.
064       */
065      @Override
066      public boolean equals(Object other) {
067        if (!(other instanceof BinaryComparable))
068          return false;
069        BinaryComparable that = (BinaryComparable)other;
070        if (this.getLength() != that.getLength())
071          return false;
072        return this.compareTo(that) == 0;
073      }
074    
075      /**
076       * Return a hash of the bytes returned from {#getBytes()}.
077       * @see org.apache.hadoop.io.WritableComparator#hashBytes(byte[],int)
078       */
079      @Override
080      public int hashCode() {
081        return WritableComparator.hashBytes(getBytes(), getLength());
082      }
083    
084    }