001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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: */
017: package org.geotools.feature.iso;
018:
019: import org.opengis.feature.Feature;
020: import org.opengis.feature.simple.SimpleFeature;
021:
022: /**
023: * A utility class for creating simple Comparators for Features.
024: *
025: * @author Ian Schneider
026: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/fm/src/main/java/org/geotools/feature/iso/FeatureComparators.java $
027: */
028: public final class FeatureComparators {
029: /** A utility comparator for comparison by id. */
030: public static final java.util.Comparator BY_ID = new java.util.Comparator() {
031: public int compare(Object o1, Object o2) {
032: Feature f1 = (Feature) o1;
033: Feature f2 = (Feature) o2;
034:
035: return f1.getID().compareTo(f2.getID());
036: }
037: };
038:
039: /**
040: * Private constructor so default constructor is not available for this
041: * utility class.
042: */
043: private FeatureComparators() {
044: }
045:
046: /**
047: * Create a Comparator which compares Features by the attribute at the
048: * given index. The attribute at the index MUST be Comparable. This will
049: * probably not work for heterogenous collections, UNLESS the classes at
050: * the given index are the same.
051: *
052: * @param idx The index to look up attributes at.
053: *
054: * @return A new Comparator.
055: */
056: public static java.util.Comparator byAttributeIndex(final int idx) {
057: return new Index(idx);
058: }
059:
060: /**
061: * Create a Comparator which compares Features by the attribute found at
062: * the given path. The attribute found MUST be Comparable. This will
063: * probably not work for heterogenous collections, UNLESS the attributes
064: * found are the same class.
065: *
066: * @param name The xpath to use while comparing.
067: *
068: * @return A new Comparator.
069: */
070: public static java.util.Comparator byName(final String name) {
071: return new Name(name);
072: }
073:
074: /**
075: * A Comparator which performs the comparison on attributes at a given
076: * index. Compares only simple features, instances of {@link SimpleFeature}.
077: *
078: */
079: public static class Index implements java.util.Comparator {
080: /** the index of the attribute to compare against. */
081: private final int idx;
082:
083: /**
084: * Create a new Comparator based on the given index.
085: *
086: * @param i The index.
087: */
088: public Index(int i) {
089: idx = i;
090: }
091:
092: /**
093: * Implementation of Comparator. Calls compareAtts to perform the
094: * actual comparison.
095: *
096: * @param o1 The first Feature.
097: * @param o2 The second Feature
098: *
099: * @return A value indicating less than, equal, or greater than.
100: */
101: public int compare(Object o1, Object o2) {
102:
103: SimpleFeature f1 = (SimpleFeature) o1;
104: SimpleFeature f2 = (SimpleFeature) o2;
105:
106: return compareAtts(f1.getValue(idx), f2.getValue(idx));
107: }
108:
109: /**
110: * Compares the two attributes.
111: *
112: * @param att1 The first attribute to compare.
113: * @param att2 The second attribute to compare.
114: *
115: * @return A value indicating less than, equal, or greater than.
116: */
117: protected int compareAtts(Object att1, Object att2) {
118: return ((Comparable) att1).compareTo((Comparable) att2);
119: }
120: }
121:
122: /**
123: * A Comparator which performs the comparison on attributes with a given
124: * name. Compares only simple features, instances of {@link SimpleFeature}.
125: */
126: public static class Name implements java.util.Comparator {
127: /** The name to compare on */
128: private final String name;
129:
130: /**
131: * Create a new Comparator based on the given index.
132: *
133: * @param name The attribute name.
134: */
135: public Name(String name) {
136: this .name = name;
137: }
138:
139: /**
140: * Implementation of Comparator. Calls compareAtts to perform the
141: * actual comparison.
142: *
143: * @param o1 The first Feature.
144: * @param o2 The second Feature
145: *
146: * @return A value indicating less than, equal, or greater than.
147: */
148: public int compare(Object o1, Object o2) {
149: SimpleFeature f1 = (SimpleFeature) o1;
150: SimpleFeature f2 = (SimpleFeature) o2;
151:
152: return compareAtts(f1.getValue(name), f2.getValue(name));
153: }
154:
155: /**
156: * Compares the two attributes.
157: *
158: * @param att1 The first attribute to compare.
159: * @param att2 The second attribute to compare.
160: *
161: * @return A value indicating less than, equal, or greater than.
162: */
163: protected int compareAtts(Object att1, Object att2) {
164: if ((att1 == null) && (att2 == null)) {
165: return 0;
166: }
167:
168: return ((Comparable) att1).compareTo((Comparable) att2);
169: }
170: }
171: }
|