01: package com.jamonapi.utils;
02:
03: import java.util.Comparator;
04:
05: /** Maps a Comparator to a column number of an array starting at position 0. Used by JAMonArrayComparator */
06: class ArrayElementComparator implements Comparator {
07:
08: // Used to compare elements of an array that reside within the same column. The Object[][] array itself
09: // will be sorted according to this algorithm.
10:
11: private int sortCol;
12: private Comparator comparator;
13:
14: /** Constructor that takes the position in array to be compared as well as it's comparator */
15: public ArrayElementComparator(int sortCol, Comparator comparator) {
16: this .comparator = comparator;
17: this .sortCol = sortCol;
18: }
19:
20: public ArrayElementComparator(int sortCol, boolean naturalOrder) {
21: comparator = new JAMonComparator(naturalOrder);
22: this .sortCol = sortCol;
23: }
24:
25: /** Return col to be sorted/compared */
26: public int getSortCol() {
27: return sortCol;
28: }
29:
30: /** Call the comparator on the column */
31: public int compare(Object o1, Object o2) {
32: return comparator.compare(o1, o2);
33: }
34:
35: public String toString() {
36: return "sortCol=" + sortCol + ", comparator=" + comparator;
37: }
38:
39: }
|