Debian Codegraph @

net/sf/saxon/expr/ItemMappingIterator.java

raw symbols SCIP
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.expr;
9
10 import net.sf.saxon.om.Item;
11 import net.sf.saxon.om.SequenceIterator;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.tree.iter.LookaheadIterator;
14
15 /**
16 * ItemMappingIterator applies a mapping function to each item in a sequence.
17 * The mapping function either returns a single item, or null (representing an
18 * empty sequence).
19 * <p>This is a specialization of the more general MappingIterator class, for use
20 * in cases where a single input item never maps to a sequence of more than one
21 * output item.</p>
22 */
23
24 public class ItemMappingIterator<F extends Item<?>, T extends Item<?>>
25 implements SequenceIterator<T>, LookaheadIterator<T>, LastPositionFinder {
26
27 private SequenceIterator<? extends F> base;
28 private ItemMappingFunction<F, T> action;
29 private boolean oneToOne = false;
30
31 /**
32 * Construct an ItemMappingIterator that will apply a specified DummyItemMappingFunction to
33 * each Item returned by the base iterator.
34 *
35 * @param base the base iterator
36 * @param action the mapping function to be applied.
37 */
38
39 public ItemMappingIterator(SequenceIterator<F> base, ItemMappingFunction<F, T> action) {
40 this.base = base;
41 this.action = action;
42 }
43
44 /**
45 * Construct an ItemMappingIterator that will apply a specified ItemMappingFunction to
46 * each Item returned by the base iterator.
47 *
48 * @param base the base iterator
49 * @param action the mapping function to be applied
50 * @param oneToOne true if this iterator is one-to-one
51 */
52
53 public ItemMappingIterator(SequenceIterator<? extends F> base, ItemMappingFunction<F, T> action, boolean oneToOne) {
54 this.base = base;
55 this.action = action;
56 this.oneToOne = oneToOne;
57 }
58
59 /**
60 * Say whether this ItemMappingIterator is one-to-one: that is, for every input item, there is
61 * always exactly one output item. The default is false.
62 *
63 * @param oneToOne true if this iterator is one-to-one
64 */
65
66 public void setOneToOne(boolean oneToOne) {
67 this.oneToOne = oneToOne;
68 }
69
70 /**
71 * Ask whether this ItemMappingIterator is one-to-one: that is, for every input item, there is
72 * always exactly one output item. The default is false.
73 *
74 * @return true if this iterator is one-to-one
75 */
76
77 public boolean isOneToOne() {
78 return oneToOne;
79 }
80
81 /**
82 * Get the base (input) iterator
83 * @return the iterator over the input sequence
84 */
85
86 protected SequenceIterator<? extends F> getBaseIterator() {
87 return base;
88 }
89
90 /**
91 * Get the mapping function (the function applied to each item in the input sequence
92 * @return the mapping function
93 */
94
95 protected ItemMappingFunction<F, T> getMappingFunction() {
96 return action;
97 }
98
99 public boolean hasNext() {
100 // Must only be called if this is a lookahead iterator, which will only be true if the base iterator
101 // is a lookahead iterator and one-to-one is true
102 return ((LookaheadIterator) base).hasNext();
103 }
104
105 public T next() throws XPathException {
106 while (true) {
107 F nextSource = base.next();
108 if (nextSource == null) {
109 return null;
110 }
111 // Call the supplied mapping function
112 T current = action.mapItem(nextSource);
113 if (current != null) {
114 return current;
115 }
116 // otherwise go round the loop to get the next item from the base sequence
117 }
118 }
119
120 public void close() {
121 base.close();
122 }
123
124 public int getLength() throws XPathException {
125 // Must only be called if this is a last-position-finder iterator, which will only be true if the base iterator
126 // is a last-position-finder iterator and one-to-one is true
127 return ((LastPositionFinder) base).getLength();
128 }
129
130 /**
131 * Get properties of this iterator, as a bit-significant integer.
132 *
133 * @return the properties of this iterator. This will be some combination of
134 * properties such as {@link net.sf.saxon.om.SequenceIterator#GROUNDED},
135 * {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER},
136 * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always
137 * acceptable to return the value zero, indicating that there are no known special properties.
138 * It is acceptable for the properties of the iterator to change depending on its state.
139 */
140
141 public int getProperties() {
142 if (oneToOne) {
143 return base.getProperties() & (LOOKAHEAD | LAST_POSITION_FINDER);
144 } else {
145 return 0;
146 }
147 }
148 }
149