KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 public class ListExpressionComparator implements Comparator JavaDoc
28 {
29
30     private List JavaDoc items = new ArrayList JavaDoc ();
31     private Query q = null;
32     private int size = 0;
33     private int count = 0;
34     private Exception JavaDoc exp = null;
35     private Object JavaDoc nullObj = new Object JavaDoc ();
36
37     private Map JavaDoc cache = new HashMap JavaDoc ();
38     private boolean caching = false;
39
40     public ListExpressionComparator (Query q,
41                      boolean caching)
42     {
43
44     this.q = q;
45     this.caching = caching;
46
47     }
48
49     public int getCount ()
50     {
51
52     return this.count;
53
54     }
55
56     public boolean equals (Object JavaDoc o)
57     {
58
59     throw new UnsupportedOperationException JavaDoc ("Not supported for instances of: " +
60                          this.getClass ().getName ());
61
62     }
63
64     public boolean isCaching ()
65     {
66
67     return this.caching;
68
69     }
70
71     public void setCaching (boolean b)
72     {
73
74     this.caching = b;
75
76     }
77
78     public void clearCache ()
79     {
80
81     this.cache.clear ();
82
83     }
84
85     public int ci (Object JavaDoc o1,
86            Object JavaDoc o2)
87                throws Exception JavaDoc
88     {
89
90     for (int i = 0; i < this.size; i++)
91     {
92
93         Item it = (Item) this.items.get (i);
94
95         this.q.setCurrentObject (o1);
96
97         Object JavaDoc eo1 = it.exp.getValue (o1,
98                       this.q);
99
100         this.q.setCurrentObject (o2);
101         
102         Object JavaDoc eo2 = it.exp.getValue (o2,
103                       this.q);
104
105         // Compare them...
106
int c = Utilities.compare (eo1,
107                        eo2);
108
109         if (c == 0)
110         {
111
112         // Go to the next...
113
continue;
114
115         }
116
117         // For speed reasons, 1 is used here rather than the constant.
118
if (it.dir == 1)
119         {
120
121         c = -1 * c;
122
123         }
124
125         return c;
126
127     }
128
129     return 0;
130
131     }
132
133     public int cic (Object JavaDoc o1,
134             Object JavaDoc o2)
135                 throws Exception JavaDoc
136     {
137
138     this.count++;
139
140     Map JavaDoc co = null;
141     boolean get = true;
142     Item it = null;
143     Object JavaDoc eo1 = null;
144     Object JavaDoc eo2 = null;
145
146     for (int i = 0; i < this.size; i++)
147     {
148
149         it = (Item) this.items.get (i);
150
151         eo1 = null;
152
153         get = true;
154
155         co = (Map JavaDoc) cache.get (o1);
156
157         if (co == null)
158         {
159
160         co = new HashMap JavaDoc (this.size);
161
162         cache.put (o1,
163                co);
164
165         get = false;
166
167         }
168
169         if (get)
170         {
171
172         eo1 = co.get (it);
173
174         if (eo1 == this.nullObj)
175         {
176
177             eo1 = null;
178
179         }
180
181         }
182         
183         if (eo1 == null)
184         {
185
186         this.q.setCurrentObject (o1);
187
188         eo1 = it.exp.getValue (o1,
189                        this.q);
190
191         co.put (it,
192             eo1);
193
194         }
195
196         get = true;
197
198         eo2 = null;
199
200         co = (Map JavaDoc) cache.get (o2);
201
202         if (co == null)
203         {
204
205         co = new HashMap JavaDoc (this.size);
206
207         cache.put (o2,
208                co);
209
210         get = false;
211
212         }
213
214         if (get)
215         {
216
217         eo2 = co.get (it);
218
219         }
220
221         if (eo2 == null)
222         {
223
224         this.q.setCurrentObject (o2);
225
226         eo2 = it.exp.getValue (o2,
227                        this.q);
228         
229         co.put (it,
230             eo2);
231
232         }
233         
234         // Compare them...
235
int c = Utilities.compare (eo1,
236                        eo2);
237
238         if (c == 0)
239         {
240
241         // Go to the next...
242
continue;
243
244         }
245
246         // For speed reasons, 1 is used here rather than the constant.
247
if (it.dir == 1)
248         {
249
250         c = -1 * c;
251
252         }
253
254         return c;
255
256     }
257
258     return 0;
259
260     }
261
262     public int compare (Object JavaDoc o1,
263             Object JavaDoc o2)
264     {
265
266     try
267     {
268
269         if (this.caching)
270         {
271
272         return this.cic (o1,
273                  o2);
274
275         } else {
276
277         return this.ci (o1,
278                 o2);
279
280         }
281
282     } catch (Exception JavaDoc e) {
283
284         this.exp = e;
285
286         return 0;
287
288     }
289
290     }
291
292     public Exception JavaDoc getException ()
293     {
294
295     return this.exp;
296
297     }
298
299     public List JavaDoc getSortItems ()
300     {
301
302     return this.items;
303
304     }
305
306     public void addSortItem (Expression exp,
307                  int dir)
308     {
309
310     Item it = new Item ();
311     it.dir = dir;
312     it.exp = exp;
313
314     this.items.add (it);
315
316     this.size = this.items.size ();
317
318     }
319
320     private class Item
321     {
322
323     public int dir = 0;
324     public Expression exp = null;
325
326     }
327
328 }
329
Popular Tags