Debian Codegraph @

net/sf/saxon/expr/sort/GroupStartingIterator.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 net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.Count;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.pattern.Pattern;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.LookaheadIterator;
17
18 import java.util.ArrayList;
19
20 /**
21 * A GroupStartingIterator iterates over a sequence of groups defined by
22 * xsl:for-each-group group-starting-with="x". The groups are returned in
23 * order of first appearance.
24 */
25
26 public class GroupStartingIterator extends GroupMatchingIterator implements LookaheadIterator<Item<?>>, GroupIterator {
27
28 public GroupStartingIterator(Expression select, Pattern startPattern,
29 XPathContext context)
30 throws XPathException {
31 this.select = select;
32 this.pattern = startPattern;
33 baseContext = context;
34 runningContext = context.newMinorContext();
35 this.population = runningContext.trackFocus(select.iterate(context));
36 // the first item in the population always starts a new group
37 next = population.next();
38 }
39
40 @Override
41 public int getLength() throws XPathException {
42 GroupStartingIterator another = new GroupStartingIterator(select, pattern, baseContext);
43 return Count.steppingCount(another);
44 }
45
46 protected void advance() throws XPathException {
47 currentMembers = new ArrayList<>(10);
48 currentMembers.add(current);
49 while (true) {
50 Item nextCandidate = population.next();
51 if (nextCandidate == null) {
52 break;
53 }
54 if (pattern.matches(nextCandidate, runningContext)) {
55 next = nextCandidate;
56 return;
57 } else {
58 currentMembers.add(nextCandidate);
59 }
60 }
61 next = null;
62 }
63
64 }
65