001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.feature;
017:
018: /**
019: * A utility class for creating simple Comparators for Features.
020: *
021: * @author Ian Schneider
022: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/FeatureComparators.java $
023: */
024: public final class FeatureComparators {
025: /** A utility comparator for comparison by id. */
026: public static final java.util.Comparator BY_ID = new java.util.Comparator() {
027: public int compare(Object o1, Object o2) {
028: Feature f1 = (Feature) o1;
029: Feature f2 = (Feature) o2;
030:
031: return f1.getID().compareTo(f2.getID());
032: }
033: };
034:
035: /**
036: * Private constructor so default constructor is not available for this
037: * utility class.
038: */
039: private FeatureComparators() {
040: }
041:
042: /**
043: * Create a Comparator which compares Features by the attribute at the
044: * given index. The attribute at the index MUST be Comparable. This will
045: * probably not work for heterogenous collections, UNLESS the classes at
046: * the given index are the same.
047: *
048: * @param idx The index to look up attributes at.
049: *
050: * @return A new Comparator.
051: */
052: public static java.util.Comparator byAttributeIndex(final int idx) {
053: return new Index(idx);
054: }
055:
056: /**
057: * Create a Comparator which compares Features by the attribute found at
058: * the given path. The attribute found MUST be Comparable. This will
059: * probably not work for heterogenous collections, UNLESS the attributes
060: * found are the same class.
061: *
062: * @param name The xpath to use while comparing.
063: *
064: * @return A new Comparator.
065: */
066: public static java.util.Comparator byAttributeName(final String name) {
067: return new Name(name);
068: }
069:
070: /**
071: * A Comparator which performs the comparison on attributes at a given
072: * index.
073: */
074: public static class Index implements java.util.Comparator {
075: /** the index of the attribute to compare against. */
076: private final int idx;
077:
078: /**
079: * Create a new Comparator based on the given index.
080: *
081: * @param i The index.
082: */
083: public Index(int i) {
084: idx = i;
085: }
086:
087: /**
088: * Implementation of Comparator. Calls compareAtts to perform the
089: * actual comparison.
090: *
091: * @param o1 The first Feature.
092: * @param o2 The second Feature
093: *
094: * @return A value indicating less than, equal, or greater than.
095: */
096: public int compare(Object o1, Object o2) {
097: Feature f1 = (Feature) o1;
098: Feature f2 = (Feature) o2;
099:
100: return compareAtts(f1.getAttribute(idx), f2
101: .getAttribute(idx));
102: }
103:
104: /**
105: * Compares the two attributes.
106: *
107: * @param att1 The first attribute to compare.
108: * @param att2 The second attribute to compare.
109: *
110: * @return A value indicating less than, equal, or greater than.
111: */
112: protected int compareAtts(Object att1, Object att2) {
113: return ((Comparable) att1).compareTo((Comparable) att2);
114: }
115: }
116:
117: /**
118: * A Comparator which performs the comparison on attributes with a given
119: * name.
120: */
121: public static class Name implements java.util.Comparator {
122: /** The name to compare on */
123: private final String name;
124:
125: /**
126: * Create a new Comparator based on the given index.
127: *
128: * @param name The attribute name.
129: */
130: public Name(String name) {
131: this .name = name;
132: }
133:
134: /**
135: * Implementation of Comparator. Calls compareAtts to perform the
136: * actual comparison.
137: *
138: * @param o1 The first Feature.
139: * @param o2 The second Feature
140: *
141: * @return A value indicating less than, equal, or greater than.
142: */
143: public int compare(Object o1, Object o2) {
144: Feature f1 = (Feature) o1;
145: Feature f2 = (Feature) o2;
146:
147: return compareAtts(f1.getAttribute(name), f2
148: .getAttribute(name));
149: }
150:
151: /**
152: * Compares the two attributes.
153: *
154: * @param att1 The first attribute to compare.
155: * @param att2 The second attribute to compare.
156: *
157: * @return A value indicating less than, equal, or greater than.
158: */
159: protected int compareAtts(Object att1, Object att2) {
160: if ((att1 == null) && (att2 == null)) {
161: return 0;
162: }
163:
164: return ((Comparable) att1).compareTo((Comparable) att2);
165: }
166: }
167: }
|