| 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.*; |
| 12 | import net.sf.saxon.value.AtomicValue; |
| 13 | import net.sf.saxon.value.EmptySequence; |
| 14 | |
| 15 | /** |
| 16 | * EmptyIterator: an iterator over an empty sequence. Since such an iterator has no state, |
| 17 | * only one instance is required; therefore a singleton instance is available via the static |
| 18 | * getInstance() method. |
| 19 | */ |
| 20 | |
| 21 | public class EmptyIterator<T extends Item<?>> implements SequenceIterator<T>, |
| 22 | ReversibleIterator<T>, LastPositionFinder, GroundedIterator<T>, |
| 23 | LookaheadIterator<T>, UnfailingIterator<T>, AtomizedValueIterator<T> { |
| 24 | |
| 25 | private static EmptyIterator<?> theInstance = new EmptyIterator<>(); |
| 26 | |
| 27 | /** |
| 28 | * Get an EmptyIterator, an iterator over an empty sequence. |
| 29 | * |
| 30 | * @return an EmptyIterator (in practice, this always returns the same |
| 31 | * one) |
| 32 | */ |
| 33 | /*@NotNull*/ |
| 34 | public static EmptyIterator getInstance() { |
| 35 | return theInstance; |
| 36 | } |
| 37 | |
| 38 | @SuppressWarnings("unchecked") |
| 39 | public static <T extends Item<?>> EmptyIterator<T> emptyIterator() { |
| 40 | return (EmptyIterator<T>)theInstance; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Protected constructor |
| 45 | */ |
| 46 | |
| 47 | protected EmptyIterator() { |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Deliver the atomic value that is next in the atomized result |
| 52 | * |
| 53 | * @return the next atomic value |
| 54 | */ |
| 55 | public AtomicSequence nextAtomizedValue() { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the next item. |
| 61 | * |
| 62 | * @return the next item. For the EmptyIterator this is always null. |
| 63 | */ |
| 64 | public T next() { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the position of the last item in the sequence. |
| 70 | * |
| 71 | * @return the position of the last item in the sequence, always zero in |
| 72 | * this implementation |
| 73 | */ |
| 74 | public int getLength() { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get another iterator over the same items, in reverse order. |
| 80 | * |
| 81 | * @return a reverse iterator over an empty sequence (in practice, it |
| 82 | * returns the same iterator each time) |
| 83 | */ |
| 84 | /*@NotNull*/ |
| 85 | public EmptyIterator<T> getReverseIterator() { |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get properties of this iterator, as a bit-significant integer. |
| 91 | * |
| 92 | * @return the properties of this iterator. This will be some combination of |
| 93 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 94 | * and {@link #LOOKAHEAD}. It is always |
| 95 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 96 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 97 | */ |
| 98 | |
| 99 | public int getProperties() { |
| 100 | return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD | ATOMIZING; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return a Value containing all the items in the sequence returned by this |
| 105 | * SequenceIterator. This should be an "in-memory" value, not a Closure. |
| 106 | * |
| 107 | * @return the corresponding Value |
| 108 | */ |
| 109 | |
| 110 | public GroundedValue<T> materialize() { |
| 111 | return EmptySequence.getInstance(); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public GroundedValue<T> getResidue() { |
| 116 | return EmptySequence.getInstance(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Determine whether there are more items to come. Note that this operation |
| 121 | * is stateless and it is not necessary (or usual) to call it before calling |
| 122 | * next(). It is used only when there is an explicit need to tell if we |
| 123 | * are at the last element. |
| 124 | * |
| 125 | * @return true if there are more nodes |
| 126 | */ |
| 127 | |
| 128 | public boolean hasNext() { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * An empty iterator for use where a sequence of nodes is required |
| 134 | */ |
| 135 | |
| 136 | public static class OfNodes extends EmptyIterator<NodeInfo> implements AxisIterator { |
| 137 | |
| 138 | public final static OfNodes THE_INSTANCE = new OfNodes(); |
| 139 | /** |
| 140 | * Get the next item. |
| 141 | * |
| 142 | * @return the next item. For the EmptyIterator this is always null. |
| 143 | */ |
| 144 | public NodeInfo next() { |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * An empty iterator for use where a sequence of atomic values is required |
| 152 | */ |
| 153 | |
| 154 | public static class OfAtomic extends EmptyIterator<AtomicValue> implements AtomicIterator { |
| 155 | |
| 156 | public final static OfAtomic THE_INSTANCE = new OfAtomic(); |
| 157 | |
| 158 | /** |
| 159 | * Get the next item. |
| 160 | * |
| 161 | * @return the next item. For the EmptyIterator this is always null. |
| 162 | */ |
| 163 | public AtomicValue next() { |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | } |
| 168 | |
| 169 | |
| 170 | } |
| 171 |