KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > comparator > BooleanComparator


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util.comparator;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Comparator JavaDoc;
21
22 /**
23  * A Comparator for Boolean objects that can sort either true or false first.
24  *
25  * @author Keith Donald
26  * @since 1.2.2
27  */

28 public final class BooleanComparator implements Comparator JavaDoc, Serializable JavaDoc {
29
30     /**
31      * A shared default instance of this comparator, treating true lower
32      * than false.
33      */

34     public static final BooleanComparator TRUE_LOW = new BooleanComparator(true);
35
36     /**
37      * A shared default instance of this comparator, treating true higher
38      * than false.
39      */

40     public static final BooleanComparator TRUE_HIGH = new BooleanComparator(false);
41
42
43     private final boolean trueLow;
44
45
46     /**
47      * Create a BooleanComparator that sorts boolean values based on
48      * the provided flag.
49      * <p>Alternatively, you can use the default shared instances:
50      * <code>BooleanComparator.TRUE_LOW</code> and
51      * <code>BooleanComparator.TRUE_HIGH</code>.
52      * @param trueLow whether to treat true as lower or higher than false
53      * @see #TRUE_LOW
54      * @see #TRUE_HIGH
55      */

56     public BooleanComparator(boolean trueLow) {
57         this.trueLow = trueLow;
58     }
59
60
61     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
62         boolean v1 = ((Boolean JavaDoc) o1).booleanValue();
63         boolean v2 = ((Boolean JavaDoc) o2).booleanValue();
64         return (v1 ^ v2) ? ((v1 ^ this.trueLow) ? 1 : -1) : 0;
65     }
66
67     public boolean equals(Object JavaDoc obj) {
68         if (this == obj) {
69             return true;
70         }
71         if (!(obj instanceof BooleanComparator)) {
72             return false;
73         }
74         return (this.trueLow == ((BooleanComparator) obj).trueLow);
75     }
76
77     public int hashCode() {
78         return (this.trueLow ? -1 : 1) * getClass().hashCode();
79     }
80
81     public String JavaDoc toString() {
82         return "BooleanComparator: " + (this.trueLow ? "true low" : "true high");
83     }
84
85 }
86
Popular Tags