01: /*
02: * The contents of this file are subject to the Mozilla Public License
03: * Version 1.1 (the "License"); you may not use this file except in
04: * compliance with the License. You may obtain a copy of the License at
05: * http://www.mozilla.org/MPL/
06: *
07: * Software distributed under the License is distributed on an "AS IS"
08: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
09: * License for the specific language governing rights and limitations
10: * under the License.
11: *
12: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
13: *
14: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
15: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
16: *
17: * Contributor(s):
18: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
19: *
20: * If you didn't download this code from the following link, you should check
21: * if you aren't using an obsolete version: http://www.isqlviewer.com
22: */
23: package org.isqlviewer.swing.table;
24:
25: import java.util.Comparator;
26: import java.util.List;
27:
28: /**
29: * Utility class for sortings collections.
30: * <p>
31: * This class was designed mainly for use within the EnhancedTableModel and the Sortable Interface.
32: *
33: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
34: * @version 1.0
35: */
36: class RowComparator implements Comparator<List<Object>> {
37:
38: private int cColumn = 0;
39: private boolean isAscending = true;
40:
41: /**
42: * Default constructor for a given colum and direction.
43: * <p>
44: * Once the compare method is called it will use the comparable column as the index of the List Object it is
45: * comparing. The isAsc variable allows for easy ascending and desceding sorting.
46: *
47: * @param comparableColumn index to compare from.
48: * @param isAsc boolean to multiply the comparable value by -1 to reverse its comparsion.
49: */
50: public RowComparator(int comparableColumn, boolean isAsc) {
51:
52: cColumn = comparableColumn;
53: isAscending = isAsc;
54: }
55:
56: /**
57: * Compares objects within the list as specified by the Comparable interface.
58: * <p>
59: *
60: * @see Comparable#compareTo(java.lang.Object)
61: * @param src instance of List to compare.
62: * @param cmp instance of List to compare.
63: */
64: public int compare(List<Object> src, List<Object> cmp) {
65:
66: if (cColumn == -1) {
67: return 0;
68: }
69:
70: Object s = src.get(cColumn);
71: Object c = cmp.get(cColumn);
72: if (s == null & c != null) {
73: return (isAscending ? -1 : 1) * -1;
74: } else if (s != null & c == null) {
75: return (isAscending ? -1 : 1) * 1;
76: } else if (s == null && c == null) {
77: return 0;
78: } else if (s instanceof Comparable) {
79: Comparable cs = (Comparable) s;
80: return (isAscending ? -1 : 1) * cs.compareTo(c);
81: }
82: return 0;
83: }
84:
85: }
|