KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.springframework.util.Assert;
26
27 /**
28  * A comparator that chains a sequence of one or more more Comparators.
29  *
30  * <p>A compound comparator calls each Comparator in sequence until a single
31  * Comparator returns a non-zero result, or the comparators are exhausted and
32  * zero is returned.
33  *
34  * <p>This facilitates in-memory sorting similar to multi-column sorting in SQL.
35  * The order of any single Comparator in the list can also be reversed.
36  *
37  * @author Keith Donald
38  * @author Juergen Hoeller
39  * @since 1.2.2
40  */

41 public class CompoundComparator implements Comparator JavaDoc, Serializable JavaDoc {
42
43     private final List JavaDoc comparators;
44
45
46     /**
47      * Construct a CompoundComparator with initially no Comparators. Clients
48      * must add at least one Comparator before calling the compare method or an
49      * IllegalStateException is thrown.
50      */

51     public CompoundComparator() {
52         this.comparators = new ArrayList JavaDoc();
53     }
54
55     /**
56      * Construct a CompoundComparator from the Comparators in the provided array.
57      * <p>All Comparators will default to ascending sort order,
58      * unless they are InvertibleComparators.
59      * @param comparators the comparators to build into a compound comparator
60      * @see InvertibleComparator
61      */

62     public CompoundComparator(Comparator JavaDoc[] comparators) {
63         this.comparators = new ArrayList JavaDoc(comparators.length);
64         for (int i = 0; i < comparators.length; i++) {
65             addComparator(comparators[i]);
66         }
67     }
68
69
70     /**
71      * Add a Comparator to the end of the chain.
72      * <p>The Comparator will default to ascending sort order,
73      * unless it is a InvertibleComparator.
74      * @param comparator the Comparator to add to the end of the chain
75      * @see InvertibleComparator
76      */

77     public void addComparator(Comparator JavaDoc comparator) {
78         if (comparator instanceof InvertibleComparator) {
79             this.comparators.add(comparator);
80         }
81         else {
82             this.comparators.add(new InvertibleComparator(comparator));
83         }
84     }
85
86     /**
87      * Add a Comparator to the end of the chain using the provided sort order.
88      * @param comparator the Comparator to add to the end of the chain
89      * @param ascending the sort order: ascending (true) or descending (false)
90      */

91     public void addComparator(Comparator JavaDoc comparator, boolean ascending) {
92         this.comparators.add(new InvertibleComparator(comparator, ascending));
93     }
94
95     /**
96      * Replace the Comparator at the given index.
97      * <p>The Comparator will default to ascending sort order,
98      * unless it is a InvertibleComparator.
99      * @param index the index of the Comparator to replace
100      * @param comparator the Comparator to place at the given index
101      * @see InvertibleComparator
102      */

103     public void setComparator(int index, Comparator JavaDoc comparator) {
104         if (comparator instanceof InvertibleComparator) {
105             this.comparators.set(index, comparator);
106         }
107         else {
108             InvertibleComparator invComp = new InvertibleComparator(comparator);
109             this.comparators.set(index, invComp);
110         }
111     }
112
113     /**
114      * Replace the Comparator at the given index using the given sort order.
115      * @param index the index of the Comparator to replace
116      * @param comparator the Comparator to place at the given index
117      * @param ascending the sort order: ascending (true) or descending (false)
118      */

119     public void setComparator(int index, Comparator JavaDoc comparator, boolean ascending) {
120         InvertibleComparator invComp = new InvertibleComparator(comparator, ascending);
121         this.comparators.set(index, invComp);
122     }
123
124     /**
125      * Invert the sort order of each sort definition contained by this compound
126      * comparator.
127      */

128     public void invertOrder() {
129         Iterator JavaDoc it = this.comparators.iterator();
130         while (it.hasNext()) {
131             ((InvertibleComparator) it.next()).invertOrder();
132         }
133     }
134
135     /**
136      * Invert the sort order of the sort definition at the specified index.
137      * @param index the index of the comparator to invert
138      */

139     public void invertOrder(int index) {
140         getInvertibleComparator(index).invertOrder();
141     }
142
143     /**
144      * Change the sort order at the given index to ascending.
145      * @param index the index of the comparator to change
146      */

147     public void setAscendingOrder(int index) {
148         getInvertibleComparator(index).setAscending(true);
149     }
150
151     /**
152      * Change the sort order at the given index to descending sort.
153      * @param index the index of the comparator to change
154      */

155     public void setDescendingOrder(int index) {
156         getInvertibleComparator(index).setAscending(false);
157     }
158
159     /**
160      * Return the InvertibleComparator for the given index, if any.
161      */

162     private InvertibleComparator getInvertibleComparator(int index) {
163         return (InvertibleComparator) this.comparators.get(index);
164     }
165
166     /**
167      * Returns the number of aggregated comparators.
168      */

169     public int getComparatorCount() {
170         return comparators.size();
171     }
172
173
174     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
175         Assert.state(this.comparators.size() > 0,
176                 "No sort definitions have been added to this CompoundComparator to compare");
177         for (Iterator JavaDoc it = this.comparators.iterator(); it.hasNext();) {
178             InvertibleComparator def = (InvertibleComparator) it.next();
179             int result = def.compare(o1, o2);
180             if (result != 0) {
181                 return result;
182             }
183         }
184         return 0;
185     }
186
187     public boolean equals(Object JavaDoc obj) {
188         if (this == obj) {
189             return true;
190         }
191         if (!(obj instanceof CompoundComparator)) {
192             return false;
193         }
194         CompoundComparator other = (CompoundComparator) obj;
195         return this.comparators.equals(other.comparators);
196     }
197
198     public int hashCode() {
199         return this.comparators.hashCode();
200     }
201
202     public String JavaDoc toString() {
203         return "CompoundComparator: " + this.comparators;
204     }
205
206 }
207
Popular Tags