Debian Codegraph @

net/sf/saxon/tree/tiny/PrecedingSiblingIterator.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.tree.tiny;
9
10 import net.sf.saxon.om.NodeInfo;
11 import net.sf.saxon.pattern.NodeTest;
12 import net.sf.saxon.tree.iter.AxisIterator;
13
14 import java.util.function.IntPredicate;
15
16 /**
17 * This class supports the preceding-sibling axis.
18 * The starting node must be an element, text node, comment, or processing instruction:
19 * to ensure this, construct the enumeration using NodeInfo#getEnumeration()
20 */
21
22 final class PrecedingSiblingIterator implements AxisIterator {
23
24 private TinyTree document;
25 private TinyNodeImpl startNode;
26 private int nextNodeNr;
27 private NodeTest test;
28 private TinyNodeImpl parentNode;
29 private final IntPredicate matcher;
30
31 PrecedingSiblingIterator(TinyTree doc, /*@NotNull*/ TinyNodeImpl node, NodeTest nodeTest) {
32 document = doc;
33 document.ensurePriorIndex();
34 test = nodeTest;
35 startNode = node;
36 nextNodeNr = node.nodeNr;
37 parentNode = node.parent; // doesn't matter if this is null (unknown)
38 this.matcher = nodeTest.getMatcher(doc);
39 }
40
41 /*@Nullable*/
42 public NodeInfo next() {
43 if (nextNodeNr < 0) {
44 // This check is needed because an errant caller can call next() again after hitting the end of sequence
45 return null;
46 }
47 while (true) {
48 nextNodeNr = document.prior[nextNodeNr];
49 if (nextNodeNr < 0) {
50 return null;
51 }
52 if (matcher.test(nextNodeNr)) {
53 TinyNodeImpl next = document.getNode(nextNodeNr);
54 next.setParentNode(parentNode);
55 return next;
56 }
57 }
58 }
59
60 }
61