Permalink
Browse files

A few leaps of faith checking for instance of Collection in order to …

…add content. CompositeFeatureCollection was adapted to use FeatureIterator
  • Loading branch information...
1 parent 5d2a655 commit 3cfbb24ef9591a5cd1bdd5d373d83d2929dab910 @jodygarnett jodygarnett committed Oct 30, 2012
@@ -25,7 +25,9 @@
import org.geotools.data.DataUtilities;
import org.geotools.data.store.DataFeatureCollection;
import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureIterator;
import org.geotools.geometry.jts.ReferencedEnvelope;
+import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.identity.FeatureId;
@@ -48,13 +50,13 @@
/**
* wrapped collecitons
*/
- List collections;
+ List<FeatureCollection> collections;
- public CompositeFeatureCollection(List collections) {
+ public CompositeFeatureCollection(List<FeatureCollection> collections) {
this.collections = collections;
}
- protected Iterator openIterator() throws IOException {
+ protected Iterator<SimpleFeature> openIterator() throws IOException {
return new CompositeIterator();
}
@@ -82,9 +84,9 @@ public int getCount() throws IOException {
return count;
}
- class CompositeIterator implements Iterator {
+ class CompositeIterator implements Iterator<SimpleFeature> {
int index;
- Iterator iterator;
+ FeatureIterator iterator;
public CompositeIterator() {
index = 0;
@@ -103,11 +105,11 @@ public boolean hasNext() {
while (index < collections.size()) {
//close current before we move to next
if (iterator != null) {
- ((FeatureCollection) collections.get(index - 1)).close(iterator);
+ iterator.close();
}
//grap next
- iterator = ((FeatureCollection) collections.get(index++)).iterator();
+ iterator = ((FeatureCollection) collections.get(index++)).features();
if (iterator.hasNext()) {
return true;
@@ -117,54 +119,33 @@ public boolean hasNext() {
//no more
if (iterator != null) {
//close the last iterator
- ((FeatureCollection) collections.get(collections.size() - 1)).close(iterator);
+ iterator.close();
}
-
return false;
}
- public Object next() {
- return iterator.next();
+ public SimpleFeature next() {
+ return (SimpleFeature) iterator.next();
}
}
public boolean addAll(Collection arg0) {
throw new RuntimeException("Can't add to a composite featurecollection; you need to add to one of the constituent collections direclty.");
}
-
- public boolean removeAll(Collection arg0) {
- Iterator it = collections.iterator();
- boolean result = false;
- while (it.hasNext()){
- FeatureCollection col = (FeatureCollection)it.next();
- result |= col.removeAll(arg0);
- }
- return result;
- }
-
- public boolean retainAll(Collection arg0) {
- boolean result = false;
-
- Iterator it = collections.iterator();
- while (it.hasNext()){
- FeatureCollection col = (FeatureCollection)it.next();
- result |= col.removeAll(arg0);
- }
-
- return result;
- }
-
- public Object[] toArray(Object[] arg0) {
- List list = new ArrayList();
-
+ public <T> T[] toArray(T[] arg0) {
+ List<T> list = new ArrayList<T>();
Iterator it = collections.iterator();
while(it.hasNext()){
FeatureCollection col = (FeatureCollection)it.next();
- Iterator it2 = col.iterator();
- while (it2.hasNext()){
- list.add(it.next());
+ FeatureIterator it2 = col.features();
+ try {
+ while (it2.hasNext()){
+ list.add((T)it.next());
+ }
+ }
+ finally {
+ it2.close();
}
- col.close(it2);
}
return list.toArray(arg0);
@@ -19,6 +19,7 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
+import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -55,15 +56,25 @@ public static EObject FeatureCollectionType_parse(EObject fct, ElementInstance i
//check for an array
SimpleFeature[] featureMembers = (SimpleFeature[]) node.getChildValue(SimpleFeature[].class);
if (featureMembers != null) {
- for (int i = 0; i < featureMembers.length; i++) {
- fc.add(featureMembers[i]);
+ if( fc instanceof Collection){
+ for (int i = 0; i < featureMembers.length; i++) {
+ ((Collection)fc).add(featureMembers[i]);
+ }
+ }
+ else {
+ throw new IllegalStateException("Require DefaultFeatureCollection or ListFeatureCollection");
}
}
else {
//gml:featureMember
- List<SimpleFeature> featureMember = node.getChildValues( SimpleFeature.class );
- for (SimpleFeature f : featureMember ) {
- fc.add( f );
+ if( fc instanceof Collection){
+ List<SimpleFeature> featureMember = node.getChildValues( SimpleFeature.class );
+ for (SimpleFeature f : featureMember ) {
+ ((Collection)fc).add( f );
+ }
+ }
+ else {
+ throw new IllegalStateException("Require DefaultFeatureCollection or ListFeatureCollection");
}
}
@@ -44,7 +44,7 @@
@Override
protected void setUp() throws Exception {
fc = WfsFactory.eINSTANCE.createFeatureCollectionType();
- FeatureCollection features = new DefaultFeatureCollection(null,null);
+ DefaultFeatureCollection features = new DefaultFeatureCollection();
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setName( "feature" );
@@ -95,7 +95,7 @@ public void testEncodeFeatureCollection11() throws Exception {
}
public void testEncodeFeatureCollectionMultipleFeatureTypes() throws Exception {
- FeatureCollection features = new DefaultFeatureCollection(null,null);
+ DefaultFeatureCollection features = new DefaultFeatureCollection();
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setName( "feature2" );
@@ -371,7 +371,7 @@ public void _testParseGetFeature() throws Exception {
assertTrue(f.getAttribute("dateProperty") instanceof Date);
assertEquals(BigDecimal.valueOf(5.03), f.getAttribute("decimalProperty"));
} finally {
- featureCollection.close(features);
+ features.close();
}
}
@@ -368,12 +368,12 @@ public boolean contains( Object o ) {
return toArray( new SimpleFeature[ size() ]);
}
- public Object[] toArray( Object[] array ) {
- List list = new ArrayList();
+ public <T> T[] toArray( T[] array ) {
+ List<T> list = new ArrayList<T>();
Iterator i = iterator();
try {
while( i.hasNext() ){
- list.add( i.next() );
+ list.add( (T) i.next() );
}
}
finally {

0 comments on commit 3cfbb24

Please sign in to comment.