001: package org.geotools.feature.iso.collection;
002:
003: import java.util.Iterator;
004: import java.util.LinkedHashMap;
005: import java.util.List;
006: import java.util.Map;
007: import java.util.NoSuchElementException;
008:
009: import org.opengis.feature.Feature;
010: import org.opengis.feature.simple.SimpleFeatureCollection;
011: import org.opengis.feature.simple.SimpleFeatureCollectionType;
012: import org.opengis.feature.type.Name;
013:
014: /**
015: * Implement a SimpleFeatureCollection by burning memory!
016: * <p>
017: * Contents are maintained in a sorted TreeMap by FID, this serves as a
018: * reference implementation when exploring the FeatureCollection api.
019: * </p>
020: *
021: * @author Jody Garnett, Refractions Research
022: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/fm/src/main/java/org/geotools/feature/iso/collection/MemorySimpleFeatureCollection.java $
023: */
024: public class MemorySimpleFeatureCollection extends
025: AbstractSimpleFeatureCollection implements
026: SimpleFeatureCollection, RandomFeatureAccess {
027:
028: public MemorySimpleFeatureCollection(
029: SimpleFeatureCollectionType type, String id) {
030: super (type, id);
031: }
032:
033: //use LinkedHashMap to preserve iteration order
034: private Map contents = new LinkedHashMap();
035:
036: public int size() {
037: return contents.size();
038: }
039:
040: protected Iterator openIterator() {
041: return new MemoryIterator(contents.values().iterator());
042: }
043:
044: protected void closeIterator(Iterator close) {
045: if (close == null)
046: return;
047:
048: MemoryIterator it = (MemoryIterator) close;
049: it.close();
050: }
051:
052: public boolean add(Object o) {
053: Feature feature = (Feature) o;
054: contents.put(feature.getID(), feature);
055: return true;
056: }
057:
058: class MemoryIterator implements Iterator {
059: Iterator it;
060:
061: MemoryIterator(Iterator iterator) {
062: it = iterator;
063: }
064:
065: public void close() {
066: it = null;
067: }
068:
069: public boolean hasNext() {
070: if (it == null) {
071: throw new IllegalStateException();
072: }
073: return it.hasNext();
074: }
075:
076: public Object next() {
077: if (it == null) {
078: throw new IllegalStateException();
079: }
080: return it.next();
081: }
082:
083: public void remove() {
084: it.remove();
085: }
086: }
087:
088: //
089: // RandomFeatureAccess
090: //
091: public Feature getFeatureMember(String id)
092: throws NoSuchElementException {
093: if (contents.containsKey(id)) {
094: return (Feature) contents.get(id);
095: }
096: throw new NoSuchElementException(id);
097: }
098:
099: public Feature removeFeatureMember(String id) {
100: if (contents.containsKey(id)) {
101: Feature old = (Feature) contents.get(id);
102: contents.remove(id);
103: return old;
104: }
105: return null;
106: }
107:
108: public Object operation(Name arg0, List arg1) {
109: throw new UnsupportedOperationException();
110: }
111:
112: /**
113: * TODO: implement
114: */
115: public List get(Name name) {
116: throw new UnsupportedOperationException();
117: }
118:
119: }
|