001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tool.select;
016:
017: import java.io.IOException;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Set;
021: import java.util.concurrent.CopyOnWriteArraySet;
022:
023: import org.eclipse.core.runtime.IAdaptable;
024: import org.geotools.data.FeatureReader;
025: import org.geotools.feature.CollectionListener;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.feature.FeatureIterator;
028: import org.geotools.feature.FeatureList;
029: import org.geotools.feature.FeatureType;
030: import org.geotools.feature.IllegalAttributeException;
031: import org.geotools.feature.visitor.FeatureVisitor;
032: import org.geotools.filter.Filter;
033: import org.geotools.filter.SortBy;
034: import org.geotools.util.ProgressListener;
035:
036: import com.vividsolutions.jts.geom.Envelope;
037: import com.vividsolutions.jts.geom.Geometry;
038:
039: /**
040: * A feature collection that adapts to other objects.
041: *
042: * @author Jesse
043: * @since 1.1.0
044: */
045: public class AdaptableFeatureCollection implements FeatureCollection,
046: IAdaptable {
047:
048: protected Set<Object> adapters = new CopyOnWriteArraySet<Object>();
049: private final FeatureCollection wrapped;
050:
051: public AdaptableFeatureCollection(final FeatureCollection wrapped) {
052: super ();
053: this .wrapped = wrapped;
054: }
055:
056: @SuppressWarnings("unchecked")
057: public Object getAdapter(Class adapter) {
058: for (Object obj : adapters) {
059: if (adapter.isAssignableFrom(obj.getClass()))
060: return obj;
061: }
062: return null;
063: }
064:
065: public void addAdapter(Object adapter) {
066: adapters.add(adapter);
067: }
068:
069: public boolean removeAdapter(Object adapter) {
070: return adapters.remove(adapter);
071: }
072:
073: public void clearAdapters() {
074: adapters.clear();
075: }
076:
077: public void accepts(FeatureVisitor arg0, ProgressListener arg1)
078: throws IOException {
079: wrapped.accepts(arg0, arg1);
080: }
081:
082: public void addListener(CollectionListener arg0)
083: throws NullPointerException {
084: wrapped.addListener(arg0);
085: }
086:
087: public void close(FeatureIterator arg0) {
088: wrapped.close(arg0);
089: }
090:
091: public void close(Iterator arg0) {
092: wrapped.close(arg0);
093: }
094:
095: public FeatureIterator features() {
096: return wrapped.features();
097: }
098:
099: public FeatureType getFeatureType() {
100: return wrapped.getFeatureType();
101: }
102:
103: public FeatureType getSchema() {
104: return wrapped.getSchema();
105: }
106:
107: public void removeListener(CollectionListener arg0)
108: throws NullPointerException {
109: wrapped.removeListener(arg0);
110: }
111:
112: public FeatureList sort(SortBy arg0) {
113: return wrapped.sort(arg0);
114: }
115:
116: public FeatureCollection subCollection(Filter arg0) {
117: return wrapped.subCollection(arg0);
118: }
119:
120: public Iterator iterator() {
121: return wrapped.iterator();
122: }
123:
124: public void purge() {
125: wrapped.purge();
126: }
127:
128: @SuppressWarnings("unchecked")
129: public boolean add(Object o) {
130: return wrapped.add(o);
131: }
132:
133: @SuppressWarnings("unchecked")
134: public boolean addAll(Collection c) {
135: return wrapped.addAll(c);
136: }
137:
138: public void clear() {
139: wrapped.clear();
140: }
141:
142: public boolean contains(Object o) {
143: return wrapped.contains(o);
144: }
145:
146: @SuppressWarnings("unchecked")
147: public boolean containsAll(Collection c) {
148: return wrapped.containsAll(c);
149: }
150:
151: public boolean isEmpty() {
152: return wrapped.isEmpty();
153: }
154:
155: public boolean remove(Object o) {
156: return wrapped.remove(o);
157: }
158:
159: @SuppressWarnings("unchecked")
160: public boolean removeAll(Collection c) {
161: return wrapped.removeAll(c);
162: }
163:
164: @SuppressWarnings("unchecked")
165: public boolean retainAll(Collection c) {
166: return wrapped.retainAll(c);
167: }
168:
169: public int size() {
170: return wrapped.size();
171: }
172:
173: public Object[] toArray() {
174: return wrapped.toArray();
175: }
176:
177: @SuppressWarnings("unchecked")
178: public Object[] toArray(Object[] a) {
179: return wrapped.toArray(a);
180: }
181:
182: @SuppressWarnings("deprecation")
183: public FeatureCollection collection() throws IOException {
184: return wrapped.collection();
185: }
186:
187: public Envelope getBounds() {
188: return wrapped.getBounds();
189: }
190:
191: @SuppressWarnings("deprecation")
192: public int getCount() throws IOException {
193: return wrapped.getCount();
194: }
195:
196: @SuppressWarnings("deprecation")
197: public FeatureReader reader() throws IOException {
198: return wrapped.reader();
199: }
200:
201: public Object getAttribute(String arg0) {
202: return wrapped.getAttribute(arg0);
203: }
204:
205: public Object getAttribute(int arg0) {
206: return wrapped.getAttribute(arg0);
207: }
208:
209: public Object[] getAttributes(Object[] arg0) {
210: return wrapped.getAttributes(arg0);
211: }
212:
213: public Geometry getDefaultGeometry() {
214: return wrapped.getDefaultGeometry();
215: }
216:
217: public String getID() {
218: return wrapped.getID();
219: }
220:
221: public int getNumberOfAttributes() {
222: return wrapped.getNumberOfAttributes();
223: }
224:
225: @SuppressWarnings("deprecation")
226: public FeatureCollection getParent() {
227: return wrapped.getParent();
228: }
229:
230: public void setAttribute(int arg0, Object arg1)
231: throws IllegalAttributeException,
232: ArrayIndexOutOfBoundsException {
233: wrapped.setAttribute(arg0, arg1);
234: }
235:
236: public void setAttribute(String arg0, Object arg1)
237: throws IllegalAttributeException {
238: wrapped.setAttribute(arg0, arg1);
239: }
240:
241: public void setDefaultGeometry(Geometry arg0)
242: throws IllegalAttributeException {
243: wrapped.setDefaultGeometry(arg0);
244: }
245:
246: public void setParent(FeatureCollection arg0) {
247: wrapped.setParent(arg0);
248: }
249:
250: @Override
251: public int hashCode() {
252: final int PRIME = 31;
253: int result = 1;
254: result = PRIME * result
255: + ((adapters == null) ? 0 : adapters.hashCode());
256: result = PRIME * result
257: + ((wrapped == null) ? 0 : wrapped.hashCode());
258: return result;
259: }
260:
261: @Override
262: public boolean equals(Object obj) {
263: if (this == obj)
264: return true;
265: if (obj == null)
266: return false;
267: if (getClass() != obj.getClass())
268: return false;
269: final AdaptableFeatureCollection other = (AdaptableFeatureCollection) obj;
270: if (adapters == null) {
271: if (other.adapters != null)
272: return false;
273: } else if (!adapters.equals(other.adapters))
274: return false;
275: if (wrapped == null) {
276: if (other.wrapped != null)
277: return false;
278: } else if (!wrapped.equals(other.wrapped))
279: return false;
280: return true;
281: }
282: }
|