01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data.store;
17:
18: import java.util.Iterator;
19: import java.util.NoSuchElementException;
20:
21: /**
22: * This iterator is used to indicate that contents could not be aquired.
23: * <p>
24: * The normal Collection.iterator() method does not let us return an error
25: * (we always have to return an iterator). However Iterator.next() can
26: * be used to return an NoSuchElementException.
27: * </p>
28: * <p>
29: * So we are basically going to lie, we are going to pretend their is content
30: * *once*, and when they ask for it we are going to hit them with
31: * a NoSuchElementExcetion. This is a mean trick, but it does convey the idea
32: * of asking for content that is supposed to be there and failing to aquire it.
33: * </p>
34: * @author jgarnett
35: * @since 2.1.RC0
36: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/store/NoContentIterator.java $
37: */
38: public class NoContentIterator implements Iterator {
39: Throwable origionalProblem;
40:
41: public NoContentIterator(Throwable t) {
42: origionalProblem = t;
43: }
44:
45: public boolean hasNext() {
46: return origionalProblem != null;
47: }
48:
49: public Object next() {
50: if (origionalProblem == null) {
51: // you only get the real error on the first offense
52: // (after that you are just silly)
53: //
54: throw new NoSuchElementException();
55: }
56: NoSuchElementException cantFind = new NoSuchElementException(
57: "Could not aquire feature:" + origionalProblem);
58: cantFind.initCause(origionalProblem);
59: origionalProblem = null;
60: throw cantFind;
61: }
62:
63: public void remove() {
64: if (origionalProblem == null) {
65: // user did not call next first
66: throw new UnsupportedOperationException();
67: }
68: // User did not call next first
69: throw new IllegalStateException();
70: }
71: }
|