Skip to content

Instantly share code, notes, and snippets.

@asw456
Forked from stucchio/UUIDWritable.java
Created May 7, 2014 13:20
Show Gist options
  • Save asw456/6a3359e7ca6363f01d21 to your computer and use it in GitHub Desktop.
Save asw456/6a3359e7ca6363f01d21 to your computer and use it in GitHub Desktop.
import org.apache.hadoop.io.*;
import java.util.*;
import java.io.*;
public class UUIDWritable implements WritableComparable<UUIDWritable> {
private UUID value;
public UUIDWritable(long mostSignificantBits, long leastSignificantBits) {
value = new UUID(mostSignificantBits, leastSignificantBits);
}
public UUIDWritable(String stringRep) {
value = UUID.fromString(stringRep);
}
public UUIDWritable(Text textRep) {
value = UUID.fromString(textRep.toString());
}
public UUIDWritable() {
value = UUID.randomUUID();
}
public String toString() {
return value.toString();
}
public Text toText() {
return new Text(toString());
}
public boolean equals(Object obj) {
UUIDWritable other = (UUIDWritable)obj;
return (value.getMostSignificantBits() == other.value.getMostSignificantBits()) && (value.getLeastSignificantBits() == other.value.getLeastSignificantBits());
}
public int hashCode() {
return value.hashCode();
}
//Implementation of WritableComparable
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(value.getMostSignificantBits());
out.writeLong(value.getLeastSignificantBits());
}
@Override
public void readFields(DataInput in) throws IOException {
long mostSignificantBits = in.readLong();
long leastSignificantBits = in.readLong();
value = new UUID(mostSignificantBits, leastSignificantBits);
}
public static UUIDWritable read(DataInput in) throws IOException {
UUIDWritable result = new UUIDWritable();
result.readFields(in);
return result;
}
@Override
public int compareTo(UUIDWritable w) {
return value.compareTo(w.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment