1 21 package com.db4o; 22 23 import java.util.*; 24 25 import com.db4o.inside.*; 26 import com.db4o.reflect.*; 27 import com.db4o.types.*; 28 29 34 public class P2LinkedList extends P1Collection implements Db4oList { 35 36 public P1ListElement i_first; 37 public P1ListElement i_last; 38 39 public void add(int index, Object element) { 40 synchronized (streamLock()) { 41 checkActive(); 42 if (index == 0) { 43 i_first = new P1ListElement(getTrans(), i_first, element); 44 store(i_first); 45 checkLastAndUpdate(null, i_first); 46 } else { 47 P2ListElementIterator i = iterator4(); 48 P1ListElement previous = i.move(index - 1); 49 if (previous == null) { 50 throw new IndexOutOfBoundsException (); 51 } 52 P1ListElement newElement = new P1ListElement(getTrans(), 53 previous.i_next, element); 54 store(newElement); 55 previous.i_next = newElement; 56 previous.update(); 57 checkLastAndUpdate(previous, newElement); 58 } 59 } 60 } 61 62 public boolean add(Object o) { 63 synchronized (streamLock()) { 64 checkActive(); 65 if (o == null) { 66 throw new NullPointerException (); 67 } 68 add4(o); 69 update(); 70 return true; 71 } 72 } 73 74 private boolean add4(Object o) { 75 if (o != null) { 76 P1ListElement newElement = new P1ListElement(getTrans(), null, o); 77 store(newElement); 78 if (i_first == null) { 79 i_first = newElement; 80 } else { 81 i_last.checkActive(); 82 i_last.i_next = newElement; 83 i_last.update(); 84 } 85 i_last = newElement; 86 return true; 87 } 88 return false; 89 } 90 91 public boolean addAll(Collection c) { 92 synchronized (streamLock()) { 93 checkActive(); 94 boolean modified = false; 95 Iterator i = c.iterator(); 96 while (i.hasNext()) { 97 if (add4(i.next())) { 98 modified = true; 99 } 100 } 101 if (modified) { 102 update(); 103 } 104 return modified; 105 } 106 } 107 108 public boolean addAll(int index, Collection c) { 109 synchronized (streamLock()) { 110 checkActive(); 111 Object first = null; 112 Iterator it = c.iterator(); 113 while (it.hasNext() && (first == null)) { 114 first = it.next(); 115 } 116 if (first != null) { 117 P1ListElement newElement = null; 118 P1ListElement nextElement = null; 119 if (index == 0) { 120 nextElement = i_first; 121 newElement = new P1ListElement(getTrans(), i_first, first); 122 i_first = newElement; 123 } else { 124 P2ListElementIterator i = iterator4(); 125 P1ListElement previous = i.move(index - 1); 126 if (previous == null) { 127 throw new IndexOutOfBoundsException (); 128 } 129 nextElement = previous.i_next; 130 newElement = new P1ListElement(getTrans(), previous.i_next, 131 first); 132 previous.i_next = newElement; 133 previous.update(); 134 } 135 while (it.hasNext()) { 136 Object obj = it.next(); 137 if (obj != null) { 138 newElement.i_next = new P1ListElement(getTrans(), 139 nextElement, obj); 140 store(newElement); 141 newElement = newElement.i_next; 142 } 143 } 144 store(newElement); 145 if (nextElement == null) { 146 i_last = newElement; 147 } 148 update(); 149 return true; 150 } 151 return false; 152 } 153 } 154 155 public int adjustReadDepth(int a_depth) { 156 return 1; 157 } 158 159 private void checkLastAndUpdate(P1ListElement a_oldLast, 160 P1ListElement a_added) { 161 if (i_last == a_oldLast) { 162 i_last = a_added; 163 } 164 update(); 165 } 166 167 void checkRemoved(P1ListElement a_previous, P1ListElement a_removed) { 168 boolean needsUpdate = false; 169 if (a_removed == i_first) { 170 i_first = a_removed.i_next; 171 needsUpdate = true; 172 } 173 if (a_removed == i_last) { 174 i_last = a_previous; 175 needsUpdate = true; 176 } 177 if (needsUpdate) { 178 update(); 179 } 180 } 181 182 public void clear() { 183 synchronized (streamLock()) { 184 checkActive(); 185 P2ListElementIterator i = iterator4(); 186 while (i.hasNext()) { 187 P1ListElement elem = i.nextElement(); 188 elem.delete(i_deleteRemoved); 189 } 190 i_first = null; 191 i_last = null; 192 update(); 193 } 194 } 195 196 public boolean contains(Object o) { 197 return indexOf(o) >= 0; 198 } 199 200 private boolean contains4(Object o) { 201 return indexOf4(o) >= 0; 202 } 203 204 public boolean containsAll(Collection c) { 205 synchronized (streamLock()) { 206 checkActive(); 207 Iterator i = c.iterator(); 208 while (i.hasNext()) { 209 if (!contains4(i.next())) { 210 return false; 211 } 212 } 213 return true; 214 } 215 } 216 217 public Object createDefault(Transaction a_trans) { 218 checkActive(); 219 P2LinkedList l4 = new P2LinkedList(); 220 l4.setTrans(a_trans); 221 P2ListElementIterator i = iterator4(); 222 while (i.hasNext()) { 223 l4.add4(i.next()); 224 } 225 return l4; 226 } 227 228 public Object get(int index) { 229 synchronized (streamLock()) { 230 checkActive(); 231 P2ListElementIterator i = iterator4(); 232 P1ListElement elem = i.move(index); 233 if (elem != null) { 234 return elem.activatedObject(elementActivationDepth()); 235 } 236 return null; 237 } 238 } 239 240 public boolean hasClassIndex() { 241 return true; 242 } 243 244 public int indexOf(Object o) { 245 synchronized (streamLock()) { 246 checkActive(); 247 return indexOf4(o); 248 } 249 } 250 251 private int indexOf4(Object o) { 252 int idx = 0; 253 if (getTrans() != null && (! getTrans().stream().handlers().isSecondClass(o))) { 256 long id = getIDOf(o); 257 if (id > 0) { 258 P2ListElementIterator i = iterator4(); 259 while (i.hasNext()) { 260 P1ListElement elem = i.nextElement(); 261 if (getIDOf(elem.i_object) == id) { 262 return idx; 263 } 264 idx++; 265 } 266 } 267 } else { 268 P2ListElementIterator i = iterator4(); 269 while (i.hasNext()) { 270 P1ListElement elem = i.nextElement(); 271 if (elem.i_object.equals(o)) { 272 return idx; 273 } 274 idx++; 275 } 276 } 277 return -1; 278 } 279 280 public boolean isEmpty() { 281 synchronized (streamLock()) { 282 checkActive(); 283 return i_first == null; 284 } 285 } 286 287 public Iterator iterator() { 288 synchronized (streamLock()) { 289 checkActive(); 290 return iterator4(); 291 } 292 } 293 294 private P2ListElementIterator iterator4() { 295 return new P2ListElementIterator(this, i_first); 296 } 297 298 public int lastIndexOf(Object o) { 299 synchronized (streamLock()) { 300 checkActive(); 301 int ret = -1; 302 int idx = 0; 303 if (getTrans() != null) { 304 long id = getIDOf(o); 305 if (id > 0) { 306 P2ListElementIterator i = iterator4(); 307 while (i.hasNext()) { 308 P1ListElement elem = i.nextElement(); 309 if (getIDOf(elem.i_object) == id) { 310 ret = idx; 311 } 312 idx++; 313 } 314 } 315 } else { 316 P2ListElementIterator i = iterator4(); 317 while (i.hasNext()) { 318 P1ListElement elem = i.nextElement(); 319 if (elem.i_object.equals(o)) { 320 ret = idx; 321 } 322 idx++; 323 } 324 } 325 return ret; 326 } 327 } 328 329 public ListIterator listIterator() { 330 throw new UnsupportedOperationException (); 331 } 332 333 public ListIterator listIterator(int index) { 334 throw new UnsupportedOperationException (); 335 } 336 337 public Object remove(int index) { 338 synchronized (streamLock()) { 339 checkActive(); 340 return remove4(index); 341 } 342 } 343 344 public boolean remove(Object o) { 345 synchronized (streamLock()) { 346 checkActive(); 347 return remove4(o); 348 } 349 } 350 351 private Object remove4(int index) { 352 Object ret = null; 353 P1ListElement elem = null; 354 P1ListElement previous = null; 355 if (index == 0) { 356 elem = i_first; 357 } else { 358 previous = iterator4().move(index - 1); 359 if (previous != null) { 360 elem = previous.i_next; 361 } 362 } 363 if (elem != null) { 364 elem.checkActive(); 365 if (previous != null) { 366 previous.i_next = elem.i_next; 367 previous.update(); 368 } 369 checkRemoved(previous, elem); 370 ret = elem.activatedObject(elementActivationDepth()); 371 elem.delete(i_deleteRemoved); 372 return ret; 373 } 374 throw new IndexOutOfBoundsException (); 375 } 376 377 private boolean remove4(Object o) { 378 int idx = indexOf4(o); 379 if (idx >= 0) { 380 remove4(idx); 381 return true; 382 } 383 return false; 384 } 385 386 public boolean removeAll(Collection c) { 387 synchronized (streamLock()) { 388 checkActive(); 389 boolean modified = false; 390 Iterator i = c.iterator(); 391 while (i.hasNext()) { 392 if (remove(i.next())) { 393 modified = true; 394 } 395 } 396 return modified; 397 } 398 } 399 400 public void replicateFrom(Object obj) { 401 checkActive(); 402 P2ListElementIterator i = iterator4(); 403 while (i.hasNext()) { 404 P1ListElement elem = i.nextElement(); 405 elem.delete(false); 406 } 407 i_first = null; 408 i_last = null; 409 P2LinkedList l4 = (P2LinkedList)obj; 410 i = l4.iterator4(); 411 while (i.hasNext()) { 412 add4(i.next()); 413 } 414 updateInternal(); 415 } 416 417 public boolean retainAll(Collection c) { 418 throw new UnsupportedOperationException (); 419 } 420 421 public Object set(int index, Object element) { 422 synchronized (streamLock()) { 423 checkActive(); 424 boolean needUpdate = false; 425 Object ret = null; 426 P1ListElement elem = null; 427 P1ListElement previous = null; 428 P1ListElement newElement = new P1ListElement(getTrans(), null, 429 element); 430 if (index == 0) { 431 elem = i_first; 432 i_first = newElement; 433 needUpdate = true; 434 } else { 435 P2ListElementIterator i = iterator4(); 436 previous = i.move(index - 1); 437 if (previous != null) { 438 elem = previous.i_next; 439 } else { 440 throw new IndexOutOfBoundsException (); 441 } 442 } 443 444 if (elem != null) { 445 elem.checkActive(); 446 newElement.i_next = elem.i_next; 447 if (previous != null) { 448 previous.i_next = newElement; 449 previous.update(); 450 } 451 ret = elem.activatedObject(elementActivationDepth()); 452 elem.delete(i_deleteRemoved); 453 } else { 454 i_last = newElement; 455 needUpdate = true; 456 } 457 if (needUpdate) { 458 update(); 459 } 460 return ret; 461 } 462 } 463 464 public synchronized int size() { 465 synchronized (streamLock()) { 466 checkActive(); 467 return size4(); 468 } 469 } 470 471 private int size4() { 472 int size = 0; 473 P2ListElementIterator i = iterator4(); 474 while (i.hasNext()) { 475 size++; 476 i.nextElement(); 477 } 478 return size; 479 } 480 481 public Object storedTo(Transaction a_trans) { 482 if (getTrans() == null) { 483 setTrans(a_trans); 484 } else { 485 if (a_trans != getTrans()) { 486 return replicate(getTrans(), a_trans); 487 } 488 } 489 return this; 490 } 491 492 public List subList(int fromIndex, int toIndex) { 493 throw new UnsupportedOperationException (); 494 } 495 496 public Object [] toArray() { 497 synchronized (streamLock()) { 498 checkActive(); 499 Object [] arr = new Object [size4()]; 500 int i = 0; 501 P2ListElementIterator j = iterator4(); 502 while (j.hasNext()) { 503 P1ListElement elem = j.nextElement(); 504 arr[i++] = elem.activatedObject(elementActivationDepth()); 505 } 506 return arr; 507 } 508 } 509 510 public Object [] toArray(Object [] a) { 511 synchronized (streamLock()) { 512 checkActive(); 513 int size = size(); 514 if (a.length < size) { 515 Transaction trans = getTrans(); 516 if(trans == null){ 517 Exceptions4.throwRuntimeException(29); 518 } 519 Reflector reflector = trans.reflector(); 520 a = 521 (Object [])reflector.array().newInstance( 522 reflector.forObject(a).getComponentType(), 523 size); 524 } 525 int i = 0; 526 P2ListElementIterator j = iterator4(); 527 while (j.hasNext()) { 528 P1ListElement elem = j.nextElement(); 529 a[i++] = elem.activatedObject(elementActivationDepth()); 530 } 531 if (a.length > size) { 532 a[size] = null; 533 } 534 return a; 535 } 536 } 537 538 } 539 | Popular Tags |