| 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.expr.sort.ItemOrderComparer; |
| 11 | import net.sf.saxon.om.NodeInfo; |
| 12 | import net.sf.saxon.om.SequenceIterator; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | |
| 15 | /** |
| 16 | * An enumeration representing a nodeset that is an intersection of two other NodeSets. |
| 17 | * This implements the XPath 2.0 operator "intersect". |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | public class IntersectionEnumeration implements SequenceIterator<NodeInfo> { |
| 22 | |
| 23 | private SequenceIterator<? extends NodeInfo> e1; |
| 24 | private SequenceIterator<? extends NodeInfo> e2; |
| 25 | /*@Nullable*/ private NodeInfo nextNode1 = null; |
| 26 | private NodeInfo nextNode2 = null; |
| 27 | private ItemOrderComparer comparer; |
| 28 | |
| 29 | /** |
| 30 | * Form an enumeration of the intersection of the nodes in two nodesets |
| 31 | * |
| 32 | * @param p1 the first operand: must be in document order |
| 33 | * @param p2 the second operand: must be in document order |
| 34 | * @param comparer Comparer to be used for putting nodes in document order |
| 35 | * @throws XPathException if an error occurs, for example reading from the input sequence |
| 36 | */ |
| 37 | |
| 38 | public IntersectionEnumeration(SequenceIterator<? extends NodeInfo> p1, SequenceIterator<? extends NodeInfo> p2, |
| 39 | ItemOrderComparer comparer) throws XPathException { |
| 40 | e1 = p1; |
| 41 | e2 = p2; |
| 42 | this.comparer = comparer; |
| 43 | |
| 44 | // move to the first node in each input nodeset |
| 45 | |
| 46 | nextNode1 = next(e1); |
| 47 | nextNode2 = next(e2); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the next item from one of the input sequences, |
| 52 | * checking that it is a node. |
| 53 | * |
| 54 | * @param iter the iterator from which the next item is to be taken |
| 55 | * @return the next value returned by that iterator |
| 56 | * @throws XPathException if a failure occurs reading from the input sequence |
| 57 | */ |
| 58 | |
| 59 | private NodeInfo next(SequenceIterator<? extends NodeInfo> iter) throws XPathException { |
| 60 | return iter.next(); |
| 61 | // rely on type-checking to prevent a ClassCastException |
| 62 | } |
| 63 | |
| 64 | public NodeInfo next() throws XPathException { |
| 65 | // main merge loop: iterate whichever sequence has the lower value, returning when a pair |
| 66 | // is found that match. |
| 67 | |
| 68 | if (nextNode1 == null || nextNode2 == null) { |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | while (nextNode1 != null && nextNode2 != null) { |
| 73 | int c = comparer.compare(nextNode1, nextNode2); |
| 74 | if (c < 0) { |
| 75 | nextNode1 = next(e1); |
| 76 | } else if (c > 0) { |
| 77 | nextNode2 = next(e2); |
| 78 | } else { // keys are equal |
| 79 | NodeInfo current = nextNode2; // which is the same as nextNode1 |
| 80 | nextNode2 = next(e2); |
| 81 | nextNode1 = next(e1); |
| 82 | return current; |
| 83 | } |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | public void close() { |
| 89 | e1.close(); |
| 90 | e2.close(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get properties of this iterator, as a bit-significant integer. |
| 95 | * |
| 96 | * @return the properties of this iterator. This will be some combination of |
| 97 | * properties such as {@link SequenceIterator#GROUNDED}, {@link SequenceIterator#LAST_POSITION_FINDER}, |
| 98 | * and {@link SequenceIterator#LOOKAHEAD}. It is always |
| 99 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 100 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 101 | */ |
| 102 | |
| 103 | public int getProperties() { |
| 104 | return 0; |
| 105 | } |
| 106 | } |
| 107 |