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