1 27 28 package org.objectweb.speedo.query.lib; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.ListIterator ; 36 import java.util.NoSuchElementException ; 37 38 import javax.jdo.JDOUnsupportedOptionException; 39 import javax.jdo.JDOUserException; 40 import javax.jdo.PersistenceManager; 41 42 import org.objectweb.medor.api.MedorException; 43 import org.objectweb.medor.tuple.api.TupleCollection; 44 import org.objectweb.speedo.api.SpeedoException; 45 import org.objectweb.util.monolog.api.BasicLevel; 46 import org.objectweb.util.monolog.api.Logger; 47 48 55 public class QueryResultList extends QueryResultCommon implements List { 56 57 60 private List values = null; 61 62 65 private QueryIterator iterator; 66 67 75 public QueryResultList(TupleCollection _tc, 76 PersistenceManager _pm, 77 Object [] _conns, 78 Class _resultClazz, 79 Class [] _selectedFieldTypes, 80 boolean staticFirstElementIndex, 81 Logger _logger) 82 throws MedorException, SpeedoException { 83 super(_tc, _pm, _conns, _resultClazz, _selectedFieldTypes, 84 staticFirstElementIndex, _logger); 85 } 86 87 92 protected boolean assertNotClosed() { 93 return pm != null; 94 } 95 96 99 protected boolean assertBeforeNext() { 100 return pm != null && !pm.isClosed(); 101 } 102 103 106 protected List getList() { 107 if (!assertNotClosed()) { 108 throw new JDOUserException("Impossible to use a closed query result"); 109 } 110 if (!assertBeforeNext()) { 111 throw new JDOUserException( 112 "Impossible to use a query result without opened persistence manager"); 113 } 114 if (values != null) 115 return values; 116 117 values = new ArrayList (); 118 try { 119 if (!tc.isEmpty()) { 120 tc.first(); 121 do { 122 Object o = getValue(tc.getTuple()); 123 if (o != null) { 124 values.add(o); 125 } 126 } while (tc.next()); 127 } 128 } catch (MedorException me) { 129 values = null; 130 throw new JDOUnsupportedOptionException( 131 "Persistence manager problem to get result", new Exception []{me}); 132 } 133 return values; 134 } 135 136 private class QueryIterator implements Iterator { 137 138 private Object next; 139 private int idx = 0; 140 141 public QueryIterator() { 142 try { 143 tc.first(); 144 if (assertBeforeNext()) { 145 calculateNext(true); 146 } else { 147 next = null; 148 } 149 } catch (MedorException e) { 150 next = null; 151 } 152 } 153 154 public void desactivate() { 155 next = null; 156 } 157 public boolean hasNext() { 158 return next != null; 159 } 160 161 public Object next() { 162 if (next == null) { 163 throw new NoSuchElementException ("No such element"); 164 } 165 Object res = next; 166 calculateNext(false); 167 return res; 168 } 169 170 private void calculateNext(boolean isfirst) { 171 boolean first = isfirst; 172 next = null; 173 try { 174 while(next == null && (first || tc.next())) { 175 first = false; 176 next = getValue(tc.getTuple()); 177 } 178 } catch (MedorException e) { 179 logger.log(BasicLevel.ERROR, "Impossible to fetch the pname", e); 180 throw new NoSuchElementException (e.getMessage()); 181 } 182 } 183 184 public void remove() { 185 throw new UnsupportedOperationException ( 186 "This method is not supported in this implementation"); 187 } 188 } 189 190 193 197 public boolean add(Object o) { 198 throw new JDOUnsupportedOptionException( 199 "This method is not supported in this implementation"); 200 } 201 202 206 public boolean addAll(Collection c) { 207 throw new JDOUnsupportedOptionException( 208 "This method is not supported in this implementation"); 209 } 210 211 215 public void clear() { 216 throw new JDOUnsupportedOptionException( 217 "This method is not supported in this implementation"); 218 } 219 220 223 public boolean contains(Object o) { 224 return indexOf(o) != -1; 225 } 226 227 231 public boolean containsAll(Collection c) { 232 if (values != null) { 233 return values.containsAll(c); 234 } 235 ArrayList tofind = new ArrayList (c); 236 for(Iterator it = iterator(); it.hasNext() && !tofind.isEmpty();) { 237 Object current = it.next(); 238 boolean found = false; 239 for (Iterator iter = tofind.iterator(); iter.hasNext() && !found;) { 240 Object o = iter.next(); 241 if ((o == null && current == null) 242 || (o != null && o.equals(current))){ 243 found = true; 244 iter.remove(); 245 } 246 } 247 if (!found) { 248 return false; 249 } 250 } 251 return true; 252 } 253 254 257 public boolean equals(Object o) { 258 throw new JDOUnsupportedOptionException( 259 "This method is not supported in this implementation"); 260 } 261 262 265 public int hashCode() { 266 return getList().hashCode(); 267 } 268 269 272 public boolean isEmpty() { 273 try { 274 return tc == null || tc.isEmpty(); 275 } catch (MedorException me) { 276 throw new JDOUnsupportedOptionException( 277 "a MedorException has been catched", 278 new Exception []{me}); 279 } 280 } 281 282 285 public Iterator iterator() { 286 if (iterator != null) { 287 iterator.desactivate(); 288 } 289 iterator = new QueryIterator(); 290 return iterator; 291 } 292 293 297 public boolean remove(Object o) { 298 throw new JDOUnsupportedOptionException( 299 "This method is not supported in this implementation"); 300 } 301 302 306 public boolean removeAll(Collection c) { 307 throw new JDOUnsupportedOptionException( 308 "This method is not supported in this implementation"); 309 } 310 311 315 public boolean retainAll(Collection c) { 316 throw new JDOUnsupportedOptionException( 317 "This method is not supported in this implementation"); 318 } 319 320 323 public int size() { 324 return getList().size(); 325 } 326 327 330 public Object [] toArray() { 331 return getList().toArray(); 332 } 333 334 338 public Object [] toArray(Object [] a) { 339 return getList().toArray(a); 340 } 341 342 public void add(int index, Object element) { 343 throw new JDOUnsupportedOptionException( 344 "This method is not supported in this implementation"); 345 } 346 public boolean addAll(int index, Collection c) { 347 throw new JDOUnsupportedOptionException( 348 "This method is not supported in this implementation"); 349 } 350 public Object get(int index) { 351 if (values != null) { 352 return values.get(index); 353 } else { 354 int i=-1; 355 Object current = null; 356 for(Iterator it = iterator(); it.hasNext()&& i<index;i++) { 357 current = it.next(); 358 } 359 if (i == index) { 360 return current; 361 } else { 362 return null; 363 } 364 } 365 } 366 public int indexOf(Object o) { 367 if (values != null) { 368 return values.indexOf(o); 369 } 370 int i=0; 371 for(Iterator it = iterator(); it.hasNext();i++) { 372 Object current = it.next(); 373 if ((o == null && current == null) 374 || (o != null && o.equals(current))){ 375 return i; 376 } 377 } 378 return -1; 379 } 380 381 public int lastIndexOf(Object o) { 382 return getList().lastIndexOf(o); 383 } 384 385 public ListIterator listIterator() { 386 return listIterator(0); 387 } 388 389 public ListIterator listIterator(int index) { 390 if (values != null) { 391 return values.listIterator(); 392 } else if (index == 0) { 393 return getList().listIterator(); 394 } else { 395 return getList().listIterator(index); 397 } 398 } 399 public Object remove(int index) { 400 throw new JDOUnsupportedOptionException( 401 "This method is not supported in this implementation"); 402 } 403 public Object set(int index, Object element) { 404 throw new JDOUnsupportedOptionException( 405 "This method is not supported in this implementation"); 406 } 407 public List subList(int fromIndex, int toIndex) { 408 if (toIndex < fromIndex) { 409 throw new IllegalArgumentException ("toIndex(" + toIndex 410 + ") < fromIndex(" + fromIndex +")"); 411 } else if (toIndex == fromIndex) { 412 return Collections.EMPTY_LIST; 413 }else if (values != null) { 414 return values.subList(fromIndex, toIndex); 415 } else { 416 ArrayList result = new ArrayList (); 417 int i=-1; 418 for(Iterator it = iterator(); it.hasNext() && i<toIndex;i++) { 419 Object current = it.next(); 420 } 421 return null; 422 } 423 } 424 } 425 | Popular Tags |