Debian Codegraph @

net/sf/saxon/tree/jiter/PairIterator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.tree.jiter;
9
10 import java.util.Iterator;
11 import java.util.NoSuchElementException;
12
13 /**
14 * An iterator over a pair of objects (typically sub-expressions of an expression)
15 */
16 public class PairIterator<T> implements Iterator<T> {
17
18 private T one;
19 private T two;
20 private int pos = 0;
21
22 /**
23 * Create an iterator over two objects
24 *
25 * @param one the first object to be returned
26 * @param two the second object to be returned
27 */
28
29 public PairIterator(T one, T two) {
30 this.one = one;
31 this.two = two;
32 }
33
34 /**
35 * Returns <tt>true</tt> if the iteration has more elements. (In other
36 * words, returns <tt>true</tt> if <tt>next</tt> would return an element
37 * rather than throwing an exception.)
38 *
39 * @return <tt>true</tt> if the iterator has more elements.
40 */
41
42 public boolean hasNext() {
43 return pos < 2;
44 }
45
46 /**
47 * Returns the next element in the iteration.
48 *
49 * @return the next element in the iteration.
50 * @throws NoSuchElementException iteration has no more elements.
51 */
52 public T next() {
53 switch (pos++) {
54 case 0:
55 return one;
56 case 1:
57 return two;
58 default:
59 throw new NoSuchElementException();
60 }
61 }
62
63 }
64