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 019package org.apache.hadoop.io; 020 021import org.apache.hadoop.classification.InterfaceAudience; 022import org.apache.hadoop.classification.InterfaceStability; 023 024/** 025 * A {@link Writable} which is also {@link Comparable}. 026 * 027 * <p><code>WritableComparable</code>s can be compared to each other, typically 028 * via <code>Comparator</code>s. Any type which is to be used as a 029 * <code>key</code> in the Hadoop Map-Reduce framework should implement this 030 * interface.</p> 031 * 032 * <p>Note that <code>hashCode()</code> is frequently used in Hadoop to partition 033 * keys. It's important that your implementation of hashCode() returns the same 034 * result across different instances of the JVM. Note also that the default 035 * <code>hashCode()</code> implementation in <code>Object</code> does <b>not</b> 036 * satisfy this property.</p> 037 * 038 * <p>Example:</p> 039 * <p><blockquote><pre> 040 * public class MyWritableComparable implements WritableComparable<MyWritableComparable> { 041 * // Some data 042 * private int counter; 043 * private long timestamp; 044 * 045 * public void write(DataOutput out) throws IOException { 046 * out.writeInt(counter); 047 * out.writeLong(timestamp); 048 * } 049 * 050 * public void readFields(DataInput in) throws IOException { 051 * counter = in.readInt(); 052 * timestamp = in.readLong(); 053 * } 054 * 055 * public int compareTo(MyWritableComparable o) { 056 * int thisValue = this.value; 057 * int thatValue = o.value; 058 * return (thisValue < thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); 059 * } 060 * 061 * public int hashCode() { 062 * final int prime = 31; 063 * int result = 1; 064 * result = prime * result + counter; 065 * result = prime * result + (int) (timestamp ^ (timestamp >>> 32)); 066 * return result 067 * } 068 * } 069 * </pre></blockquote></p> 070 */ 071@InterfaceAudience.Public 072@InterfaceStability.Stable 073public interface WritableComparable<T> extends Writable, Comparable<T> { 074}