[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.io; |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * Interface supported by {@link org.apache.hadoop.io.WritableComparable} |
---|
| 23 | * types supporting ordering/permutation by a representative set of bytes. |
---|
| 24 | */ |
---|
| 25 | public abstract class BinaryComparable implements Comparable<BinaryComparable> { |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Return n st bytes 0..n-1 from {#getBytes()} are valid. |
---|
| 29 | */ |
---|
| 30 | public abstract int getLength(); |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Return representative byte array for this instance. |
---|
| 34 | */ |
---|
| 35 | public abstract byte[] getBytes(); |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Compare bytes from {#getBytes()}. |
---|
| 39 | * @see org.apache.hadoop.io.WritableComparator#compareBytes(byte[],int,int,byte[],int,int) |
---|
| 40 | */ |
---|
| 41 | public int compareTo(BinaryComparable other) { |
---|
| 42 | if (this == other) |
---|
| 43 | return 0; |
---|
| 44 | return WritableComparator.compareBytes(getBytes(), 0, getLength(), |
---|
| 45 | other.getBytes(), 0, other.getLength()); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Compare bytes from {#getBytes()} to those provided. |
---|
| 50 | */ |
---|
| 51 | public int compareTo(byte[] other, int off, int len) { |
---|
| 52 | return WritableComparator.compareBytes(getBytes(), 0, getLength(), |
---|
| 53 | other, off, len); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Return true if bytes from {#getBytes()} match. |
---|
| 58 | */ |
---|
| 59 | public boolean equals(Object other) { |
---|
| 60 | if (!(other instanceof BinaryComparable)) |
---|
| 61 | return false; |
---|
| 62 | BinaryComparable that = (BinaryComparable)other; |
---|
| 63 | if (this.getLength() != that.getLength()) |
---|
| 64 | return false; |
---|
| 65 | return this.compareTo(that) == 0; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Return a hash of the bytes returned from {#getBytes()}. |
---|
| 70 | * @see org.apache.hadoop.io.WritableComparator#hashBytes(byte[],int) |
---|
| 71 | */ |
---|
| 72 | public int hashCode() { |
---|
| 73 | return WritableComparator.hashBytes(getBytes(), getLength()); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | } |
---|