001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.feature;
006:
007: import com.vividsolutions.jts.geom.Envelope;
008: import org.geotools.data.DataUtilities;
009: import org.geotools.data.store.DataFeatureCollection;
010: import org.geotools.feature.FeatureCollection;
011: import org.geotools.feature.FeatureType;
012: import org.geotools.geometry.jts.ReferencedEnvelope;
013:
014: import java.io.IOException;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: /**
019: * Wraps multiple feature collections into a single.
020: *
021: * @author Justin Deoliveira, The Open Planning Project
022: *
023: */
024: public class CompositeFeatureCollection extends DataFeatureCollection {
025: /**
026: * wrapped collecitons
027: */
028: List collections;
029:
030: public CompositeFeatureCollection(List collections) {
031: this .collections = collections;
032: }
033:
034: protected Iterator openIterator() throws IOException {
035: return new CompositeIterator();
036: }
037:
038: public FeatureType getSchema() {
039: return null;
040: }
041:
042: public ReferencedEnvelope getBounds() {
043: return ReferencedEnvelope.reference(DataUtilities.bounds(this ));
044: }
045:
046: public int getCount() throws IOException {
047: int count = 0;
048: Iterator i = iterator();
049:
050: try {
051: while (i.hasNext()) {
052: i.next();
053: count++;
054: }
055: } finally {
056: close(i);
057: }
058:
059: return count;
060: }
061:
062: class CompositeIterator implements Iterator {
063: int index;
064: Iterator iterator;
065:
066: public CompositeIterator() {
067: index = 0;
068: }
069:
070: public void remove() {
071: }
072:
073: public boolean hasNext() {
074: //is there a current iterator that has another element
075: if ((iterator != null) && iterator.hasNext()) {
076: return true;
077: }
078:
079: //get the next iterator
080: while (index < collections.size()) {
081: //close current before we move to next
082: if (iterator != null) {
083: ((FeatureCollection) collections.get(index - 1))
084: .close(iterator);
085: }
086:
087: //grap next
088: iterator = ((FeatureCollection) collections
089: .get(index++)).iterator();
090:
091: if (iterator.hasNext()) {
092: return true;
093: }
094: }
095:
096: //no more
097: if (iterator != null) {
098: //close the last iterator
099: ((FeatureCollection) collections
100: .get(collections.size() - 1)).close(iterator);
101: }
102:
103: return false;
104: }
105:
106: public Object next() {
107: return iterator.next();
108: }
109: }
110: }
|