| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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.trans.XPathException; |
| 13 |
import net.sf.saxon.value.EmptySequence; |
| 14 |
|
| 15 |
import java.util.function.Consumer; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class SingletonIterator<T extends Item<?>> implements SequenceIterator<T>, UnfailingIterator<T>, |
| 23 |
ReversibleIterator<T>, LastPositionFinder, GroundedIterator<T>, LookaheadIterator<T> { |
| 24 |
|
| 25 |
private T item; |
| 26 |
boolean gone = false; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private SingletonIterator(T value) { |
| 35 |
this.item = value; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static <T extends Item<?>> UnfailingIterator<T> makeIterator(T item) { |
| 48 |
if (item == null) { |
| 49 |
return EmptyIterator.emptyIterator(); |
| 50 |
} else { |
| 51 |
return new SingletonIterator<>(item); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static <T extends Item<?>> SingletonIterator<T> rawIterator(T item) { |
| 64 |
assert item != null; |
| 65 |
return new SingletonIterator<>(item); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public boolean hasNext() { |
| 78 |
return !gone; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public T next() { |
| 83 |
if (gone) { |
| 84 |
return null; |
| 85 |
} else { |
| 86 |
gone = true; |
| 87 |
return item; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public int getLength() { |
| 92 |
return 1; |
| 93 |
} |
| 94 |
|
| 95 |
public void close() { |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
public SingletonIterator<T> getReverseIterator() { |
| 100 |
return new SingletonIterator<>(item); |
| 101 |
} |
| 102 |
|
| 103 |
public Item getValue() { |
| 104 |
return item; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public GroundedValue<T> materialize() { |
| 117 |
if (item instanceof GroundedValue) { |
| 118 |
|
| 119 |
return (GroundedValue<T>)item; |
| 120 |
} else { |
| 121 |
return new ZeroOrOne<>(null); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public GroundedValue<T> getResidue() { |
| 127 |
return gone ? EmptySequence.getInstance() : materialize(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public int getProperties() { |
| 141 |
return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void forEach(Consumer<T> consumer) { |
| 146 |
consumer.accept(item); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void forEachOrFail(ItemConsumer<T> consumer) throws XPathException { |
| 151 |
if (!gone) { |
| 152 |
consumer.accept(item); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|