KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > internal > GroupByExpressionComparator


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.internal;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import org.josql.expressions.Expression;
24
25 import org.josql.Query;
26 import org.josql.QueryResults;
27
28 public class GroupByExpressionComparator implements Comparator JavaDoc
29 {
30
31     private List JavaDoc items = new ArrayList JavaDoc ();
32     private Query q = null;
33     private int size = 0;
34     private int count = 0;
35     private Exception JavaDoc exp = null;
36     private Object JavaDoc nullObj = new Object JavaDoc ();
37
38     private Map JavaDoc cache = new HashMap JavaDoc ();
39     private boolean caching = false;
40
41     public GroupByExpressionComparator (Query q,
42                     boolean caching)
43     {
44
45     this.q = q;
46     this.caching = caching;
47
48     }
49
50     public int getCount ()
51     {
52
53     return this.count;
54
55     }
56
57     public boolean equals (Object JavaDoc o)
58     {
59
60     throw new UnsupportedOperationException JavaDoc ("Not supported for instances of: " +
61                          this.getClass ().getName ());
62
63     }
64
65     public boolean isCaching ()
66     {
67
68     return this.caching;
69
70     }
71
72     public void setCaching (boolean b)
73     {
74
75     this.caching = b;
76
77     }
78
79     public void clearCache ()
80     {
81
82     this.cache.clear ();
83
84     }
85
86     public int ci (Object JavaDoc o1,
87            Object JavaDoc o2)
88                throws Exception JavaDoc
89     {
90
91     List JavaDoc lo1 = (List JavaDoc) o1;
92     List JavaDoc lo2 = (List JavaDoc) o2;
93     
94     QueryResults qr = this.q.getQueryResults ();
95
96     for (int i = 0; i < this.size; i++)
97     {
98
99         Item it = (Item) this.items.get (i);
100
101         // The "current object" here will be a list, it is also the "key"
102
// to the group by results.
103
this.q.setAllObjects ((List JavaDoc) qr.getGroupByResults ().get (lo1));
104
105         this.q.setCurrentGroupByObjects (this.q.getAllObjects ());
106
107         // Also, setup the save values.
108
this.q.setSaveValues ((Map JavaDoc) qr.getGroupBySaveValues (lo1));
109
110         this.q.setCurrentObject (o1);
111
112         Object JavaDoc eo1 = null;
113
114         if (it.exp != null)
115         {
116
117         eo1 = it.exp.getValue (o1,
118                        this.q);
119
120         } else {
121
122         eo1 = lo1.get (it.ind);
123
124         }
125
126         this.q.setAllObjects ((List JavaDoc) qr.getGroupByResults ().get (lo2));
127
128         // Also, setup the save values.
129
this.q.setSaveValues ((Map JavaDoc) qr.getGroupBySaveValues (lo2));
130
131         this.q.setCurrentObject (o2);
132         
133         Object JavaDoc eo2 = null;
134
135         if (it.exp != null)
136         {
137
138         eo2 = it.exp.getValue (o2,
139                        this.q);
140
141         } else {
142
143         eo2 = lo2.get (it.ind);
144
145         }
146
147         // Compare them...
148
int c = Utilities.compare (eo1,
149                        eo2);
150
151         if (c == 0)
152         {
153
154         // Go to the next...
155
continue;
156
157         }
158
159         // For speed reasons, 1 is used here rather than the constant.
160
if (it.dir == 1)
161         {
162
163         c = -1 * c;
164
165         }
166
167         return c;
168
169     }
170
171     return 0;
172
173     }
174
175     public int cic (Object JavaDoc o1,
176             Object JavaDoc o2)
177                 throws Exception JavaDoc
178     {
179
180     this.count++;
181
182     Map JavaDoc co = null;
183     boolean get = true;
184     Item it = null;
185     Object JavaDoc eo1 = null;
186     Object JavaDoc eo2 = null;
187
188     QueryResults qr = this.q.getQueryResults ();
189     List JavaDoc lo1 = (List JavaDoc) o1;
190     List JavaDoc lo2 = (List JavaDoc) o2;
191
192     for (int i = 0; i < this.size; i++)
193     {
194
195         it = (Item) this.items.get (i);
196
197         eo1 = null;
198
199         get = true;
200
201         co = (Map JavaDoc) cache.get (o1);
202
203         if (co == null)
204         {
205
206         co = new HashMap JavaDoc (this.size);
207
208         cache.put (o1,
209                co);
210
211         get = false;
212
213         }
214
215         if (get)
216         {
217
218         eo1 = co.get (it);
219
220         if (eo1 == this.nullObj)
221         {
222
223             eo1 = null;
224
225         }
226
227         }
228         
229         if (eo1 == null)
230         {
231
232         this.q.setAllObjects ((List JavaDoc) qr.getGroupByResults ().get (lo1));
233
234         // Also, setup the save values.
235
this.q.setSaveValues ((Map JavaDoc) qr.getGroupBySaveValues (lo1));
236
237         this.q.setCurrentObject (o1);
238
239         if (it.exp != null)
240         {
241
242             eo1 = it.exp.getValue (o1,
243                        this.q);
244
245         } else {
246
247             eo1 = lo1.get (it.ind - 1);
248
249         }
250
251         co.put (it,
252             eo1);
253
254         }
255
256         get = true;
257
258         eo2 = null;
259
260         co = (Map JavaDoc) cache.get (o2);
261
262         if (co == null)
263         {
264
265         co = new HashMap JavaDoc (this.size);
266
267         cache.put (o2,
268                co);
269
270         get = false;
271
272         }
273
274         if (get)
275         {
276
277         eo2 = co.get (it);
278
279         }
280
281         if (eo2 == null)
282         {
283
284         this.q.setAllObjects ((List JavaDoc) qr.getGroupByResults ().get (lo2));
285
286         // Also, setup the save values.
287
this.q.setSaveValues ((Map JavaDoc) qr.getGroupBySaveValues (lo2));
288
289         this.q.setCurrentObject (o2);
290
291         if (it.exp != null)
292         {
293
294             eo2 = it.exp.getValue (o2,
295                        this.q);
296
297         } else {
298
299             eo2 = lo2.get (it.ind - 1);
300
301         }
302         
303         co.put (it,
304             eo2);
305
306         }
307         
308         // Compare them...
309
int c = Utilities.compare (eo1,
310                        eo2);
311
312         if (c == 0)
313         {
314
315         // Go to the next...
316
continue;
317
318         }
319
320         // For speed reasons, 1 is used here rather than the constant.
321
if (it.dir == 1)
322         {
323
324         c = -1 * c;
325
326         }
327
328         return c;
329
330     }
331
332     return 0;
333
334     }
335
336     public int compare (Object JavaDoc o1,
337             Object JavaDoc o2)
338     {
339
340     try
341     {
342
343         if (this.caching)
344         {
345
346         return this.cic (o1,
347                  o2);
348
349         } else {
350
351         return this.ci (o1,
352                 o2);
353
354         }
355
356     } catch (Exception JavaDoc e) {
357
358         this.exp = e;
359
360         return 0;
361
362     }
363
364     }
365
366     public Exception JavaDoc getException ()
367     {
368
369     return this.exp;
370
371     }
372
373     public void addSortItem (Expression exp,
374                  int ind,
375                  int dir)
376     {
377
378     Item it = new Item ();
379     it.dir = dir;
380     it.ind = ind;
381     it.exp = exp;
382
383     this.items.add (it);
384
385     this.size = this.items.size ();
386
387     }
388
389     private class Item
390     {
391
392     public int dir = 0;
393     public int ind = 0;
394     public Expression exp = null;
395
396     }
397
398 }
399
Popular Tags