Debian Codegraph @

net/sf/saxon/expr/sort/GroupAdjacentIterator.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.sort;
9
10 import com.saxonica.ee.stream.ManualGroupIterator;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.LastPositionFinder;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.functions.Count;
15 import net.sf.saxon.lib.StringCollator;
16 import net.sf.saxon.om.*;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.iter.ListIterator;
19 import net.sf.saxon.tree.iter.LookaheadIterator;
20 import net.sf.saxon.value.AtomicValue;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26 * A GroupAdjacentIterator iterates over a sequence of groups defined by
27 * xsl:for-each-group group-adjacent="x". The groups are returned in
28 * order of first appearance.
29 * <p>Each step of this iterator advances to the first item of the next group,
30 * leaving the members of that group in a saved list.</p>
31 */
32
33 public class GroupAdjacentIterator implements GroupIterator, LastPositionFinder, LookaheadIterator<Item<?>> {
34
35 private Expression select;
36 private FocusIterator population;
37 private Expression keyExpression;
38 private StringCollator collator;
39 private XPathContext baseContext;
40 private XPathContext runningContext;
41 private List<AtomicMatchKey> currentComparisonKey;
42 private AtomicSequence currentKey;
43 private List<Item<?>> currentMembers;
44 private List<AtomicMatchKey> nextComparisonKey;
45 private List<AtomicValue> nextKey = null;
46 private Item next;
47 private Item current = null;
48 private int position = 0;
49 private boolean composite = false;
50
51 public GroupAdjacentIterator(Expression select, Expression keyExpression,
52 XPathContext baseContext, StringCollator collator, boolean composite)
53 throws XPathException {
54 this.select = select;
55 this.keyExpression = keyExpression;
56 this.baseContext = baseContext;
57 this.runningContext = baseContext.newMinorContext();
58 this.population = runningContext.trackFocus(select.iterate(baseContext));
59 this.collator = collator;
60 this.composite = composite;
61 next = population.next();
62 if (next != null) {
63 nextKey = getKey(runningContext);
64 nextComparisonKey = getComparisonKey(nextKey, baseContext);
65 }
66 }
67
68 @Override
69 public int getLength() throws XPathException {
70 GroupAdjacentIterator another = new GroupAdjacentIterator(select, keyExpression, baseContext, collator, composite);
71 return Count.steppingCount(another);
72 }
73
74 private List<AtomicValue> getKey(XPathContext context) throws XPathException {
75 List<AtomicValue> key = new ArrayList<>();
76 SequenceIterator iter = keyExpression.iterate(context);
77 while (true) {
78 AtomicValue val = (AtomicValue) iter.next();
79 if (val == null) {
80 break;
81 }
82 key.add(val);
83 }
84 return key;
85 }
86
87 private List<AtomicMatchKey> getComparisonKey(List<AtomicValue> key, XPathContext keyContext) throws XPathException {
88 List<AtomicMatchKey> ckey = new ArrayList<>(key.size());
89 for (AtomicValue aKey : key) {
90 AtomicMatchKey comparisonKey;
91 if (aKey.isNaN()) {
92 comparisonKey = AtomicMatchKey.NaN_MATCH_KEY;
93 } else {
94 comparisonKey = aKey.getXPathComparable(false, collator, keyContext.getImplicitTimezone());
95 }
96 ckey.add(comparisonKey);
97 }
98 return ckey;
99 }
100
101 private void advance() throws XPathException {
102 currentMembers = new ArrayList<>(20);
103 currentMembers.add(current);
104 while (true) {
105 Item nextCandidate = population.next();
106 if (nextCandidate == null) {
107 break;
108 }
109 List<AtomicValue> newKey = getKey(runningContext);
110 List<AtomicMatchKey> newComparisonKey = getComparisonKey(newKey, baseContext);
111
112 try {
113 if (currentComparisonKey.equals(newComparisonKey)) {
114 currentMembers.add(nextCandidate);
115 } else {
116 next = nextCandidate;
117 nextComparisonKey = newComparisonKey;
118 nextKey = newKey;
119 return;
120 }
121 } catch (ClassCastException e) {
122 String message = "Grouping key values are of non-comparable types";
123 XPathException err = new XPathException(message);
124 err.setIsTypeError(true);
125 err.setXPathContext(runningContext);
126 throw err;
127 }
128 }
129 next = null;
130 nextKey = null;
131 }
132
133 public AtomicSequence getCurrentGroupingKey() {
134 return currentKey;
135 }
136
137 public SequenceIterator<?> iterateCurrentGroup() {
138 return new ListIterator<>(currentMembers);
139 }
140
141 public boolean hasNext() {
142 return next != null;
143 }
144
145 public Item next() throws XPathException {
146 if (next == null) {
147 current = null;
148 position = -1;
149 return null;
150 }
151 current = next;
152 if (nextKey.size() == 1) {
153 currentKey = nextKey.get(0);
154 } else {
155 currentKey = new AtomicArray(nextKey);
156 }
157 currentComparisonKey = nextComparisonKey;
158 position++;
159 advance();
160 return current;
161 }
162
163 public void close() {
164 population.close();
165 }
166
167 /**
168 * Get properties of this iterator, as a bit-significant integer.
169 *
170 * @return the properties of this iterator. This will be some combination of
171 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
172 * and {@link #LOOKAHEAD}. It is always
173 * acceptable to return the value zero, indicating that there are no known special properties.
174 * It is acceptable for the properties of the iterator to change depending on its state.
175 */
176
177 public int getProperties() {
178 return LOOKAHEAD | LAST_POSITION_FINDER;
179 }
180
181
182 //#if EE==true
183
184 public ManualGroupIterator getSnapShot(XPathContext context) {
185 return new ManualGroupAdjacentIterator();
186 }
187
188 private class ManualGroupAdjacentIterator extends ManualGroupIterator {
189
190 List<Item<?>> cMembers = currentMembers;
191 XPathContext savedcontext = runningContext.newMinorContext();
192
193 ManualGroupAdjacentIterator() {
194 super(current, position);
195 setCurrentGroupingKey(currentKey);
196 setLastPositionFinder(() -> savedcontext.getLast());
197 }
198
199 public SequenceIterator<Item<?>> iterateCurrentGroup() {
200 return new ListIterator<>(cMembers);
201 }
202
203 }
204 //#endif
205
206 }
207