| 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 | |
| 12 | /** |
| 13 | * A Java Iterator which applies a mapping function to each item in an input sequence |
| 14 | * @param <S> the type of the input items |
| 15 | * @param <T> the type of the delivered item |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | public class MappingJavaIterator<S, T> implements Iterator<T> { |
| 20 | |
| 21 | private Iterator<S> input; |
| 22 | private java.util.function.Function<S, T> mapper; |
| 23 | |
| 24 | /** |
| 25 | * Create a mapping iterator |
| 26 | * @param in the input sequence |
| 27 | * @param mapper the mapping function to be applied to each item in the ipnut sequence to |
| 28 | * generate the corresponding item in the result sequence |
| 29 | */ |
| 30 | |
| 31 | public MappingJavaIterator(Iterator<S> in, java.util.function.Function<S, T> mapper) { |
| 32 | this.input = in; |
| 33 | this.mapper = mapper; |
| 34 | } |
| 35 | |
| 36 | public boolean hasNext() { |
| 37 | return input.hasNext(); |
| 38 | } |
| 39 | |
| 40 | public T next() { |
| 41 | while (true) { |
| 42 | T next = mapper.apply(input.next()); |
| 43 | if (next != null) { |
| 44 | return next; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public void remove() { |
| 50 | input.remove(); |
| 51 | } |
| 52 | |
| 53 | } |
| 54 |