1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.XPathContext; 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.om.SequenceIterator; 5 import net.sf.saxon.sort.AtomicSortComparer; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.value.AtomicValue; 8 9 import java.util.HashSet ; 10 11 14 15 public class DistinctValues extends CollatingFunction { 16 17 20 21 public SequenceIterator iterate(XPathContext context) throws XPathException { 22 SequenceIterator iter = argument[0].iterate(context); 23 return new DistinctIterator(iter, getAtomicSortComparer(1, context)); 24 } 25 26 32 33 protected AtomicSortComparer getAtomicSortComparer(int arg, XPathContext context) throws XPathException { 34 AtomicSortComparer asc = new AtomicSortComparer(getCollator(arg, context, true), context); 35 return asc; 36 } 37 38 41 42 public static class DistinctIterator implements SequenceIterator { 43 44 private SequenceIterator base; 45 private AtomicSortComparer comparer; 46 private int position; 47 private AtomicValue current; 48 private HashSet lookup = new HashSet (40); 49 50 56 57 public DistinctIterator(SequenceIterator base, AtomicSortComparer comparer) { 58 this.base = base; 59 this.comparer = comparer; 60 position = 0; 61 } 62 63 70 71 public Item next() throws XPathException { 72 while (true) { 73 AtomicValue nextBase = (AtomicValue)base.next(); 74 if (nextBase==null) { 75 current = null; 76 position = -1; 77 return null; 78 } 79 AtomicSortComparer.ComparisonKey key = comparer.getComparisonKey(nextBase); 80 if (lookup.contains(key)) { 81 continue; 82 } else { 83 lookup.add(key); 84 current = nextBase; 85 position++; 86 return nextBase; 87 } 88 } 89 } 90 91 100 101 public Item current() { 102 return current; 103 } 104 105 113 114 public int position() { 115 return position; 116 } 117 118 127 128 public SequenceIterator getAnother() throws XPathException { 129 return new DistinctIterator(base.getAnother(), comparer); 130 } 131 132 141 142 public int getProperties() { 143 return 0; 144 } 145 } 146 147 } 148 149 150 151 152 | Popular Tags |