KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > io > IntWritable


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.io;
5
6 import java.io.*;
7
8 /** A WritableComparable for ints. */
9 public class IntWritable implements WritableComparable {
10   private int value;
11
12   public IntWritable() {}
13
14   public IntWritable(int value) { set(value); }
15
16   /** Set the value of this IntWritable. */
17   public void set(int value) { this.value = value; }
18
19   /** Return the value of this IntWritable. */
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   /** Returns true iff <code>o</code> is a IntWritable with the same value. */
31   public boolean equals(Object JavaDoc 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   /** Compares two IntWritables. */
43   public int compareTo(Object JavaDoc o) {
44     int thisValue = this.value;
45     int thatValue = ((IntWritable)o).value;
46     return thisValue - thatValue;
47   }
48
49   public String JavaDoc toString() {
50     return Integer.toString(value);
51   }
52
53   /** A Comparator optimized for IntWritable. */
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