| 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 single object (typically a sub-expression of an expression) |
| 15 | */ |
| 16 | public class MonoIterator<T> implements Iterator<T> { |
| 17 | |
| 18 | private T thing; // the single object in the collection |
| 19 | private boolean gone; // true if the single object has already been returned |
| 20 | |
| 21 | /** |
| 22 | * Create an iterator of the single object supplied |
| 23 | * |
| 24 | * @param thing the object to be iterated over |
| 25 | */ |
| 26 | |
| 27 | public MonoIterator(T thing) { |
| 28 | gone = false; |
| 29 | this.thing = thing; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Returns <tt>true</tt> if the iteration has more elements. (In other |
| 34 | * words, returns <tt>true</tt> if <tt>next</tt> would return an element |
| 35 | * rather than throwing an exception.) |
| 36 | * |
| 37 | * @return <tt>true</tt> if the iterator has more elements. |
| 38 | */ |
| 39 | |
| 40 | public boolean hasNext() { |
| 41 | return !gone; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns the next element in the iteration. |
| 46 | * |
| 47 | * @return the next element in the iteration. |
| 48 | * @throws NoSuchElementException iteration has no more elements. |
| 49 | */ |
| 50 | |
| 51 | public T next() { |
| 52 | if (gone) { |
| 53 | throw new NoSuchElementException(); |
| 54 | } else { |
| 55 | gone = true; |
| 56 | return thing; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | } |
| 61 |