HADOOP-7547. Add generic type in WritableComparable subclasses. Contributed by Uma Maheswara Rao G

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1162008 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tsz-wo Sze 2011-08-26 06:58:52 +00:00
parent 23e7fc4f46
commit 56dd8ba0b8
21 changed files with 127 additions and 70 deletions

View File

@ -344,6 +344,9 @@ Release 0.23.0 - Unreleased
HADOOP-7561. Make test-patch only run tests for changed modules. (tomwhite) HADOOP-7561. Make test-patch only run tests for changed modules. (tomwhite)
HADOOP-7547. Add generic type in WritableComparable subclasses.
(Uma Maheswara Rao G via szetszwo)
OPTIMIZATIONS OPTIMIZATIONS
HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole

View File

@ -43,6 +43,7 @@ public abstract class BinaryComparable implements Comparable<BinaryComparable> {
* Compare bytes from {#getBytes()}. * Compare bytes from {#getBytes()}.
* @see org.apache.hadoop.io.WritableComparator#compareBytes(byte[],int,int,byte[],int,int) * @see org.apache.hadoop.io.WritableComparator#compareBytes(byte[],int,int,byte[],int,int)
*/ */
@Override
public int compareTo(BinaryComparable other) { public int compareTo(BinaryComparable other) {
if (this == other) if (this == other)
return 0; return 0;
@ -61,6 +62,7 @@ public int compareTo(byte[] other, int off, int len) {
/** /**
* Return true if bytes from {#getBytes()} match. * Return true if bytes from {#getBytes()} match.
*/ */
@Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (!(other instanceof BinaryComparable)) if (!(other instanceof BinaryComparable))
return false; return false;
@ -74,6 +76,7 @@ public boolean equals(Object other) {
* Return a hash of the bytes returned from {#getBytes()}. * Return a hash of the bytes returned from {#getBytes()}.
* @see org.apache.hadoop.io.WritableComparator#hashBytes(byte[],int) * @see org.apache.hadoop.io.WritableComparator#hashBytes(byte[],int)
*/ */
@Override
public int hashCode() { public int hashCode() {
return WritableComparator.hashBytes(getBytes(), getLength()); return WritableComparator.hashBytes(getBytes(), getLength());
} }

View File

@ -28,7 +28,7 @@
*/ */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class BooleanWritable implements WritableComparable { public class BooleanWritable implements WritableComparable<BooleanWritable> {
private boolean value; private boolean value;
/** /**
@ -69,6 +69,7 @@ public void write(DataOutput out) throws IOException {
/** /**
*/ */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof BooleanWritable)) { if (!(o instanceof BooleanWritable)) {
return false; return false;
@ -77,6 +78,7 @@ public boolean equals(Object o) {
return this.value == other.value; return this.value == other.value;
} }
@Override
public int hashCode() { public int hashCode() {
return value ? 0 : 1; return value ? 0 : 1;
} }
@ -85,12 +87,14 @@ public int hashCode() {
/** /**
*/ */
public int compareTo(Object o) { @Override
public int compareTo(BooleanWritable o) {
boolean a = this.value; boolean a = this.value;
boolean b = ((BooleanWritable) o).value; boolean b = o.value;
return ((a == b) ? 0 : (a == false) ? -1 : 1); return ((a == b) ? 0 : (a == false) ? -1 : 1);
} }
@Override
public String toString() { public String toString() {
return Boolean.toString(get()); return Boolean.toString(get());
} }
@ -103,6 +107,7 @@ public Comparator() {
super(BooleanWritable.class); super(BooleanWritable.class);
} }
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
return compareBytes(b1, s1, l1, b2, s2, l2); return compareBytes(b1, s1, l1, b2, s2, l2);

View File

@ -26,7 +26,7 @@
/** A WritableComparable for a single byte. */ /** A WritableComparable for a single byte. */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class ByteWritable implements WritableComparable { public class ByteWritable implements WritableComparable<ByteWritable> {
private byte value; private byte value;
public ByteWritable() {} public ByteWritable() {}
@ -48,6 +48,7 @@ public void write(DataOutput out) throws IOException {
} }
/** Returns true iff <code>o</code> is a ByteWritable with the same value. */ /** Returns true iff <code>o</code> is a ByteWritable with the same value. */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof ByteWritable)) { if (!(o instanceof ByteWritable)) {
return false; return false;
@ -56,17 +57,20 @@ public boolean equals(Object o) {
return this.value == other.value; return this.value == other.value;
} }
@Override
public int hashCode() { public int hashCode() {
return (int)value; return (int)value;
} }
/** Compares two ByteWritables. */ /** Compares two ByteWritables. */
public int compareTo(Object o) { @Override
public int compareTo(ByteWritable o) {
int thisValue = this.value; int thisValue = this.value;
int thatValue = ((ByteWritable)o).value; int thatValue = o.value;
return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1)); return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
} }
@Override
public String toString() { public String toString() {
return Byte.toString(value); return Byte.toString(value);
} }
@ -77,6 +81,7 @@ public Comparator() {
super(ByteWritable.class); super(ByteWritable.class);
} }
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
byte thisValue = b1[s1]; byte thisValue = b1[s1];

View File

@ -183,6 +183,7 @@ public void write(DataOutput out) throws IOException {
out.write(bytes, 0, size); out.write(bytes, 0, size);
} }
@Override
public int hashCode() { public int hashCode() {
return super.hashCode(); return super.hashCode();
} }
@ -190,6 +191,7 @@ public int hashCode() {
/** /**
* Are the two byte sequences equal? * Are the two byte sequences equal?
*/ */
@Override
public boolean equals(Object right_obj) { public boolean equals(Object right_obj) {
if (right_obj instanceof BytesWritable) if (right_obj instanceof BytesWritable)
return super.equals(right_obj); return super.equals(right_obj);
@ -199,6 +201,7 @@ public boolean equals(Object right_obj) {
/** /**
* Generate the stream of bytes as hex pairs separated by ' '. * Generate the stream of bytes as hex pairs separated by ' '.
*/ */
@Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(3*size); StringBuilder sb = new StringBuilder(3*size);
for (int idx = 0; idx < size; idx++) { for (int idx = 0; idx < size; idx++) {
@ -225,6 +228,7 @@ public Comparator() {
/** /**
* Compare the buffers in serialized form. * Compare the buffers in serialized form.
*/ */
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
return compareBytes(b1, s1+LENGTH_BYTES, l1-LENGTH_BYTES, return compareBytes(b1, s1+LENGTH_BYTES, l1-LENGTH_BYTES,

View File

@ -30,7 +30,7 @@
*/ */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class DoubleWritable implements WritableComparable { public class DoubleWritable implements WritableComparable<DoubleWritable> {
private double value = 0.0; private double value = 0.0;
@ -57,6 +57,7 @@ public void write(DataOutput out) throws IOException {
/** /**
* Returns true iff <code>o</code> is a DoubleWritable with the same value. * Returns true iff <code>o</code> is a DoubleWritable with the same value.
*/ */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof DoubleWritable)) { if (!(o instanceof DoubleWritable)) {
return false; return false;
@ -65,15 +66,17 @@ public boolean equals(Object o) {
return this.value == other.value; return this.value == other.value;
} }
@Override
public int hashCode() { public int hashCode() {
return (int)Double.doubleToLongBits(value); return (int)Double.doubleToLongBits(value);
} }
public int compareTo(Object o) { @Override
DoubleWritable other = (DoubleWritable)o; public int compareTo(DoubleWritable o) {
return (value < other.value ? -1 : (value == other.value ? 0 : 1)); return (value < o.value ? -1 : (value == o.value ? 0 : 1));
} }
@Override
public String toString() { public String toString() {
return Double.toString(value); return Double.toString(value);
} }
@ -84,6 +87,7 @@ public Comparator() {
super(DoubleWritable.class); super(DoubleWritable.class);
} }
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
double thisValue = readDouble(b1, s1); double thisValue = readDouble(b1, s1);

View File

@ -26,7 +26,7 @@
/** A WritableComparable for floats. */ /** A WritableComparable for floats. */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class FloatWritable implements WritableComparable { public class FloatWritable implements WritableComparable<FloatWritable> {
private float value; private float value;
public FloatWritable() {} public FloatWritable() {}
@ -48,6 +48,7 @@ public void write(DataOutput out) throws IOException {
} }
/** Returns true iff <code>o</code> is a FloatWritable with the same value. */ /** Returns true iff <code>o</code> is a FloatWritable with the same value. */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof FloatWritable)) if (!(o instanceof FloatWritable))
return false; return false;
@ -55,17 +56,20 @@ public boolean equals(Object o) {
return this.value == other.value; return this.value == other.value;
} }
@Override
public int hashCode() { public int hashCode() {
return Float.floatToIntBits(value); return Float.floatToIntBits(value);
} }
/** Compares two FloatWritables. */ /** Compares two FloatWritables. */
public int compareTo(Object o) { @Override
public int compareTo(FloatWritable o) {
float thisValue = this.value; float thisValue = this.value;
float thatValue = ((FloatWritable)o).value; float thatValue = o.value;
return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
} }
@Override
public String toString() { public String toString() {
return Float.toString(value); return Float.toString(value);
} }
@ -75,7 +79,7 @@ public static class Comparator extends WritableComparator {
public Comparator() { public Comparator() {
super(FloatWritable.class); super(FloatWritable.class);
} }
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
float thisValue = readFloat(b1, s1); float thisValue = readFloat(b1, s1);

View File

@ -18,7 +18,10 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
@ -26,7 +29,7 @@
/** A WritableComparable for ints. */ /** A WritableComparable for ints. */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class IntWritable implements WritableComparable { public class IntWritable implements WritableComparable<IntWritable> {
private int value; private int value;
public IntWritable() {} public IntWritable() {}
@ -48,6 +51,7 @@ public void write(DataOutput out) throws IOException {
} }
/** Returns true iff <code>o</code> is a IntWritable with the same value. */ /** Returns true iff <code>o</code> is a IntWritable with the same value. */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof IntWritable)) if (!(o instanceof IntWritable))
return false; return false;
@ -55,17 +59,20 @@ public boolean equals(Object o) {
return this.value == other.value; return this.value == other.value;
} }
@Override
public int hashCode() { public int hashCode() {
return value; return value;
} }
/** Compares two IntWritables. */ /** Compares two IntWritables. */
public int compareTo(Object o) { @Override
public int compareTo(IntWritable o) {
int thisValue = this.value; int thisValue = this.value;
int thatValue = ((IntWritable)o).value; int thatValue = o.value;
return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
} }
@Override
public String toString() { public String toString() {
return Integer.toString(value); return Integer.toString(value);
} }
@ -76,6 +83,7 @@ public Comparator() {
super(IntWritable.class); super(IntWritable.class);
} }
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
int thisValue = readInt(b1, s1); int thisValue = readInt(b1, s1);

View File

@ -18,7 +18,10 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
@ -26,7 +29,7 @@
/** A WritableComparable for longs. */ /** A WritableComparable for longs. */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class LongWritable implements WritableComparable { public class LongWritable implements WritableComparable<LongWritable> {
private long value; private long value;
public LongWritable() {} public LongWritable() {}
@ -60,9 +63,9 @@ public int hashCode() {
} }
/** Compares two LongWritables. */ /** Compares two LongWritables. */
public int compareTo(Object o) { public int compareTo(LongWritable o) {
long thisValue = this.value; long thisValue = this.value;
long thatValue = ((LongWritable)o).value; long thatValue = o.value;
return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
} }
@ -86,6 +89,8 @@ public int compare(byte[] b1, int s1, int l1,
/** A decreasing Comparator optimized for LongWritable. */ /** A decreasing Comparator optimized for LongWritable. */
public static class DecreasingComparator extends Comparator { public static class DecreasingComparator extends Comparator {
@Override
public int compare(WritableComparable a, WritableComparable b) { public int compare(WritableComparable a, WritableComparable b) {
return -super.compare(a, b); return -super.compare(a, b);
} }

View File

@ -18,24 +18,23 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.io.*;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.util.Options;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.conf.*; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.util.Options;
import org.apache.hadoop.util.Progressable; import org.apache.hadoop.util.Progressable;
import org.apache.hadoop.util.ReflectionUtils; import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.SequenceFile.Reader;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.DefaultCodec;
/** A file-based map from keys to values. /** A file-based map from keys to values.
* *

View File

@ -26,7 +26,7 @@
/** Singleton Writable with no data. */ /** Singleton Writable with no data. */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class NullWritable implements WritableComparable { public class NullWritable implements WritableComparable<NullWritable> {
private static final NullWritable THIS = new NullWritable(); private static final NullWritable THIS = new NullWritable();
@ -39,12 +39,11 @@ public String toString() {
return "(null)"; return "(null)";
} }
@Override
public int hashCode() { return 0; } public int hashCode() { return 0; }
public int compareTo(Object other) {
if (!(other instanceof NullWritable)) { @Override
throw new ClassCastException("can't compare " + other.getClass().getName() public int compareTo(NullWritable other) {
+ " to NullWritable");
}
return 0; return 0;
} }
public boolean equals(Object other) { return other instanceof NullWritable; } public boolean equals(Object other) { return other instanceof NullWritable; }
@ -60,6 +59,7 @@ public Comparator() {
/** /**
* Compare the buffers in serialized form. * Compare the buffers in serialized form.
*/ */
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
assert 0 == l1; assert 0 == l1;

View File

@ -18,12 +18,14 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.io.*;
import org.apache.hadoop.fs.*; import java.io.IOException;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.conf.*; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/** A file-based set of keys. */ /** A file-based set of keys. */
@InterfaceAudience.Public @InterfaceAudience.Public

View File

@ -265,6 +265,7 @@ private void setCapacity(int len, boolean keepData) {
* Convert text back to string * Convert text back to string
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
@Override
public String toString() { public String toString() {
try { try {
return decode(bytes, 0, length); return decode(bytes, 0, length);
@ -305,6 +306,7 @@ public boolean equals(Object o) {
return false; return false;
} }
@Override
public int hashCode() { public int hashCode() {
return super.hashCode(); return super.hashCode();
} }

View File

@ -36,7 +36,7 @@
@Deprecated @Deprecated
@InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"}) @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
@InterfaceStability.Stable @InterfaceStability.Stable
public class UTF8 implements WritableComparable { public class UTF8 implements WritableComparable<UTF8> {
private static final Log LOG= LogFactory.getLog(UTF8.class); private static final Log LOG= LogFactory.getLog(UTF8.class);
private static final DataInputBuffer IBUF = new DataInputBuffer(); private static final DataInputBuffer IBUF = new DataInputBuffer();
@ -129,13 +129,14 @@ public void write(DataOutput out) throws IOException {
} }
/** Compare two UTF8s. */ /** Compare two UTF8s. */
public int compareTo(Object o) { @Override
UTF8 that = (UTF8)o; public int compareTo(UTF8 o) {
return WritableComparator.compareBytes(bytes, 0, length, return WritableComparator.compareBytes(bytes, 0, length,
that.bytes, 0, that.length); o.bytes, 0, o.length);
} }
/** Convert to a String. */ /** Convert to a String. */
@Override
public String toString() { public String toString() {
StringBuilder buffer = new StringBuilder(length); StringBuilder buffer = new StringBuilder(length);
try { try {
@ -150,6 +151,7 @@ public String toString() {
} }
/** Returns true iff <code>o</code> is a UTF8 with the same contents. */ /** Returns true iff <code>o</code> is a UTF8 with the same contents. */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof UTF8)) if (!(o instanceof UTF8))
return false; return false;
@ -161,6 +163,7 @@ public boolean equals(Object o) {
that.bytes, 0, that.length) == 0; that.bytes, 0, that.length) == 0;
} }
@Override
public int hashCode() { public int hashCode() {
return WritableComparator.hashBytes(bytes, length); return WritableComparator.hashBytes(bytes, length);
} }
@ -171,6 +174,7 @@ public Comparator() {
super(UTF8.class); super(UTF8.class);
} }
@Override
public int compare(byte[] b1, int s1, int l1, public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) { byte[] b2, int s2, int l2) {
int n1 = readUnsignedShort(b1, s1); int n1 = readUnsignedShort(b1, s1);

View File

@ -30,7 +30,7 @@
*/ */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class VIntWritable implements WritableComparable { public class VIntWritable implements WritableComparable<VIntWritable> {
private int value; private int value;
public VIntWritable() {} public VIntWritable() {}
@ -52,6 +52,7 @@ public void write(DataOutput out) throws IOException {
} }
/** Returns true iff <code>o</code> is a VIntWritable with the same value. */ /** Returns true iff <code>o</code> is a VIntWritable with the same value. */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof VIntWritable)) if (!(o instanceof VIntWritable))
return false; return false;
@ -59,17 +60,20 @@ public boolean equals(Object o) {
return this.value == other.value; return this.value == other.value;
} }
@Override
public int hashCode() { public int hashCode() {
return value; return value;
} }
/** Compares two VIntWritables. */ /** Compares two VIntWritables. */
public int compareTo(Object o) { @Override
public int compareTo(VIntWritable o) {
int thisValue = this.value; int thisValue = this.value;
int thatValue = ((VIntWritable)o).value; int thatValue = o.value;
return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1)); return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
} }
@Override
public String toString() { public String toString() {
return Integer.toString(value); return Integer.toString(value);
} }

View File

@ -30,7 +30,7 @@
*/ */
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class VLongWritable implements WritableComparable { public class VLongWritable implements WritableComparable<VLongWritable> {
private long value; private long value;
public VLongWritable() {} public VLongWritable() {}
@ -52,6 +52,7 @@ public void write(DataOutput out) throws IOException {
} }
/** Returns true iff <code>o</code> is a VLongWritable with the same value. */ /** Returns true iff <code>o</code> is a VLongWritable with the same value. */
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof VLongWritable)) if (!(o instanceof VLongWritable))
return false; return false;
@ -59,17 +60,20 @@ public boolean equals(Object o) {
return this.value == other.value; return this.value == other.value;
} }
@Override
public int hashCode() { public int hashCode() {
return (int)value; return (int)value;
} }
/** Compares two VLongWritables. */ /** Compares two VLongWritables. */
public int compareTo(Object o) { @Override
public int compareTo(VLongWritable o) {
long thisValue = this.value; long thisValue = this.value;
long thatValue = ((VLongWritable)o).value; long thatValue = o.value;
return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1)); return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
} }
@Override
public String toString() { public String toString() {
return Long.toString(value); return Long.toString(value);
} }

View File

@ -37,7 +37,7 @@
* *
* <p>Example:</p> * <p>Example:</p>
* <p><blockquote><pre> * <p><blockquote><pre>
* public class MyWritableComparable implements WritableComparable { * public class MyWritableComparable implements WritableComparable<MyWritableComparable> {
* // Some data * // Some data
* private int counter; * private int counter;
* private long timestamp; * private long timestamp;
@ -54,7 +54,7 @@
* *
* public int compareTo(MyWritableComparable o) { * public int compareTo(MyWritableComparable o) {
* int thisValue = this.value; * int thisValue = this.value;
* int thatValue = ((IntWritable)o).value; * int thatValue = o.value;
* return (thisValue &lt; thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); * return (thisValue &lt; thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
* } * }
* *

View File

@ -167,7 +167,7 @@ public void readFields(DataInput in) throws IOException {
} }
// Comparable // Comparable
@Override
public int compareTo(Key other) { public int compareTo(Key other) {
int result = this.bytes.length - other.getBytes().length; int result = this.bytes.length - other.getBytes().length;
for (int i = 0; result == 0 && i < bytes.length; i++) { for (int i = 0; result == 0 && i < bytes.length; i++) {

View File

@ -18,10 +18,13 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.util.*; import java.io.DataInput;
import java.io.*; import java.io.DataOutput;
import java.io.IOException;
import java.util.Random;
public class RandomDatum implements WritableComparable {
public class RandomDatum implements WritableComparable<RandomDatum> {
private int length; private int length;
private byte[] data; private byte[] data;
@ -49,20 +52,22 @@ public void readFields(DataInput in) throws IOException {
in.readFully(data, 0, length); in.readFully(data, 0, length);
} }
public int compareTo(Object o) { @Override
RandomDatum that = (RandomDatum)o; public int compareTo(RandomDatum o) {
return WritableComparator.compareBytes(this.data, 0, this.length, return WritableComparator.compareBytes(this.data, 0, this.length,
that.data, 0, that.length); o.data, 0, o.length);
} }
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
return compareTo(o) == 0; return compareTo((RandomDatum)o) == 0;
} }
private static final char[] HEX_DIGITS = private static final char[] HEX_DIGITS =
{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
/** Returns a string representation of this object. */ /** Returns a string representation of this object. */
@Override
public String toString() { public String toString() {
StringBuilder buf = new StringBuilder(length*2); StringBuilder buf = new StringBuilder(length*2);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {

View File

@ -21,14 +21,10 @@
import junit.framework.TestCase; import junit.framework.TestCase;
import java.nio.charset.MalformedInputException; import java.nio.charset.MalformedInputException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.util.*;
import java.util.Arrays; import java.util.Arrays;
/** Unit tests for NonUTF8. */ /** Unit tests for NonUTF8. */
public class TestTextNonUTF8 extends TestCase { public class TestTextNonUTF8 extends TestCase {
private static final Log LOG= LogFactory.getLog(TestTextNonUTF8.class);
public void testNonUTF8() throws Exception{ public void testNonUTF8() throws Exception{
// this is a non UTF8 byte array // this is a non UTF8 byte array

View File

@ -18,12 +18,12 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.io.*; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Random; import java.util.Random;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.ReflectionUtils;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -97,7 +97,7 @@ public void testAddName() throws Exception {
public void testBadName() throws Exception { public void testBadName() throws Exception {
Configuration conf = new Configuration(); Configuration conf = new Configuration();
try { try {
Class<?> test = WritableName.getClass("unknown_junk",conf); WritableName.getClass("unknown_junk",conf);
assertTrue(false); assertTrue(false);
} catch(IOException e) { } catch(IOException e) {
assertTrue(e.getMessage().matches(".*unknown_junk.*")); assertTrue(e.getMessage().matches(".*unknown_junk.*"));