|
From: <svn...@os...> - 2011-12-13 11:07:25
|
Author: aaime
Date: 2011-12-13 03:07:14 -0800 (Tue, 13 Dec 2011)
New Revision: 38419
Added:
trunk/modules/library/main/src/main/java/org/geotools/feature/collection/SortedSimpleFeatureCollection.java
trunk/modules/library/main/src/test/java/org/geotools/data/collection/SortedFeatureCollectionTest.java
Log:
[GEOT-3975] Create a sorted feature collection decorator that does not require in memory sorting
Added: trunk/modules/library/main/src/main/java/org/geotools/feature/collection/SortedSimpleFeatureCollection.java
===================================================================
--- trunk/modules/library/main/src/main/java/org/geotools/feature/collection/SortedSimpleFeatureCollection.java (rev 0)
+++ trunk/modules/library/main/src/main/java/org/geotools/feature/collection/SortedSimpleFeatureCollection.java 2011-12-13 11:07:14 UTC (rev 38419)
@@ -0,0 +1,61 @@
+package org.geotools.feature.collection;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.geotools.data.simple.SimpleFeatureCollection;
+import org.geotools.data.simple.SimpleFeatureIterator;
+import org.geotools.data.sort.SortedFeatureIterator;
+import org.geotools.data.store.FeatureIteratorIterator;
+import org.geotools.factory.Hints;
+import org.geotools.feature.FeatureIterator;
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.filter.sort.SortBy;
+
+/**
+ * A wrapper that will sort a feature collection using a size sensitive algorithm, in main memory
+ * for small collections, using secondary memory otherwise. The threshold is defined by the
+ * {@link Hints#MAX_MEMORY_SORT} feature count
+ *
+ * @author Andrea Aime - GeoSolutions
+ *
+ */
+public class SortedSimpleFeatureCollection extends DecoratingSimpleFeatureCollection {
+
+ private SortBy[] sort;
+
+ public SortedSimpleFeatureCollection(SimpleFeatureCollection delegate, SortBy[] sort) {
+ super(delegate);
+ this.sort = sort;
+ }
+
+ @Override
+ public SimpleFeatureIterator features() {
+ try {
+ SimpleFeatureIterator features = ((SimpleFeatureCollection) delegate).features();
+ // sort if necessary
+ if (sort != null) {
+ features = new SortedFeatureIterator(features, getSchema(), sort, -1);
+ }
+ return features;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public Iterator iterator() {
+ return new FeatureIteratorIterator<SimpleFeature>(features());
+ }
+
+ @Override
+ public void close(FeatureIterator<SimpleFeature> close) {
+ close.close();
+ }
+
+ @Override
+ public void close(Iterator<SimpleFeature> close) {
+ FeatureIteratorIterator<SimpleFeature> fii = (FeatureIteratorIterator<SimpleFeature>) close;
+ delegate.close(fii.getDelegate());
+ }
+}
Added: trunk/modules/library/main/src/test/java/org/geotools/data/collection/SortedFeatureCollectionTest.java
===================================================================
--- trunk/modules/library/main/src/test/java/org/geotools/data/collection/SortedFeatureCollectionTest.java (rev 0)
+++ trunk/modules/library/main/src/test/java/org/geotools/data/collection/SortedFeatureCollectionTest.java 2011-12-13 11:07:14 UTC (rev 38419)
@@ -0,0 +1,68 @@
+/*
+ * GeoTools - The Open Source Java GIS Toolkit
+ * http://geotools.org
+ *
+ * (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+package org.geotools.data.collection;
+
+import java.util.Comparator;
+
+import org.geotools.data.DataUtilities;
+import org.geotools.data.simple.SimpleFeatureIterator;
+import org.geotools.data.store.FeatureCollectionWrapperTestSupport;
+import org.geotools.factory.CommonFactoryFinder;
+import org.geotools.feature.collection.SortedSimpleFeatureCollection;
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.filter.FilterFactory2;
+import org.opengis.filter.sort.SortBy;
+import org.opengis.filter.sort.SortOrder;
+
+public class SortedFeatureCollectionTest extends FeatureCollectionWrapperTestSupport {
+
+ FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
+
+ public void testNaturalSort() throws Exception {
+ SortedSimpleFeatureCollection sorted = new SortedSimpleFeatureCollection(delegate,
+ new SortBy[] { SortBy.NATURAL_ORDER });
+ checkSorted(sorted, DataUtilities.sortComparator(SortBy.NATURAL_ORDER));
+ }
+
+ public void testReverseSort() throws Exception {
+ SortedSimpleFeatureCollection sorted = new SortedSimpleFeatureCollection(delegate,
+ new SortBy[] { SortBy.REVERSE_ORDER });
+ checkSorted(sorted, DataUtilities.sortComparator(SortBy.REVERSE_ORDER));
+ }
+
+ public void testSortAttribute() throws Exception {
+ SortBy sort = ff.sort("someAtt", SortOrder.ASCENDING);
+ SortedSimpleFeatureCollection sorted = new SortedSimpleFeatureCollection(delegate,
+ new SortBy[] { sort });
+ checkSorted(sorted, DataUtilities.sortComparator(sort));
+ }
+
+ private void checkSorted(SortedSimpleFeatureCollection sorted,
+ Comparator<SimpleFeature> comparator) {
+ SimpleFeatureIterator fi = sorted.features();
+ SimpleFeature prev = null;
+ while (fi.hasNext()) {
+ SimpleFeature curr = fi.next();
+ if (prev != null) {
+ assertTrue("Failed on " + prev + " / " + curr, comparator.compare(prev, curr) <= 0);
+ }
+ prev = curr;
+ }
+ fi.close();
+ }
+
+}
|