001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.memory;
017:
018: import java.util.Iterator;
019: import java.util.NoSuchElementException;
020: import java.util.TreeMap;
021:
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureType;
024: import org.geotools.feature.collection.AbstractFeatureCollection;
025: import org.geotools.feature.collection.FeatureState;
026: import org.geotools.feature.collection.RandomFeatureAccess;
027:
028: /**
029: * Implement a FeatureCollection by burning memory!
030: * <p>
031: * Contents are maintained in a sorted TreeMap by FID, this serves as a reference implementation
032: * when exploring the FeatureCollection api.
033: * </p>
034: * <p>
035: * This is similar to DefaultFeatureCollection, although additional methods are
036: * supported and test cases have been written. Unlike DefaultFeatureCollection
037: * the type information must be known at construction time.
038: * </p>
039: *
040: * @author Jody Garnett, Refractions Research
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/memory/MemoryFeatureCollection.java $
042: */
043: public class MemoryFeatureCollection extends AbstractFeatureCollection
044: implements RandomFeatureAccess {
045: TreeMap contents = new TreeMap();
046:
047: public MemoryFeatureCollection(FeatureType schema) {
048: super (schema);
049: }
050:
051: public int size() {
052: return contents.size();
053: }
054:
055: protected Iterator openIterator() {
056: return new MemoryIterator(contents.values().iterator());
057: }
058:
059: protected void closeIterator(Iterator close) {
060: if (close == null)
061: return;
062:
063: MemoryIterator it = (MemoryIterator) close;
064: it.close();
065: }
066:
067: public boolean add(Object o) {
068: Feature feature = (Feature) o;
069: contents.put(feature.getID(), feature);
070: return true;
071: }
072:
073: class MemoryIterator implements Iterator {
074: Iterator it;
075:
076: MemoryIterator(Iterator iterator) {
077: it = iterator;
078: }
079:
080: public void close() {
081: it = null;
082: }
083:
084: public boolean hasNext() {
085: if (it == null) {
086: throw new IllegalStateException();
087: }
088: return it.hasNext();
089: }
090:
091: public Object next() {
092: if (it == null) {
093: throw new IllegalStateException();
094: }
095: return it.next();
096: }
097:
098: public void remove() {
099: it.remove();
100: }
101: }
102:
103: //
104: // RandomFeatureAccess
105: //
106: public Feature getFeatureMember(String id)
107: throws NoSuchElementException {
108: if (contents.containsKey(id)) {
109: return (Feature) contents.get(id);
110: }
111: throw new NoSuchElementException(id);
112: }
113:
114: public Feature removeFeatureMember(String id) {
115: if (contents.containsKey(id)) {
116: Feature old = (Feature) contents.get(id);
117: contents.remove(id);
118: return old;
119: }
120: return null;
121: };
122: }
|