KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.josql.Query;
25 import org.josql.QueryExecutionException;
26 import org.josql.QueryParseException;
27
28 import org.josql.expressions.Expression;
29
30 import org.josql.internal.Utilities;
31
32 public class FilteredArrayList extends ArrayList JavaDoc
33 {
34
35     private Expression where = null;
36     private Comparator JavaDoc orderByComp = null;
37     private Query q = null;
38     private Exception JavaDoc ex = null;
39     private boolean noThrow = false;
40
41     public FilteredArrayList (String JavaDoc q)
42                           throws QueryParseException
43     {
44
45     this (q,
46           10);
47
48     }
49
50     public FilteredArrayList (String JavaDoc q,
51                   int cap)
52                           throws QueryParseException
53     {
54
55     super (cap);
56
57     this.q = new Query ();
58     this.q.parse (q);
59     this.where = this.q.getWhereClause ();
60
61     }
62
63     public FilteredArrayList (String JavaDoc q,
64                   Collection JavaDoc c)
65                           throws QueryParseException
66     {
67
68     this (q);
69
70     this.addAll (c);
71
72     }
73
74     public FilteredArrayList (Query q)
75     {
76
77     this.q = q;
78
79     }
80
81     public FilteredArrayList (Query q,
82                   Collection JavaDoc c)
83     {
84
85     this (q);
86
87     this.addAll (c);
88
89     }
90
91     public boolean isNoThrowOnWhereFalse ()
92     {
93
94     return this.noThrow;
95
96     }
97
98     public void setNoThrowOnWhereFalse (boolean v)
99     {
100
101     this.noThrow = v;
102
103     }
104
105     public Exception JavaDoc getException ()
106     {
107
108     return this.ex;
109
110     }
111
112     public Query getQuery ()
113     {
114
115     return this.q;
116
117     }
118
119     public void resort ()
120     {
121
122     if (this.orderByComp == null)
123     {
124
125         this.orderByComp = this.q.getOrderByComparator ();
126
127     }
128
129     if (this.orderByComp != null)
130     {
131
132         Collections.sort (this,
133                   this.orderByComp);
134
135         return;
136
137     }
138
139     Collections.sort (this);
140
141     }
142
143     private boolean check (Object JavaDoc o)
144                        throws IllegalArgumentException JavaDoc
145     {
146
147     this.ex = null;
148
149     if (this.where == null)
150     {
151
152         return true;
153
154     }
155
156     try
157     {
158
159         if (!this.where.isTrue (o,
160                     this.q))
161         {
162
163         if (!this.noThrow)
164         {
165
166             throw new IllegalArgumentException JavaDoc ("Where clause: " +
167                             this.where +
168                             " evaluates to false for object cannot be added");
169         
170         }
171
172         return false;
173
174         }
175
176         return true;
177
178     } catch (QueryExecutionException e) {
179
180         this.ex = e;
181
182         throw new IllegalArgumentException JavaDoc ("Where clause: " +
183                         this.where +
184                         " throws exception during execution, use: getException for details.");
185
186     }
187
188     }
189
190     public boolean addAll (Collection JavaDoc c)
191                        throws IllegalArgumentException JavaDoc
192     {
193
194     int s = this.size () - 1;
195
196     if (s < 0)
197     {
198
199         s = 0;
200
201     }
202
203     return this.addAll (s,
204                 c);
205
206     }
207
208     public boolean addAll (int index,
209                Collection JavaDoc c)
210                        throws IllegalArgumentException JavaDoc
211     {
212
213     this.ex = null;
214
215     if (c == null)
216     {
217
218         throw new NullPointerException JavaDoc ("Expected collection to be non-null.");
219
220     }
221
222     boolean change = false;
223
224     int st = index;
225
226     if (c instanceof List JavaDoc)
227     {
228
229         List JavaDoc l = (List JavaDoc) c;
230
231         int s = l.size ();
232
233         for (int i = 0; i < s; i++)
234         {
235
236         Object JavaDoc o = l.get (i);
237
238         try
239         {
240
241             if (this.where.isTrue (o,
242                        this.q))
243             {
244
245             super.add (st,
246                    o);
247
248             st++;
249             change = true;
250
251             } else {
252
253             if (!this.noThrow)
254             {
255
256                 throw new IllegalArgumentException JavaDoc ("Where clause: " +
257                                 this.where +
258                                 " evaluates to false for object cannot be added");
259
260             }
261
262             }
263
264         } catch (QueryExecutionException e) {
265             
266             this.ex = e;
267             
268             throw new IllegalArgumentException JavaDoc ("Where clause: " +
269                             this.where +
270                             " throws exception during execution, use: getException for details.");
271
272         }
273
274         }
275
276     } else {
277
278         Iterator JavaDoc iter = c.iterator ();
279
280         while (iter.hasNext ())
281         {
282
283         Object JavaDoc o = iter.next ();
284
285         try
286         {
287
288             if (this.where.isTrue (o,
289                        this.q))
290             {
291
292             super.add (st,
293                    o);
294
295             st++;
296             change = true;
297
298             } else {
299
300             if (!this.noThrow)
301             {
302
303                 throw new IllegalArgumentException JavaDoc ("Where clause: " +
304                                 this.where +
305                                 " evaluates to false for object cannot be added");
306
307             }
308
309             }
310
311         } catch (QueryExecutionException e) {
312             
313             this.ex = e;
314
315             throw new IllegalArgumentException JavaDoc ("Where clause: " +
316                             this.where +
317                             " throws exception during execution, use: getException for details.");
318
319         }
320
321         }
322
323     }
324
325     return change;
326
327     }
328
329     public void add (int index,
330              Object JavaDoc o)
331                  throws IllegalArgumentException JavaDoc
332     {
333
334     if (!this.check (o))
335     {
336
337         return;
338
339     }
340
341     super.add (index,
342            o);
343
344     }
345
346     public Object JavaDoc set (int index,
347                Object JavaDoc o)
348                    throws IllegalArgumentException JavaDoc
349     {
350
351     Object JavaDoc oo = this.get (index);
352
353     if (!this.check (o))
354     {
355
356         return oo;
357
358     }
359
360     super.set (index,
361            o);
362
363     return oo;
364
365     }
366
367     public boolean add (Object JavaDoc o)
368                     throws IllegalArgumentException JavaDoc
369     {
370
371     if (!this.check (o))
372     {
373
374         return false;
375
376     }
377
378     return super.add (o);
379
380     }
381
382     public boolean canAdd (Object JavaDoc o)
383                        throws QueryExecutionException
384     {
385
386     return this.where.isTrue (o,
387                   this.q);
388
389     }
390
391     public Object JavaDoc clone ()
392     {
393
394     FilteredArrayList l = new FilteredArrayList (this.q,
395                              this);
396
397     return l;
398
399     }
400
401     public List JavaDoc cloneList (Query q)
402     {
403
404     return new FilteredArrayList (q,
405                       this);
406
407     }
408
409     public List JavaDoc cloneList ()
410     {
411
412     return new FilteredArrayList (this.q,
413                       this);
414
415     }
416
417     public FilteredArrayList cloneSelf ()
418     {
419
420     return (FilteredArrayList) this.cloneList ();
421
422     }
423
424     public FilteredArrayList cloneSelf (Query q)
425     {
426
427     return (FilteredArrayList) this.cloneList (q);
428
429     }
430
431 }
432
Popular Tags