| 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.expr; |
| 9 | |
| 10 | import java.util.Iterator; |
| 11 | import java.util.List; |
| 12 | |
| 13 | /** |
| 14 | * An iterator that combines the results of a sequence of iterators |
| 15 | */ |
| 16 | public class MultiIterator<T> implements Iterator<T> { |
| 17 | |
| 18 | private List<Iterator<T>> array; |
| 19 | private int current; |
| 20 | |
| 21 | /** |
| 22 | * Create an iterator that concatenates a number of supplied iterators |
| 23 | * |
| 24 | * @param array the iterators to be concatenated |
| 25 | */ |
| 26 | |
| 27 | public MultiIterator(List<Iterator<T>> array) { |
| 28 | this.array = array; |
| 29 | current = 0; |
| 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 | while (true) { |
| 42 | if (current >= array.size()) { |
| 43 | return false; |
| 44 | } |
| 45 | if (array.get(current).hasNext()) { |
| 46 | return true; |
| 47 | } |
| 48 | current++; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the next element in the iteration. |
| 54 | * |
| 55 | * @return the next element in the iteration. |
| 56 | * @throws java.util.NoSuchElementException |
| 57 | * iteration has no more elements. |
| 58 | */ |
| 59 | public T next() { |
| 60 | return array.get(current).next(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Removes from the underlying collection the last element returned by the |
| 65 | * iterator (optional operation). This method can be called only once per |
| 66 | * call to <tt>next</tt>. The behavior of an iterator is unspecified if |
| 67 | * the underlying collection is modified while the iteration is in |
| 68 | * progress in any way other than by calling this method. |
| 69 | * |
| 70 | * @throws UnsupportedOperationException if the <tt>remove</tt> |
| 71 | * operation is not supported by this Iterator. |
| 72 | * @throws IllegalStateException if the <tt>next</tt> method has not |
| 73 | * yet been called, or the <tt>remove</tt> method has already |
| 74 | * been called after the last call to the <tt>next</tt> |
| 75 | * method. |
| 76 | */ |
| 77 | public void remove() { |
| 78 | throw new UnsupportedOperationException(); |
| 79 | } |
| 80 | } |
| 81 |