001: /*
002: * Geotools2 - 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: */
017: package org.geotools.data.feature;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.opengis.filter.Filter;
025:
026: /**
027: *
028: * @author Gabriel Roldan, Axios Engineering
029: * @version $Id: FilteringCollection.java 25884 2007-06-18 15:12:57Z groldan $
030: * @source $URL:
031: * http://svn.geotools.org/geotools/trunk/gt/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/feature/FilteringCollection.java $
032: * @since 2.4
033: */
034: public class FilteringCollection implements Collection {
035:
036: private Collection collection;
037:
038: private Filter filter;
039:
040: public FilteringCollection(Collection collection, Filter filter) {
041: this .collection = collection;
042: this .filter = filter;
043: }
044:
045: public boolean add(Object obj) {
046: throw new UnsupportedOperationException();
047: }
048:
049: public boolean addAll(Collection col) {
050: throw new UnsupportedOperationException();
051: }
052:
053: public void clear() {
054: throw new UnsupportedOperationException();
055: }
056:
057: public boolean contains(Object o) {
058: if (!filter.evaluate(o)) {
059: return false;
060: }
061: boolean internalContains = collection.contains(o);
062: if (!internalContains) {
063: return false;
064: }
065: for (Iterator it = iterator(); it.hasNext();) {
066: Object object = it.next();
067: if (object.equals(o)) {
068: return true;
069: }
070: }
071: return false;
072: }
073:
074: public boolean containsAll(Collection col) {
075:
076: return false;
077: }
078:
079: public boolean isEmpty() {
080: return !iterator().hasNext();
081: }
082:
083: public Iterator iterator() {
084: return new FilteringIterator(collection.iterator(), filter);
085: }
086:
087: public boolean remove(Object o) {
088: throw new UnsupportedOperationException();
089: }
090:
091: public boolean removeAll(Collection arg0) {
092: throw new UnsupportedOperationException();
093: }
094:
095: public boolean retainAll(Collection arg0) {
096: throw new UnsupportedOperationException();
097: }
098:
099: private int size = -1;
100:
101: public int size() {
102: if (size == -1) {
103: int count = 0;
104: for (Iterator it = iterator(); it.hasNext();) {
105: it.next();
106: count++;
107: }
108: size = count;
109: }
110: return size;
111: }
112:
113: public Object[] toArray() {
114: List list = new ArrayList();
115: for (Iterator it = iterator(); it.hasNext();) {
116: list.add(it.next());
117: }
118: return list.toArray();
119: }
120:
121: public Object[] toArray(Object[] destination) {
122: List list = new ArrayList();
123: for (Iterator it = iterator(); it.hasNext();) {
124: list.add(it.next());
125: }
126: return list.toArray(destination);
127: }
128:
129: }
|