| 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.GroundedValue; |
| 12 | import net.sf.saxon.value.AtomicValue; |
| 13 | import net.sf.saxon.value.EmptySequence; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * SingletonIterator: an iterator over a sequence of zero or one values |
| 18 | */ |
| 19 | |
| 20 | public class SingleAtomicIterator implements AtomicIterator, |
| 21 | ReversibleIterator<AtomicValue>, LastPositionFinder, |
| 22 | GroundedIterator<AtomicValue>, LookaheadIterator<AtomicValue> { |
| 23 | |
| 24 | private AtomicValue item; |
| 25 | boolean gone = false; |
| 26 | |
| 27 | /** |
| 28 | * Private constructor: external classes should use the factory method |
| 29 | * |
| 30 | * @param value the item to iterate over |
| 31 | */ |
| 32 | |
| 33 | public SingleAtomicIterator(AtomicValue value) { |
| 34 | if (value == null) { |
| 35 | throw new NullPointerException(); |
| 36 | } |
| 37 | this.item = value; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Determine whether there are more items to come. Note that this operation |
| 42 | * is stateless and it is not necessary (or usual) to call it before calling |
| 43 | * next(). It is used only when there is an explicit need to tell if we |
| 44 | * are at the last element. |
| 45 | * |
| 46 | * @return true if there are more items |
| 47 | */ |
| 48 | |
| 49 | public boolean hasNext() { |
| 50 | return !gone; |
| 51 | } |
| 52 | |
| 53 | /*@Nullable*/ |
| 54 | public AtomicValue next() { |
| 55 | if (gone) { |
| 56 | return null; |
| 57 | } else { |
| 58 | gone = true; |
| 59 | return item; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public int getLength() { |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | public void close() { |
| 68 | } |
| 69 | |
| 70 | /*@NotNull*/ |
| 71 | public SingleAtomicIterator getReverseIterator() { |
| 72 | return new SingleAtomicIterator(item); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Return a Value containing all the items in the sequence returned by this |
| 77 | * SequenceIterator |
| 78 | * |
| 79 | * @return the corresponding Value. If the value is a closure or a function call package, it will be |
| 80 | * evaluated and expanded. |
| 81 | */ |
| 82 | |
| 83 | /*@NotNull*/ |
| 84 | public GroundedValue<AtomicValue> materialize() { |
| 85 | return item; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public GroundedValue<AtomicValue> getResidue() { |
| 90 | return gone ? EmptySequence.getInstance() : item; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get properties of this iterator, as a bit-significant integer. |
| 95 | * |
| 96 | * @return the properties of this iterator. This will be some combination of |
| 97 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 98 | * and {@link #LOOKAHEAD}. It is always |
| 99 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 100 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 101 | */ |
| 102 | |
| 103 | public int getProperties() { |
| 104 | return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD; |
| 105 | } |
| 106 | |
| 107 | } |
| 108 |