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