Hi Jody,

Should the ‘after’ syntax for the FeatureCollection close method also include 
an example using the new Java 7 try-with-resource syntax also for use in code 
targeting Java 7. This would appear in examples and perhaps not in the code 
base yet.

Try (Iterator iterator = collection.iterator()) {
         ...
}

This is so much shorter.

Brett

From: Jody Garnett [mailto:jody.garn...@gmail.com]
Sent: Thursday, 1 November 2012 10:05 AM
To: Michael Bedward
Cc: geotools-devel@lists.sourceforge.net
Subject: Re: [Geotools-devel] feature collection cleanup pull request and 
update instructions

So could DefaultFeatureCollection.add be deprecated ?
Nope - it is going to remain our implementation of FeatureCollection + 
Collection. It is just now we ask people
to use it explicitly.

Consider:
- ListFeatureCollection currently does not prevent the user adding the same 
feature twice (or two feature instances with the same feature id)
- Code migrating from FeatureCollections.newCollection() was ending up with a 
DefaultFeatureCollection anyways, so using DefaultFeatureCollection here is the 
least risk alternative.
- FeatureCollections is now deprecated - in favour of new 
DefaultFeatureCollection() or other explicit choice

Here are the upgrade 
instructions<http://docs.codehaus.org/display/GEOTOOLS/FeatureCollection+revolution#FeatureCollectionrevolution-UpgradeInstructions>,
 perhaps we can go over them in email here and refine them more directly :-)
FeatureCollection Add

With the FeatureCollection.add method being removed, you will need to use an 
explicit instance that supports adding content.

BEFORE:
SimpleFeatureCollection features = FeatureCollections.newCollection();

for( SimpleFeature feature : list ){
   features.add( feature );
}


AFTER:
DefaultFeatureCollection features = new DefaultFeatureCollection();
for( SimpleFeature feature : list ){
   features.add( feature );
}


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");
}

FeatureCollection Iterator

The deprecated FeatureCollection.iterator() method is no longer available, 
please use FeatureCollection.features() as shown below.

BEFORE:
Iterator i=featureCollection.iterator();
try {
    while( i.hasNext(); ){
       SimpleFeature feature = i.next();
       ...
    }
}
finally {
    featureCollection.close( i );
}


AFTER:
FeatureIterator i=featureCollection.features();
try {
     while( i.hasNext(); ){
         SimpleFeature feature = i.next();
         ...
     }
}
finally {
     i.close();
}

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 ).

BEFORE:
Iterator iterator = collection.iterator();
try {
   ...
} finally {
    if (collection instanceof SimpleFeatureCollection) {
        ((SimpleFeatureCollection) collection).close(iterator);
    }
}


AFTER:
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 );
        }
    }
}



------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
GeoTools-Devel mailing list
GeoTools-Devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to