-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGroupComparator.java
More file actions
53 lines (46 loc) · 1.28 KB
/
GroupComparator.java
File metadata and controls
53 lines (46 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
/**
* This Comparator will invoke other Comparators as necesary in an attempt
* to sort on multiple fields from the same object.
*
* The first Comparator in the List is always invoked. The other Comparators
* are only invoked as required when the previous Comparator test returns
* an "equal" result.
*/
public class GroupComparator implements Comparator
{
private List<Comparator> comparators = new ArrayList<Comparator>();
/*
* A GroupComparator will always have at least two Comparators
*/
GroupComparator(Comparator first, Comparator second)
{
comparators.add( first );
comparators.add( second );
}
/*
* Add another Comparator to the sort mix.
*/
public void addComparator(Comparator comparator)
{
comparators.add( comparator );
}
/*
* Implement the Comparator interface
*/
@SuppressWarnings("unchecked")
public int compare(Object object1, Object object2)
{
// As long as the compare() result is equal (ie. 0),
// invoke the next Comparator
for (Comparator comparator : comparators)
{
int returnValue = comparator.compare(object1, object2);
if (returnValue != 0)
return returnValue;
}
return 0;
}
}