KickJava   Java API By Example, From Geeks To Geeks.

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


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 longs. */
9 public class LongWritable implements WritableComparable {
10   private long value;
11
12   public LongWritable() {}
13
14   public LongWritable(long value) { set(value); }
15
16   /** Set the value of this LongWritable. */
17   public void set(long value) { this.value = value; }
18
19   /** Return the value of this LongWritable. */
20   public long get() { return value; }
21
22   public void readFields(DataInput in) throws IOException {
23     value = in.readLong();
24   }
25
26   public void write(DataOutput out) throws IOException {
27     out.writeLong(value);
28   }
29
30   /** Returns true iff <code>o</code> is a LongWritable with the same value. */
31   public boolean equals(Object JavaDoc o) {
32     if (!(o instanceof LongWritable))
33       return false;
34     LongWritable other = (LongWritable)o;
35     return this.value == other.value;
36   }
37
38   public int hashCode() {
39     return (int)value;
40   }
41
42   /** Compares two LongWritables. */
43   public int compareTo(Object JavaDoc o) {
44     long thisValue = this.value;
45     long thatValue = ((LongWritable)o).value;
46     return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
47   }
48
49   public String JavaDoc toString() {
50     return Long.toString(value);
51   }
52
53   /** A Comparator optimized for LongWritable. */
54   public static class Comparator extends WritableComparator {
55     public Comparator() {
56       super(LongWritable.class);
57     }
58
59     public int compare(byte[] b1, int s1, int l1,
60                        byte[] b2, int s2, int l2) {
61       long thisValue = readLong(b1, s1);
62       long thatValue = readLong(b2, s2);
63       return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
64     }
65   }
66
67 }
68
69
Popular Tags