01: package com.jamonapi.utils; // FormattedDataSet API
02:
03: import java.util.*;
04:
05: /**
06: * <p>This class allows you to compare Object[] arrays by multiple columns. It can also compare any Object that implements the ToArray interface.
07: * Each column will use the underlying
08: * Compareable interface in natural or reverse order depending on how it is added, or will use the passed in Comparator.
09: * </p>
10: *
11: */
12:
13: /** Note I took this code from fdsapi.com, and would like to eventually merge these 2 projects, so
14: * this class will eventually be replaced by the one in FDS.
15: */
16:
17: public class JAMonArrayComparator extends java.lang.Object implements
18: Comparator
19:
20: {
21: private List sortCols = new ArrayList();
22: private static final Comparator defaultComparator = new JAMonComparator();
23:
24: public JAMonArrayComparator() {
25: }
26:
27: /** Sort/compare the passed in col number starting at 0 in natural (true) or reverse (false)
28: * order based on the columns Compareable interface being called.
29: * @param sortCol
30: * @param naturalOrder
31: */
32: public JAMonArrayComparator(int sortCol, boolean naturalOrder) {
33: addCompareCol(sortCol, naturalOrder);
34: }
35:
36: /** Compare the passed in col in natural order */
37: public void addCompareCol(int sortCol) {
38: sortCols.add(new ArrayElementComparator(sortCol, true));
39: }
40:
41: /** Compare the passed in col in natural or reverse order */
42:
43: public void addCompareCol(int sortCol, boolean naturalOrder) {
44: sortCols.add(new ArrayElementComparator(sortCol, naturalOrder));
45: }
46:
47: /** Compare the passed in col based on the passed in Comparator */
48:
49: public void addCompareCol(int sortCol, Comparator comparator) {
50: sortCols.add(new ArrayElementComparator(sortCol, comparator));
51: }
52:
53: /**
54: * Method used by the comparator interface. <br><br>
55: * o1 < o2 - returns a negative integer<br>
56: * o1==o2 - returns zero<br>
57: * o1>o2 - returns a postitive integer<br><br>
58: * Iterate through all columns that should be compared (in the proper order)
59: * and call the Comparator for each of the column elements.
60: */
61:
62: public int compare(Object o1, Object o2) {
63: Object[] array1 = null;
64: Object[] array2 = null;
65: int compareVal = 0;
66:
67: // Put data into array format or call the underlying defaultComparator if
68: // the data isn't tabular
69: if (o1 instanceof ToArray && o2 instanceof ToArray) {
70: array1 = ((ToArray) o1).toArray();
71: array2 = ((ToArray) o2).toArray();
72: } else if (o1 instanceof Object[] && o2 instanceof Object[]) {
73: array1 = (Object[]) o1;
74: array2 = (Object[]) o2;
75: } else {
76: return defaultComparator.compare(o1, o2);
77: }
78:
79: /** Iterate through the Arrays comparators to compare columns */
80: Iterator iter = sortCols.iterator();
81: while (iter.hasNext()) {
82: ArrayElementComparator elementComp = (ArrayElementComparator) iter
83: .next();
84: Object element1 = array1[elementComp.getSortCol()];
85: Object element2 = array2[elementComp.getSortCol()];
86:
87: compareVal = elementComp.compare(element1, element2);
88:
89: if (compareVal != 0) // if one of the elments is not equal we know all we need to know and can return
90: break;
91: }
92:
93: return compareVal;
94:
95: }
96:
97: }
|