Gertjan van Oosten ha scritto:
> Hi developers,
> 
> I'm getting unexpected behaviour from FeatureCollection.
> Basically, what I'm doing is this:
> 
>   final FeatureCollection featuresBeforeInsert =
>     getAllFeatures(connectionString, typeName);
> 
>   insertFeature(connectionString, typeName, featureAttributes);
> 
>   final FeatureCollection featuresAfterInsert =
>     getAllFeatures(connectionString, typeName);
> 
>   assertEquals(featuresBeforeInsert.size(), featuresAfterInsert.size()-1);
> 
> where getAllFeatures() retrieves all features of the given type from the
> PostGIS data store, and insertFeature inserts one feature of the given
> type with the given attributes in the data store.
> 
> The assert fails, because both collections featuresBeforeInsert and
> featuresAfterInsert have the same size.  Apparently, the insertFeature
> also changes the collection under the hood of the featuresBeforeInsert...
> That is rather unexpected.  Is this intended behaviour?  Should I have
> been aware of it?  Is it documented somewhere?  And more importantly:
> how do I get a FeatureCollection that does not get changed underneath me?

Unfortunately this is intended behaviour :(
FeatureCollection is a persistent collection, whatever you do against
it, you do against your persistent data. Usually its memory footprint
is close to zero, that is, each time you try and access it, it'll
reload data from the store, and so on. All that is gives you is a
collection like access to your storage, but it's not an in memory
snapshot like one would expect (at least, like me and you would expect).

If you need an in memory copy of the data instead, it should work
if you create a DefaultFeatureCollection :

FeatureCollection dstoreColl = myFeatureSource.getFeatures();
DefaultFeatureCollection inMemoryColl = new 
DefaultFeatureCollection(null, dstoreColl.getType());
inMemoryColl.addAll(dstoreColl);

Hope this helps
Cheers
Andrea

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to