1 15 package org.josql.incubator; 16 17 import java.util.List ; 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.ArrayList ; 21 import java.util.Comparator ; 22 import java.util.Iterator ; 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 33 { 34 35 private Expression where = null; 36 private Comparator orderByComp = null; 37 private Query q = null; 38 private Exception ex = null; 39 private boolean noThrow = false; 40 41 public FilteredArrayList (String q) 42 throws QueryParseException 43 { 44 45 this (q, 46 10); 47 48 } 49 50 public FilteredArrayList (String 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 q, 64 Collection 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 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 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 o) 144 throws IllegalArgumentException 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 ("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 ("Where clause: " + 183 this.where + 184 " throws exception during execution, use: getException for details."); 185 186 } 187 188 } 189 190 public boolean addAll (Collection c) 191 throws IllegalArgumentException 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 c) 210 throws IllegalArgumentException 211 { 212 213 this.ex = null; 214 215 if (c == null) 216 { 217 218 throw new NullPointerException ("Expected collection to be non-null."); 219 220 } 221 222 boolean change = false; 223 224 int st = index; 225 226 if (c instanceof List ) 227 { 228 229 List l = (List ) c; 230 231 int s = l.size (); 232 233 for (int i = 0; i < s; i++) 234 { 235 236 Object 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 ("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 ("Where clause: " + 269 this.where + 270 " throws exception during execution, use: getException for details."); 271 272 } 273 274 } 275 276 } else { 277 278 Iterator iter = c.iterator (); 279 280 while (iter.hasNext ()) 281 { 282 283 Object 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 ("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 ("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 o) 331 throws IllegalArgumentException 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 set (int index, 347 Object o) 348 throws IllegalArgumentException 349 { 350 351 Object 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 o) 368 throws IllegalArgumentException 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 o) 383 throws QueryExecutionException 384 { 385 386 return this.where.isTrue (o, 387 this.q); 388 389 } 390 391 public Object clone () 392 { 393 394 FilteredArrayList l = new FilteredArrayList (this.q, 395 this); 396 397 return l; 398 399 } 400 401 public List cloneList (Query q) 402 { 403 404 return new FilteredArrayList (q, 405 this); 406 407 } 408 409 public List 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 |