KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > incubator > ObjectIndex


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.incubator;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.Collections JavaDoc;
21
22 import com.gentlyweb.utils.Getter;
23
24 /**
25  * This is an experimental class aimed at producing an index across a
26  * collection of homogeneous objects. It should be noted here that it is the "sorting"
27  * of the elements in the List of objects that takes nearly all the time here.
28  * Use the {@link #sort()} method yourself to only perform sort once. Everytime you
29  * add an object however the sort status of the List of objects will be invalidated and
30  * have to be re-sorted before you have retrieve the objects again.
31  */

32 public class ObjectIndex implements Comparator JavaDoc
33 {
34
35     private List JavaDoc objs = new ArrayList JavaDoc ();
36     private List JavaDoc indices = new ArrayList JavaDoc ();
37     private Class JavaDoc c = null;
38     private int size = -1;
39     private boolean dirty = false;
40     private boolean syncOnAdd = false;
41
42     public ObjectIndex (Class JavaDoc c)
43     {
44
45     this.c = c;
46
47     }
48
49     public boolean isSyncOnAdd ()
50     {
51
52     return this.syncOnAdd;
53
54     }
55
56     public void setSyncOnAdd (boolean v)
57     {
58
59     this.syncOnAdd = v;
60
61     }
62
63     public List JavaDoc getObjects (List JavaDoc keys)
64     {
65
66     if (this.dirty)
67     {
68
69         Collections.sort (this.objs,
70                   this);
71
72         this.dirty = false;
73
74     }
75
76     List JavaDoc res = new ArrayList JavaDoc ();
77
78     int low = 0;
79     int os1 = this.objs.size () - 1;
80
81     int high = os1;
82
83     while (low <= high)
84     {
85
86         int mid = (low + high) / 2;
87
88         Object JavaDoc o = this.objs.get (mid);
89
90         int r = this.compare (o,
91                   keys);
92
93         if (r != 0)
94         {
95
96         // They are not equal, so now determine whether we go "up" or "down".
97
if (r < 0)
98         {
99
100             low = mid + 1;
101
102         } else {
103
104             high = mid - 1;
105
106         }
107
108         } else {
109
110         // Add it to the list...
111
res.add (o);
112
113         int oi = mid;
114
115         // Now we need to check either side for others that may
116
// match.
117
while (mid < os1)
118         {
119
120             mid++;
121
122             o = this.objs.get (mid);
123             
124             r = this.compare (o,
125                       keys);
126             
127             if (r == 0)
128             {
129
130             res.add (o);
131                 
132             } else {
133
134             // Not equal...
135
break;
136
137             }
138
139         }
140
141         mid = oi;
142
143         while (mid > -1)
144         {
145
146             mid--;
147
148             o = this.objs.get (mid);
149             
150             r = this.compare (o,
151                       keys);
152             
153             if (r == 0)
154             {
155
156             res.add (o);
157                 
158             } else {
159
160             // Not equal...
161
break;
162
163             }
164
165         }
166
167         // If we are here then we've got 'em all.
168
break;
169
170         }
171
172     }
173
174     return res;
175
176     }
177
178     public int size ()
179     {
180
181     return this.size;
182
183     }
184
185     public void sort ()
186     {
187
188     Collections.sort (this.objs,
189               this);
190
191     this.dirty = false;
192
193     }
194
195     public void add (String JavaDoc name)
196     {
197
198     if (this.syncOnAdd)
199     {
200
201         Collections.sort (this.objs,
202                   this);
203
204         this.dirty = false;
205
206     } else {
207
208         this.dirty = true;
209
210     }
211
212     this.indices.add (new Getter (name,
213                       this.c));
214
215     }
216
217     public void removeObject (Object JavaDoc o)
218     {
219
220     this.objs.remove (o);
221
222     }
223
224     public void addObject (Object JavaDoc o)
225     {
226
227     if (this.objs.contains (o))
228     {
229
230         return;
231
232     }
233
234     if (this.syncOnAdd)
235     {
236
237         Collections.sort (this.objs,
238                   this);
239
240         this.dirty = false;
241
242     } else {
243
244         this.dirty = true;
245
246     }
247
248     this.objs.add (o);
249
250     this.size = this.objs.size ();
251
252     }
253
254     public int compare (Object JavaDoc o,
255             List JavaDoc keys)
256     {
257
258     try
259     {
260
261         int ks = keys.size ();
262
263         for (int i = 0; i < ks; i++)
264         {
265
266         Getter g = (Getter) this.indices.get (i);
267         
268         Object JavaDoc eo1 = g.getValue (o);
269
270         Object JavaDoc kso = keys.get (i);
271
272         // Can't compare what's not there, return 0.
273
if ((eo1 == null)
274             ||
275             (kso == null)
276            )
277         {
278
279             return 0;
280
281         }
282
283         int v = 0;
284             
285         if (eo1 instanceof Comparable JavaDoc)
286         {
287             
288             // We can use a simple compareTo.
289
Comparable JavaDoc comp = (Comparable JavaDoc) eo1;
290             
291             v = comp.compareTo (kso);
292             
293         } else {
294             
295             v = eo1.toString ().compareTo (kso.toString ());
296             
297         }
298             
299         if (v != 0)
300         {
301             
302             return v;
303             
304         }
305         
306         // They are equal, so need to go to the next field...
307
continue;
308
309         }
310
311     } catch (Exception JavaDoc e) {
312         
313     }
314
315     // All equal.
316
return 0;
317
318     }
319
320     public int compare (Object JavaDoc o1,
321             Object JavaDoc o2)
322     {
323
324     try
325     {
326
327         for (int i = 0; i < this.size; i++)
328         {
329
330         Getter g = (Getter) this.indices.get (i);
331         
332         Object JavaDoc eo1 = g.getValue (o1);
333
334         Object JavaDoc eo2 = g.getValue (o2);
335
336         // Can't compare what's not there, return 0.
337
if ((eo1 == null)
338             ||
339             (eo2 == null)
340            )
341         {
342
343             return 0;
344
345         }
346
347         int v = 0;
348             
349         if (eo1 instanceof Comparable JavaDoc)
350         {
351             
352             // We can use a simple compareTo.
353
Comparable JavaDoc comp = (Comparable JavaDoc) eo1;
354             
355             v = comp.compareTo (eo2);
356             
357         } else {
358             
359             v = eo1.toString ().compareTo (eo2.toString ());
360             
361         }
362             
363         if (v != 0)
364         {
365             
366             return v;
367             
368         }
369         
370         // They are equal, so need to go to the next field...
371
continue;
372
373         }
374
375     } catch (Exception JavaDoc e) {
376         
377     }
378
379     // All equal.
380
return 0;
381
382     }
383
384 }
385
Popular Tags