| 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.iter; |
| 9 | |
| 10 | import net.sf.saxon.expr.LastPositionFinder; |
| 11 | import net.sf.saxon.om.Item; |
| 12 | import net.sf.saxon.om.SequenceIterator; |
| 13 | |
| 14 | import java.util.List; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * ReverseListIterator is used to enumerate items held in an array in reverse order. |
| 19 | * |
| 20 | * @author Michael H. Kay |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | public class ReverseListIterator<T extends Item<?>> implements UnfailingIterator<T>, |
| 25 | ReversibleIterator<T>, LookaheadIterator<T>, LastPositionFinder { |
| 26 | |
| 27 | List<T> items; |
| 28 | int index = 0; |
| 29 | int start; |
| 30 | int end; // item after the last to be output |
| 31 | |
| 32 | /** |
| 33 | * Create an iterator a slice of an array |
| 34 | * |
| 35 | * @param items The list of items |
| 36 | */ |
| 37 | |
| 38 | public ReverseListIterator(List<T> items) { |
| 39 | this.items = items; |
| 40 | index = items.size() - 1; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Determine whether there are more items to come. Note that this operation |
| 46 | * is stateless and it is not necessary (or usual) to call it before calling |
| 47 | * next(). It is used only when there is an explicit need to tell if we |
| 48 | * are at the last element. |
| 49 | * |
| 50 | * @return true if there are more items in the sequence |
| 51 | */ |
| 52 | |
| 53 | public boolean hasNext() { |
| 54 | return index >= 0; |
| 55 | } |
| 56 | |
| 57 | /*@Nullable*/ |
| 58 | public T next() { |
| 59 | if (index >= 0) { |
| 60 | return items.get(index--); |
| 61 | } else { |
| 62 | return null; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public int getLength() { |
| 67 | return items.size(); |
| 68 | } |
| 69 | |
| 70 | public void close() { |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get properties of this iterator, as a bit-significant integer. |
| 75 | * |
| 76 | * @return the properties of this iterator. This will be some combination of |
| 77 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 78 | * and {@link #LOOKAHEAD}. It is always |
| 79 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 80 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 81 | */ |
| 82 | |
| 83 | public int getProperties() { |
| 84 | return LAST_POSITION_FINDER; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get an iterator that processes the same items in reverse order. |
| 89 | * Since this iterator is processing the items backwards, this method |
| 90 | * returns an ArrayIterator that processes them forwards. |
| 91 | * |
| 92 | * @return a new ArrayIterator |
| 93 | */ |
| 94 | |
| 95 | public SequenceIterator<T> getReverseIterator() { |
| 96 | return new ListIterator<>(items); |
| 97 | } |
| 98 | } |
| 99 |