01: package net.sf.saxon.expr;
02:
03: import java.util.Iterator;
04: import java.util.NoSuchElementException;
05:
06: /**
07: * An iterator over a single object (typically a sub-expression of an expression)
08: */
09: public class MonoIterator implements Iterator {
10:
11: private Object thing; // the single object in the collection
12: private boolean gone; // true if the single object has already been returned
13:
14: public MonoIterator(Object thing) {
15: this .gone = false;
16: this .thing = thing;
17: }
18:
19: /**
20: * Returns <tt>true</tt> if the iteration has more elements. (In other
21: * words, returns <tt>true</tt> if <tt>next</tt> would return an element
22: * rather than throwing an exception.)
23: *
24: * @return <tt>true</tt> if the iterator has more elements.
25: */
26:
27: public boolean hasNext() {
28: return !gone;
29: }
30:
31: /**
32: * Returns the next element in the iteration.
33: *
34: * @return the next element in the iteration.
35: * @exception NoSuchElementException iteration has no more elements.
36: */
37:
38: public Object next() {
39: if (gone) {
40: throw new NoSuchElementException();
41: } else {
42: gone = true;
43: return thing;
44: }
45: }
46:
47: /**
48: *
49: * Removes from the underlying collection the last element returned by the
50: * iterator (optional operation). This method can be called only once per
51: * call to <tt>next</tt>. The behavior of an iterator is unspecified if
52: * the underlying collection is modified while the iteration is in
53: * progress in any way other than by calling this method.
54: *
55: * @exception UnsupportedOperationException if the <tt>remove</tt>
56: * operation is not supported by this Iterator (which is the
57: * case for this iterator).
58: */
59:
60: public void remove() {
61: throw new UnsupportedOperationException();
62: }
63: }
64:
65: //
66: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
67: // you may not use this file except in compliance with the License. You may obtain a copy of the
68: // License at http://www.mozilla.org/MPL/
69: //
70: // Software distributed under the License is distributed on an "AS IS" basis,
71: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
72: // See the License for the specific language governing rights and limitations under the License.
73: //
74: // The Original Code is: all this file.
75: //
76: // The Initial Developer of the Original Code is Michael H. Kay.
77: //
78: // Contributor(s): Michael Kay
79: //
|