1 2 3 4 package net.nutch.io; 5 6 import java.io.*; 7 8 9 public class IntWritable implements WritableComparable { 10 private int value; 11 12 public IntWritable() {} 13 14 public IntWritable(int value) { set(value); } 15 16 17 public void set(int value) { this.value = value; } 18 19 20 public int get() { return value; } 21 22 public void readFields(DataInput in) throws IOException { 23 value = in.readInt(); 24 } 25 26 public void write(DataOutput out) throws IOException { 27 out.writeInt(value); 28 } 29 30 31 public boolean equals(Object o) { 32 if (!(o instanceof IntWritable)) 33 return false; 34 IntWritable other = (IntWritable)o; 35 return this.value == other.value; 36 } 37 38 public int hashCode() { 39 return (int)value; 40 } 41 42 43 public int compareTo(Object o) { 44 int thisValue = this.value; 45 int thatValue = ((IntWritable)o).value; 46 return thisValue - thatValue; 47 } 48 49 public String toString() { 50 return Integer.toString(value); 51 } 52 53 54 public static class Comparator extends WritableComparator { 55 public Comparator() { 56 super(IntWritable.class); 57 } 58 59 public int compare(byte[] b1, int s1, int l1, 60 byte[] b2, int s2, int l2) { 61 int thisValue = readInt(b1, s1); 62 int thatValue = readInt(b2, s2); 63 return thisValue - thatValue; 64 } 65 } 66 67 } 68 69 | Popular Tags |