| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
public class DistinctValues extends CollatingFunctionFixed { |
| 26 |
|
| 27 |
|
| 28 |
public String getStreamerName() { |
| 29 |
return "DistinctValues"; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 82 |
return nextBase; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public void close() { |
| 88 |
base.close(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|