KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > xquery > util > DistinctValues


1 package gnu.xquery.util;
2 import gnu.lists.*;
3 import gnu.mapping.*;
4 import gnu.kawa.util.*;
5 import gnu.xml.*;
6 import gnu.kawa.xml.*;
7 import gnu.math.*;
8
9 public class DistinctValues
10 {
11   public static void distinctValues$X (Object JavaDoc values, NamedCollator coll,
12                                        CallContext ctx)
13   {
14     DistinctValuesConsumer out
15       = new DistinctValuesConsumer(coll, ctx.consumer);
16     Values.writeValues(values, out);
17   }
18
19   /*
20   public void apply (CallContext ctx) throws Throwable
21   {
22     Object arg = ctx.getNextArg();
23     Object collation = ctx.getNextArg(null);
24     ctx.lastArg();
25     DistinctValuesConsumer out
26       = new DistinctValuesConsumer(ctx.consumer);
27     if (collation != null)
28       {
29     // FIXME
30       }
31     Values.writeValues(arg, out);
32   }
33   */

34 }
35
36 /*
37 class FilterValueConsumer implements Consumer
38 {
39   Convert convert;
40
41   public Consumer append (char v)
42   {
43     writeObject(convert.charToObject(v));
44   }
45   
46   public void writeBoolean (boolean v)
47   {
48     writeObject(convert.booleanToObject(v));
49   }
50
51   public void writeFloat (float v)
52   {
53     writeObject(convert.floatToObject(v));
54   }
55
56   public void writeDouble (double v)
57   {
58     writeObject(convert.doubleToObject(v));
59   }
60
61   public void writeInt (int v)
62   {
63     writeObject(convert.intToObject(v));
64   }
65
66   public void writeLong (long v)
67   {
68     writeObject(convert.longToObject(v));
69   }
70 }
71 */

72
73 class DistinctValuesHashTable extends GeneralHashTable
74 {
75   NamedCollator collator;
76
77   public DistinctValuesHashTable (NamedCollator collator)
78   {
79     this.collator = collator;
80   }
81
82   public int hash (Object JavaDoc key)
83   {
84     if (key == null)
85       return 0;
86     if (key instanceof Number JavaDoc
87         && (key instanceof RealNum || !(key instanceof Numeric)))
88       {
89         int hash = Float.floatToIntBits(((Number JavaDoc) key).floatValue());
90         if (hash == 0x80000000) // Map -0.0 to 0
91
hash = 0;
92         return hash;
93       }
94     // This mostly-works, but isn't reliable FIXME.
95
// One problem is that we ignore collation.
96
return key.hashCode();
97   }
98
99   public boolean matches (Object JavaDoc value1, Object JavaDoc value2)
100   {
101     if (value1 == value2)
102       return true;
103     if (NumberValue.isNaN(value1) && NumberValue.isNaN(value2))
104       return true;
105     return Compare.apply(Compare.LENIENT_EQ, value1, value2, collator);
106   }
107 }
108
109 class DistinctValuesConsumer extends FilterConsumer implements PositionConsumer
110 {
111   DistinctValuesHashTable table;
112
113   public DistinctValuesConsumer (NamedCollator collator, Consumer out)
114   {
115     super(out);
116     table = new DistinctValuesHashTable(collator);
117   }
118
119   public void consume(SeqPosition position)
120   {
121     writeObject(position);
122   }
123
124   public void writePosition(AbstractSequence seq, int ipos)
125   {
126     writeObject(((NodeTree) seq).typedValue(ipos));
127   }
128
129   public void writeBoolean(boolean v)
130   {
131     writeObject(v ? Boolean.TRUE : Boolean.FALSE);
132   }
133
134   public void writeObject (Object JavaDoc value)
135   {
136     if (value instanceof Values)
137       {
138     Values.writeValues(value, this);
139     return;
140       }
141     if (value instanceof KNode)
142       {
143         KNode node = (KNode) value;
144         writeObject(((NodeTree) node.sequence).typedValue(node.ipos));
145         return;
146       }
147     Object JavaDoc old = table.get(value, null);
148     if (old != null)
149       return;
150     table.put(value, value);
151     base.writeObject(value);
152   }
153 }
154
Popular Tags