KickJava   Java API By Example, From Geeks To Geeks.

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


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 decorator for a comparator, with an "ascending" flag denoting
24  * whether comparison results should be treated in forward (standard
25  * ascending) order or flipped for reverse (descending) order.
26  *
27  * @author Keith Donald
28  * @author Juergen Hoeller
29  * @since 1.2.2
30  */

31 public class InvertibleComparator implements Comparator JavaDoc, Serializable JavaDoc {
32
33     private final Comparator JavaDoc comparator;
34
35     private boolean ascending = true;
36
37
38     /**
39      * Create an InvertibleComparator that sorts ascending by default.
40      * For the actual comparison, the specified Comparator will be used.
41      * @param comparator the comparator to decorate
42      */

43     public InvertibleComparator(Comparator JavaDoc comparator) {
44         this.comparator = comparator;
45     }
46
47     /**
48      * Create an InvertibleComparator that sorts based on the provided order.
49      * For the actual comparison, the specified Comparator will be used.
50      * @param comparator the comparator to decorate
51      * @param ascending the sort order: ascending (true) or descending (false)
52      */

53     public InvertibleComparator(Comparator JavaDoc comparator, boolean ascending) {
54         this.comparator = comparator;
55         setAscending(ascending);
56     }
57
58
59     /**
60      * Specify the sort order: ascending (true) or descending (false).
61      */

62     public void setAscending(boolean ascending) {
63         this.ascending = ascending;
64     }
65
66     /**
67      * Return the sort order: ascending (true) or descending (false).
68      */

69     public boolean isAscending() {
70         return ascending;
71     }
72
73     /**
74      * Invert the sort order: ascending -> descending or
75      * descending -> ascending.
76      */

77     public void invertOrder() {
78         this.ascending = !this.ascending;
79     }
80
81
82     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
83         int result = this.comparator.compare(o1, o2);
84         if (result != 0) {
85             // Invert the order if it is a reverse sort.
86
if (!this.ascending) {
87                 if (Integer.MIN_VALUE == result) {
88                     result = Integer.MAX_VALUE;
89                 }
90                 else {
91                     result *= -1;
92                 }
93             }
94             return result;
95         }
96         return 0;
97     }
98
99     public boolean equals(Object JavaDoc obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (!(obj instanceof InvertibleComparator)) {
104             return false;
105         }
106         InvertibleComparator other = (InvertibleComparator) obj;
107         return (this.comparator.equals(other.comparator) && this.ascending == other.ascending);
108     }
109
110     public int hashCode() {
111         return this.comparator.hashCode();
112     }
113
114     public String JavaDoc toString() {
115         return "InvertibleComparator: [" + this.comparator + "]; ascending=" + this.ascending;
116     }
117
118 }
119
Popular Tags