001: package org.geotools.feature.iso.collection;
002:
003: import java.util.Iterator;
004: import java.util.List;
005: import java.util.ListIterator;
006: import java.util.NoSuchElementException;
007:
008: import org.geotools.data.collection.ResourceCollection;
009: import org.geotools.data.collection.ResourceList;
010:
011: /**
012: * Simple SubList based on from, to index.
013: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/fm/src/main/java/org/geotools/feature/iso/collection/SubResourceList.java $
014: */
015: public class SubResourceList extends AbstractResourceList implements
016: ResourceCollection, List {
017: ResourceList collection;
018: int fromIndex;
019: int toIndex;
020:
021: public SubResourceList(ResourceList collection, int from, int to) {
022: this .collection = collection;
023: this .fromIndex = from;
024: this .toIndex = to;
025: }
026:
027: public Object get(int index) {
028: return collection.get(index - fromIndex);
029: }
030:
031: public int size() {
032: return toIndex - fromIndex;
033: }
034:
035: protected Iterator openIterator() {
036: return openIterator(0);
037: }
038:
039: public ListIterator openIterator(int index) {
040: return new SubResourceListIterator(index);
041: }
042:
043: protected void closeIterator(Iterator close) {
044: if (close == null)
045: return;
046: if (close instanceof SubResourceListIterator) {
047: SubResourceListIterator it = (SubResourceListIterator) close;
048: collection.close(it.delegate);
049: }
050: open.remove(close);
051: }
052:
053: private class SubResourceListIterator implements ListIterator {
054: ListIterator delegate;
055:
056: SubResourceListIterator(int index) {
057: delegate = collection.listIterator(index + fromIndex);
058: }
059:
060: public boolean hasNext() {
061: return delegate.nextIndex() < toIndex;
062: }
063:
064: public Object next() {
065: if (delegate.nextIndex() >= toIndex)
066: throw new NoSuchElementException();
067: return delegate.next();
068: }
069:
070: public void remove() {
071: delegate.remove();
072: toIndex--;
073: }
074:
075: public boolean hasPrevious() {
076: return delegate.previousIndex() > fromIndex;
077: }
078:
079: public Object previous() {
080: if (delegate.nextIndex() < fromIndex)
081: throw new NoSuchElementException();
082: return delegate.next();
083: }
084:
085: public int nextIndex() {
086: return delegate.nextIndex() + fromIndex;
087: }
088:
089: public int previousIndex() {
090: return delegate.previousIndex() + fromIndex;
091: }
092:
093: public void set(Object o) {
094: delegate.set(o);
095: }
096:
097: public void add(Object o) {
098: delegate.add(o);
099: toIndex++;
100: }
101: }
102:
103: // Overrides for speed
104: public void clear() {
105: collection.removeRange(fromIndex, toIndex);
106: }
107: }
|