01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.uilib.list.util.comparator;
16:
17: import java.io.Serializable;
18: import java.util.Comparator;
19: import org.apache.commons.lang.builder.HashCodeBuilder;
20:
21: /**
22: * Not-null comparator that compares <code>Boolean</code> values.
23: */
24: public class BooleanComparator implements Comparator, Serializable {
25: public static final Comparator TRUE_FIRST = new BooleanComparator(
26: true);
27: public static final Comparator FALSE_FIRST = new BooleanComparator(
28: false);
29:
30: private boolean trueFirst;
31:
32: private BooleanComparator(boolean trueFirst) {
33: this .trueFirst = trueFirst;
34: }
35:
36: public int compare(Object o1, Object o2) {
37: if (o1.equals(o2)) {
38: return 0;
39: }
40: // !o1.equals(o2)
41: if (this .trueFirst) {
42: return Boolean.TRUE.equals(o1) ? -1 : 1;
43: }
44: return Boolean.TRUE.equals(o1) ? 1 : -1;
45: }
46:
47: public boolean equals(Object obj) {
48: return BooleanComparator.class.equals(obj.getClass())
49: && ((BooleanComparator) obj).trueFirst == trueFirst;
50: }
51:
52: public int hashCode() {
53: return new HashCodeBuilder(20070327, 1455).append(trueFirst)
54: .toHashCode();
55: }
56: }
|