Permalink
Browse files

Additional DataUtilities methods in response to review by Andrea and …

…Micheal
  • Loading branch information...
1 parent 6ba39ad commit c0fd151d0265aa53b696010d0d995344c6533104 @jodygarnett jodygarnett committed Nov 3, 2012
Showing with 282 additions and 546 deletions.
  1. +24 −21 docs/user/welcome/upgrade.rst
  2. +5 −13 ...-schema/app-schema/src/test/java/org/geotools/data/complex/filter/UnmappingFilterVisitorTest.java
  3. +5 −50 modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Encoder.java
  4. +8 −18 ...n/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/AbstractFeatureCollectionTypeBinding.java
  5. +2 −32 modules/extension/xsd/xsd-kml/src/main/java/org/geotools/kml/bindings/DocumentTypeBinding.java
  6. +7 −16 modules/library/coverage/src/main/java/org/geotools/resources/coverage/FeatureUtilities.java
  7. +163 −44 modules/library/main/src/main/java/org/geotools/data/DataUtilities.java
  8. +2 −42 modules/library/main/src/main/java/org/geotools/data/collection/CollectionDataStore.java
  9. +12 −80 modules/library/main/src/main/java/org/geotools/data/collection/TreeSetFeatureCollection.java
  10. +42 −0 modules/library/main/src/main/java/org/geotools/feature/collection/BridgeIterator.java
  11. +2 −8 modules/library/main/src/main/java/org/geotools/feature/collection/DelegateFeatureIterator.java
  12. +2 −8 modules/library/main/src/main/java/org/geotools/feature/collection/FeatureIteratorImpl.java
  13. 0 modules/library/main/src/{main → test}/java/org/geotools/data/collection/FeatureCollectionTest.java
  14. +2 −3 modules/library/main/src/test/java/org/geotools/data/memory/MemoryFeatureCollectionTest.java
  15. +2 −14 modules/library/render/src/main/java/org/geotools/renderer/lite/StreamingRenderer.java
  16. +2 −188 ...ioned/src/main/java/org/geotools/data/versioning/decorator/DefaultVersionedFeatureCollection.java
  17. +2 −9 ...rted/process-raster/src/main/java/org/geotools/process/raster/RasterAsPointCollectionProcess.java
@@ -63,15 +63,7 @@ AFTER::
ALTERNATE::
- SimpleFeatureCollection features = FeatureCollections.newCollection();
- if( features instanceof Collection ){
- Collection<SimpleFeature> collection = (Collection) features;
- collection.addAll( list );
- }
- else {
- throw new IllegalStateException("FeatureCollections configured with immutbale implementation");
- }
-
+ ListFeatureCollection features = FeatureCollections.newCollection( schema, list );
FeatureCollection Iterator
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -107,24 +99,36 @@ AFTER::
}
-FeatureCollection close method
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+How to Close an Iterator
+^^^^^^^^^^^^^^^^^^^^^^^^
-We have made FeatureCollection implement closable (for Java 7 try-with-resource compatibility). This
+We have made FeatureIterator implement Closable (for Java 7 try-with-resource compatibility). This
also provides an excellent replacement for FeatureCollection.close( Iterator ).
+If you are using any wrapping Iterators that still require the ability to close()
+please consider the following approach.
+
BEFORE::
- Iterator iterator = collection.iterator();
- try {
- ...
- } finally {
- if (collection instanceof SimpleFeatureCollection) {
- ((SimpleFeatureCollection) collection).close(iterator);
- }
+ Iterator iterator = collection.iterator();
+ try {
+ ...
+ } finally {
+ if (collection instanceof SimpleFeatureCollection) {
+ ((SimpleFeatureCollection) collection).close(iterator);
}
+ }
-AFTER::
+QUICK::
+
+ Iterator iterator = collection.iterator();
+ try {
+ ...
+ } finally {
+ DataUtilities.close( iterator );
+ }
+
+DETAIL::
Iterator iterator = collection.iterator();
try {
@@ -141,7 +145,6 @@ AFTER::
}
}
-
GeoTools 8.0
------------
@@ -38,6 +38,7 @@
import org.geotools.data.DataAccess;
import org.geotools.data.DataAccessFinder;
+import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureSource;
import org.geotools.data.complex.AppSchemaDataAccess;
import org.geotools.data.complex.AttributeMapping;
@@ -313,15 +314,7 @@ private void checkUnrollIdExpression(Expression idExpression) throws Exception {
this.visitor = new UnmappingFilterVisitor(this.mapping);
// retrieve a single sample feature
- FeatureCollection<?,?> content = mapping.getSource().getFeatures();
- FeatureIterator<?> iterator = content.features();
- Feature sourceFeature = null;
- try {
- sourceFeature = iterator.next();
- }
- finally {
- iterator.close();
- }
+ Feature sourceFeature = DataUtilities.first( mapping.getSource().getFeatures() );
String fid = sourceFeature.getIdentifier().toString();
Id fidFilter = ff.id(Collections.singleton(ff.featureId(fid)));
Filter unrolled = (Filter) fidFilter.accept(visitor, null);
@@ -331,10 +324,9 @@ private void checkUnrollIdExpression(Expression idExpression) throws Exception {
FeatureCollection<SimpleFeatureType,SimpleFeature> results = mapping.getSource()
.getFeatures(unrolled);
assertEquals(1, getCount(results));
- iterator = results.features();
- SimpleFeature unmappedFeature = (SimpleFeature) iterator.next();
- iterator.close();
-
+
+ SimpleFeature unmappedFeature = DataUtilities.first( results);
+
assertEquals(fid, unmappedFeature.getID());
}
@@ -58,6 +58,7 @@
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.util.XSDUtil;
+import org.geotools.data.DataUtilities;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.xml.impl.BindingFactoryImpl;
@@ -1025,7 +1026,7 @@ public int compare(Object o1, Object o2) {
iterator = collection.iterator();
} else if (obj instanceof FeatureCollection) {
FeatureCollection collection = (FeatureCollection) obj;
- iterator = new BridgeIterator( collection.features() );
+ iterator = DataUtilities.iterator( collection.features() );
} else {
iterator = new SingleIterator(obj);
}
@@ -1120,25 +1121,10 @@ public String encodeAsString(Object object, QName name ) throws IOException {
return new String(out.toByteArray());
}
- protected void closeIterator(Iterator itr, Object source) {
- if( itr instanceof Closeable){
- try {
- ((Closeable)itr).close();
- }
- catch( IOException e){
- Logger log = Logger.getLogger( source.getClass().getPackage().toString() );
- log.log(Level.FINE, e.getMessage(), e );
- }
- }
- //special case check here for feature collection
- // we need to ensure the iterator is closed properly
-// if ( source instanceof FeatureCollection ) {
-// //only close the iterator if not just a wrapping one
-// if ( !( itr instanceof SingleIterator ) ) {
-// ((FeatureCollection)source).close( itr );
-// }
-// }
+ protected void closeIterator(Iterator iterator, Object source) {
+ DataUtilities.close( iterator );
}
+
protected Node encode(Object object, XSDNamedComponent component) {
return encode( object, component, null );
}
@@ -1267,37 +1253,6 @@ public Object next() {
return null;
}
}
- /**
- * Internal closeable iterator used to bridge from FeatureCollection
- * to a "normal" iterator for encoding.
- *
- * @author jody
- */
- private static class BridgeIterator implements Iterator<Feature>, Closeable {
- FeatureIterator<?> delegate;
- public BridgeIterator(FeatureIterator<?> features) {
- this.delegate = features;
- }
-
- @Override
- public boolean hasNext() {
- return delegate.hasNext();
- }
-
- @Override
- public Feature next() {
- return delegate.next();
- }
-
- @Override
- public void remove() {
- }
-
- @Override
- public void close() throws IOException {
- delegate.close();
- }
- }
private static class SingleIterator implements Iterator {
Object object;
@@ -21,6 +21,7 @@
import javax.xml.namespace.QName;
+import org.geotools.data.DataUtilities;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.DefaultFeatureCollections;
@@ -95,30 +96,19 @@ public Object parse(ElementInstance instance, Node node, Object value)
}
//&lt;element maxOccurs="unbounded" minOccurs="0" ref="gml:featureMember"/&gt;
- List childValues = node.getChildValues(SimpleFeature.class);
+ List<SimpleFeature> childValues = node.getChildValues(SimpleFeature.class);
- if( featureCollection instanceof Collection){
- // example DefaultFeatureCollections or ListFeatureCollection
- ((Collection)featureCollection).addAll(childValues);
- }
- else {
- throw new IllegalStateException("DefaultFeatureCollection or ListFeatureCollection required");
- }
+ // example DefaultFeatureCollections or ListFeatureCollection
+ Collection<SimpleFeature> collection = DataUtilities.collectionCast( featureCollection );
+ collection.addAll(childValues);
//&lt;element minOccurs="0" ref="gml:featureMembers"/&gt;
SimpleFeature[] featureMembers = (SimpleFeature[]) node.getChildValue(SimpleFeature[].class);
- if( featureCollection instanceof Collection){
- // example DefaultFeatureCollections or ListFeatureCollection
- if (featureMembers != null) {
- for (int i = 0; i < featureMembers.length; i++) {
- ((Collection)featureCollection).add(featureMembers[i]);
- }
+ if (featureMembers != null) {
+ for (int i = 0; i < featureMembers.length; i++) {
+ collection.add(featureMembers[i]);
}
-
- }
- else {
- throw new IllegalStateException("DefaultFeatureCollection or ListFeatureCollection required");
}
return featureCollection;
@@ -29,6 +29,7 @@
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
+import org.geotools.data.DataUtilities;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
@@ -126,7 +127,7 @@ public List getProperties(Object object) throws Exception {
if ( object instanceof FeatureCollection ) {
FeatureCollection fc = (FeatureCollection) object;
//TODO: this does not close the iterator!!
- BridgeIterator iterator = new BridgeIterator( fc.features() );
+ Iterator iterator = DataUtilities.iterator( fc.features() );
prop[1] = iterator;
}
@@ -142,36 +143,5 @@ else if ( object instanceof SimpleFeature ) {
return l;
}
- /**
- * Internal closeable iterator used to bridge from FeatureCollection
- * to a "normal" iterator for encoding.
- *
- * @author jody
- */
- private static class BridgeIterator implements Iterator<Feature>, Closeable {
- FeatureIterator<?> delegate;
- public BridgeIterator(FeatureIterator<?> features) {
- this.delegate = features;
- }
-
- @Override
- public boolean hasNext() {
- return delegate.hasNext();
- }
-
- @Override
- public Feature next() {
- return delegate.next();
- }
-
- @Override
- public void remove() {
- }
-
- @Override
- public void close() throws IOException {
- delegate.close();
- }
- }
}
@@ -27,6 +27,7 @@
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.FactoryRegistryException;
import org.geotools.factory.GeoTools;
+import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
@@ -149,14 +150,9 @@ public static SimpleFeatureCollection wrapGridCoverage(final GridCoverage2D cove
fb.add(coverage);
SimpleFeature feature = fb.buildFeature(null);
- final SimpleFeatureCollection collection = FeatureCollections.newCollection();
- if( collection instanceof Collection){
- ((Collection<SimpleFeature>)collection).add(feature);
- return collection;
- }
- else {
- throw new IllegalStateException("Require access to a FeatureCollection supporting Collection.add");
- }
+ final DefaultFeatureCollection collection = new DefaultFeatureCollection();
+ collection.add(feature);
+ return collection;
}
/**
@@ -233,14 +229,9 @@ public static SimpleFeatureCollection wrapGridCoverageReader(final AbstractGridC
fb.add(params);
SimpleFeature feature = fb.buildFeature(null);
- final SimpleFeatureCollection collection = FeatureCollections.newCollection();
- if( collection instanceof Collection ){
- ((Collection<SimpleFeature>)collection).add(feature);
- return collection;
- }
- else {
- throw new IllegalStateException("Require access to SimpleFeatureCollection implementing Collecion.add");
- }
+ final DefaultFeatureCollection collection = new DefaultFeatureCollection();
+ collection.add(feature);
+ return collection;
}
/**
Oops, something went wrong.

0 comments on commit c0fd151

Please sign in to comment.