...
FeatureCollection Add
With the FeatureCollection.add method being removed, you will need to use an explicit instance that supports adding content.
BEFORE:
| Code Block |
|---|
SimpleFeatureCollection features = FeatureCollections.newCollection();
for( SimpleFeature feature : list ){
features.add( feature );
} |
AFTER:
| Code Block |
|---|
DefaultFeatureCollection features = new DefaultFeatureCollection();
for( SimpleFeature feature : list ){
features.add( feature );
} |
ALTERNATE (will throw exception if FeatureColleciton does not implement java.util.Collection )
| Code Block |
|---|
Collection<SimpleFeature> collection = DataUtilities.collectionCast( featureCollection );
collection.addAll( list ); |
ALTERNATE DETAIL:
| Code Block |
|---|
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");
} |
FeatureCollection Iterator
The deprecated FeatureCollection.iterator() method is no longer available, please use FeatureCollection.features() as shown below.
BEFORE:
| Code Block |
|---|
Iterator i=featureCollection.iterator();
try {
while( i.hasNext(); ){
SimpleFeature feature = i.next();
...
}
}
finally {
featureCollection.close( i );
} |
AFTER:
| Code Block |
|---|
FeatureIterator i=featureCollection.features();
try {
while( i.hasNext(); ){
SimpleFeature feature = i.next();
...
}
}
finally {
i.close();
} |
JAVA7 (using try-with-resource):
| Code Block |
|---|
try ( FeatureIterator i=featureCollection.features()){
while( i.hasNext() ){
SimpleFeature feature = i.next();
...
}
} |
FeatureCollection close method
We have made FeatureCollection implement closable (for Java 7 try-with-resource compatibility). This also provides an excellent replacement for FeatureCollection.close( Iterator ).
For other code that relied on FeatureCollection.close( Iterator ) to clean up after things, please make sure your Iterator implements Closeable.
BEFORE:
| Code Block |
|---|
Iterator iterator = collection.iterator();
try {
...
} finally {
if (collection instanceof SimpleFeatureCollection) {
((SimpleFeatureCollection) collection).close(iterator);
}
} |
AFTER:
| Code Block |
|---|
Iterator iterator = collection.iterator();
try {
...
} finally {
DataUtilities.close( iterator, collection );
} |
DETAIL:
| Code Block |
|---|
Iterator iterator = collection.iterator();
try {
...
} finally {
if (iterator instanceof Closeable) {
try {
((Closeable)iterator).close();
}
catch( IOException e){
Logger log = Logger.getLogger( collection.getClass().getPackage().toString() );
log.log(Level.FINE, e.getMessage(), e );
}
}
} |
Utility method for the above logic:
| Code Block |
|---|
Iterator iterator = collection.iterator();
try {
...
} finally {
DataUtilities.close( iterator, collection );
} |
JAVA7 (JAVA7: using try-with-resource ):syntax for iterators that implement Closeable
| Code Block |
|---|
try ( FeatureIterator i=featureCollection.features()){
...
} |