Debian Codegraph @

net/sf/saxon/expr/MappingIterator.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
14 /**
15 * MappingIterator merges a sequence of sequences into a single flat
16 * sequence. It takes as inputs an iteration, and a mapping function to be
17 * applied to each Item returned by that iteration. The mapping function itself
18 * returns another iteration. The result is an iteration of the concatenation of all
19 * the iterations returned by the mapping function.
20 * <p>This is a powerful class. It is used, with different mapping functions,
21 * in a great variety of ways. It underpins the way that "for" expressions and
22 * path expressions are evaluated, as well as sequence expressions. It is also
23 * used in the implementation of the document(), key(), and id() functions.</p>
24 */
25
26 public class MappingIterator<F extends Item<?>, T extends Item<?>> implements SequenceIterator<T> {
27
28 private SequenceIterator<F> base;
29 private MappingFunction<? super F, ? extends T> action;
30 private SequenceIterator<? extends T> results = null;
31
32 /**
33 * Construct a MappingIterator that will apply a specified MappingFunction to
34 * each Item returned by the base iterator.
35 *
36 * @param base the base iterator, which must deliver items of type F
37 * @param action the mapping function to be applied
38 */
39
40 public MappingIterator(SequenceIterator<F> base, MappingFunction<? super F, ? extends T> action) {
41 this.base = base;
42 this.action = action;
43 }
44
45 public T next() throws XPathException {
46 T nextItem;
47 while (true) {
48 if (results != null) {
49 nextItem = results.next();
50 if (nextItem != null) {
51 break;
52 } else {
53 results = null;
54 }
55 }
56 F nextSource = base.next();
57 if (nextSource != null) {
58 // Call the supplied mapping function
59 SequenceIterator<? extends T> obj = action.map(nextSource);
60
61 // The result may be null (representing an empty sequence),
62 // or a SequenceIterator (any sequence)
63
64 if (obj != null) {
65 results = obj;
66 nextItem = results.next();
67 if (nextItem == null) {
68 results = null;
69 } else {
70 break;
71 }
72 }
73 // now go round the loop to get the next item from the base sequence
74 } else {
75 results = null;
76 return null;
77 }
78 }
79
80 return nextItem;
81 }
82
83 public void close() {
84 if (results != null) {
85 results.close();
86 }
87 base.close();
88 }
89
90 /**
91 * Get properties of this iterator, as a bit-significant integer.
92 *
93 * @return the properties of this iterator. This will be some combination of
94 * properties such as {@link SequenceIterator#GROUNDED}, {@link SequenceIterator#LAST_POSITION_FINDER},
95 * and {@link SequenceIterator#LOOKAHEAD}. It is always
96 * acceptable to return the value zero, indicating that there are no known special properties.
97 * It is acceptable for the properties of the iterator to change depending on its state.
98 */
99
100 public int getProperties() {
101 return 0;
102 }
103
104 }
105