KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
9  * A WritableComparable for booleans.
10  */

11 public class BooleanWritable implements WritableComparable {
12     private boolean value;
13
14     /**
15      */

16     public BooleanWritable() {};
17
18     /**
19      */

20     public BooleanWritable(boolean value) {
21         set(value);
22     }
23
24     /**
25      * Set the value of the BooleanWritable
26      */

27     public void set(boolean value) {
28         this.value = value;
29     }
30
31     /**
32      * Returns the value of the BooleanWritable
33      */

34     public boolean get() {
35         return value;
36     }
37
38     /**
39      */

40     public void readFields(DataInput in) throws IOException {
41         value = in.readBoolean();
42     }
43
44     /**
45      */

46     public void write(DataOutput out) throws IOException {
47         out.writeBoolean(value);
48     }
49
50     /**
51      */

52     public boolean equals(Object JavaDoc o) {
53         if (!(o instanceof BooleanWritable)) {
54             return false;
55         }
56         BooleanWritable other = (BooleanWritable) o;
57         return this.value == other.value;
58     }
59
60     /**
61      */

62     public int compareTo(Object JavaDoc o) {
63         boolean a = this.value;
64         boolean b = ((BooleanWritable) o).value;
65         return ((a == b) ? 0 : (a == false) ? -1 : 1);
66     }
67
68     /**
69      * A Comparator optimized for BooleanWritable.
70      */

71     public static class Comparator extends WritableComparator {
72         public Comparator() {
73             super(BooleanWritable.class);
74         }
75
76         public int compare(byte[] b1, int s1, int l1,
77                            byte[] b2, int s2, int l2) {
78             boolean a = (readInt(b1, s1) == 1) ? true : false;
79             boolean b = (readInt(b2, s2) == 1) ? true : false;
80             return ((a == b) ? 0 : (a == false) ? -1 : 1);
81         }
82     }
83 }
84
Popular Tags