Debian Codegraph @

net/sf/saxon/functions/DistinctValues.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.functions;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.expr.sort.AtomicMatchKey;
12 import net.sf.saxon.lib.StringCollator;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.SequenceIterator;
15 import net.sf.saxon.om.SequenceTool;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.value.AtomicValue;
18
19 import java.util.HashSet;
20
21 /**
22 * The XPath 2.0 distinct-values() function, with the collation argument already known
23 */
24
25 public class DistinctValues extends CollatingFunctionFixed {
26
27
28 public String getStreamerName() {
29 return "DistinctValues";
30 }
31
32
33 /**
34 * Iterator class to return the distinct values in a sequence
35 */
36
37 public static class DistinctIterator implements SequenceIterator<AtomicValue> {
38
39 private SequenceIterator<? extends AtomicValue> base;
40 private StringCollator collator;
41 private XPathContext context;
42 private HashSet<AtomicMatchKey> lookup = new HashSet<>(40);
43
44 /**
45 * Create an iterator over the distinct values in a sequence
46 *
47 * @param base the input sequence. This must return atomic values only.
48 * @param collator The comparer used to obtain comparison keys from each value;
49 * these comparison keys are themselves compared using equals().
50 * @param context the XPath dynamic context
51 */
52
53 public DistinctIterator(SequenceIterator<? extends AtomicValue> base, StringCollator collator, XPathContext context) {
54 this.base = base;
55 this.collator = collator;
56 this.context = context;
57 }
58
59 /**
60 * Get the next item in the sequence. <BR>
61 *
62 * @return the next item, or null if there are no more items.
63 * @throws net.sf.saxon.trans.XPathException
64 * if an error occurs retrieving the next item
65 */
66
67 public AtomicValue next() throws XPathException {
68 int implicitTimezone = context.getImplicitTimezone();
69 while (true) {
70 AtomicValue nextBase = base.next();
71 if (nextBase == null) {
72 return null;
73 }
74 AtomicMatchKey key;
75 if (nextBase.isNaN()) {
76 key = AtomicMatchKey.NaN_MATCH_KEY;
77 } else {
78 key = nextBase.getXPathComparable(false, collator, implicitTimezone);
79 }
80 if (lookup.add(key)) {
81 // returns true if newly added (if not, keep looking)
82 return nextBase;
83 }
84 }
85 }
86
87 public void close() {
88 base.close();
89 }
90
91 /**
92 * Get properties of this iterator, as a bit-significant integer.
93 *
94 * @return the properties of this iterator. This will be some combination of
95 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
96 * and {@link #LOOKAHEAD}. It is always
97 * acceptable to return the value zero, indicating that there are no known special properties.
98 * It is acceptable for the properties of the iterator to change depending on its state.
99 */
100
101 public int getProperties() {
102 return 0;
103 }
104 }
105
106 public Sequence<AtomicValue> call(XPathContext context, Sequence[] arguments) throws XPathException {
107 StringCollator collator = getStringCollator();
108 return SequenceTool.toLazySequence(new DistinctIterator(arguments[0].iterate(), collator, context));
109 }
110
111 }
112