Skip to content

Commit

Permalink
Additional DataUtilities methods in response to review by Andrea and …
Browse files Browse the repository at this point in the history
…Micheal
  • Loading branch information
jodygarnett committed Nov 3, 2012
1 parent 6ba39ad commit c0fd151
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 546 deletions.
45 changes: 24 additions & 21 deletions docs/user/welcome/upgrade.rst
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -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 {
Expand All @@ -141,7 +145,6 @@ AFTER::
}
}


GeoTools 8.0
------------

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}
}
}

Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit c0fd151

Please sign in to comment.