KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
8
9 /** A Comparator for {@link WritableComparable}s.
10  *
11  * <p>This base implemenation uses the natural ordering. To define alternate
12  * orderings, override {@link #compare(WritableComparable,WritableComparable)}.
13  *
14  * <p>One may optimize compare-intensive operations by overriding
15  * {@link #compare(byte[],int,int,byte[],int,int)}. Static utility methods are
16  * provided to assist in optimized implementations of this method.
17  */

18 public class WritableComparator {
19   private DataInputBuffer buffer = new DataInputBuffer();
20
21   private Class JavaDoc keyClass;
22   private WritableComparable key1;
23   private WritableComparable key2;
24
25   /** Construct for a {@link WritableComparable} implementation. */
26   public WritableComparator(Class JavaDoc keyClass) {
27     this.keyClass = keyClass;
28     this.key1 = newKey();
29     this.key2 = newKey();
30   }
31
32   /** Returns the WritableComparable implementation class. */
33   public Class JavaDoc getKeyClass() { return keyClass; }
34
35   /** Construct a new {@link WritableComparable} instance. */
36   public WritableComparable newKey() {
37     try {
38       return (WritableComparable)keyClass.newInstance();
39     } catch (InstantiationException JavaDoc e) {
40       throw new RuntimeException JavaDoc(e);
41     } catch (IllegalAccessException JavaDoc e) {
42       throw new RuntimeException JavaDoc(e);
43     }
44   }
45
46   /** Optimization hook. Override this to make SequenceFile.Sorter's scream.
47    *
48    * <p>The default implementation reads the data into two {@link
49    * WritableComparable}s (using {@link
50    * Writable#readFields(DataInput)}, then calls {@link
51    * #compare(WritableComparable,WritableComparable)}.
52    */

53   public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
54     try {
55       buffer.reset(b1, s1, l1); // parse key1
56
key1.readFields(buffer);
57       
58       buffer.reset(b2, s2, l2); // parse key2
59
key2.readFields(buffer);
60       
61     } catch (IOException e) {
62       throw new RuntimeException JavaDoc(e);
63     }
64     
65     return compare(key1, key2); // compare them
66
}
67
68   /** Compare two WritableComparables.
69    *
70    * <p> The default implementation uses the natural ordering, calling {@link
71    * Comparable#compareTo(Object)}. */

72   public int compare(WritableComparable a, WritableComparable b) {
73     return a.compareTo(b);
74   }
75
76   /** Lexicographic order of binary data. */
77   public static int compareBytes(byte[] b1, int s1, int l1,
78                                  byte[] b2, int s2, int l2) {
79     int end1 = s1 + l1;
80     int end2 = s2 + l2;
81     for (int i = s1, j = s2; i < end1 && j < end2; i++, j++) {
82       int a = (b1[i] & 0xff);
83       int b = (b2[j] & 0xff);
84       if (a != b) {
85         return a - b;
86       }
87     }
88     return l1 - l2;
89   }
90
91   /** Parse an unsigned short from a byte array. */
92   public static int readUnsignedShort(byte[] bytes, int start) {
93     return (((bytes[start] & 0xff) << 8) +
94             ((bytes[start+1] & 0xff)));
95   }
96
97   /** Parse an integer from a byte array. */
98   public static int readInt(byte[] bytes, int start) {
99     return (((bytes[start ] & 0xff) << 24) +
100             ((bytes[start+1] & 0xff) << 16) +
101             ((bytes[start+2] & 0xff) << 8) +
102             ((bytes[start+3] & 0xff)));
103
104   }
105
106   /** Parse a float from a byte array. */
107   public static float readFloat(byte[] bytes, int start) {
108     return Float.intBitsToFloat(readInt(bytes, start));
109   }
110
111   /** Parse a long from a byte array. */
112   public static long readLong(byte[] bytes, int start) {
113     return ((long)(readInt(bytes, start)) << 32) +
114       (readInt(bytes, start+4) & 0xFFFFFFFFL);
115   }
116
117 }
118
Popular Tags