01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.ui;
16:
17: import java.io.Serializable;
18: import java.util.Comparator;
19:
20: import org.eclipse.swt.SWT;
21: import org.geotools.feature.Feature;
22:
23: public class AttributeComparator implements Comparator<Feature>,
24: Serializable {
25: /** long serialVersionUID field */
26: private static final long serialVersionUID = -3521669094688139495L;
27: private int sortDir;
28: private String xpath;
29:
30: public AttributeComparator(int dir, String xpath) {
31: if (dir == SWT.UP) {
32: this .sortDir = -1;
33: } else if (dir == SWT.DOWN) {
34: this .sortDir = 1;
35: } else
36: throw new IllegalArgumentException(
37: "dir must be SWT.UP or SWT.DOWN was: " + dir); //$NON-NLS-1$
38: this .xpath = xpath;
39: }
40:
41: @SuppressWarnings("unchecked")
42: public int compare(Feature f0, Feature f1) {
43:
44: Object data0 = f0.getAttribute(xpath);
45: Object data1 = f1.getAttribute(xpath);
46: int result;
47:
48: if (data0 == null) {
49: if (data1 == null)
50: return 0;
51: else
52: return 1;
53: }
54:
55: if (data0.equals(data1)) {
56: result = 1;
57: } else if (data0 instanceof Comparable
58: && data1 instanceof Comparable) {
59: Comparable<Object> comparable0 = (Comparable) data0;
60: Comparable<Object> comparable1 = (Comparable) data1;
61: result = comparable0.compareTo(comparable1) > 0 ? 1 : -1;
62: } else {
63: result = 1;
64: }
65:
66: return sortDir * result;
67: }
68:
69: @Override
70: public int hashCode() {
71: final int PRIME = 31;
72: int result = 1;
73: result = PRIME * result + sortDir;
74: result = PRIME * result
75: + ((xpath == null) ? 0 : xpath.hashCode());
76: return result;
77: }
78:
79: @Override
80: public boolean equals(Object obj) {
81: if (this == obj)
82: return true;
83: if (obj == null)
84: return false;
85: if (getClass() != obj.getClass())
86: return false;
87: final AttributeComparator other = (AttributeComparator) obj;
88: if (sortDir != other.sortDir)
89: return false;
90: if (xpath == null) {
91: if (other.xpath != null)
92: return false;
93: } else if (!xpath.equals(other.xpath))
94: return false;
95: return true;
96: }
97:
98: }
|