1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import org.netbeans.jmi.javamodel.*; 24 import org.netbeans.mdr.handlers.BaseObjectHandler; 25 import javax.jmi.reflect.RefObject; 26 import javax.jmi.reflect.TypeMismatchException; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.ListIterator ; 31 import java.lang.reflect.Array ; 32 33 37 public class FeaturesList implements List { 38 private ClassDefinition javaClass; 39 40 public FeaturesList(ClassDefinition javaClass) { 41 this.javaClass = javaClass; 42 } 43 44 private void lock() { 45 lock(false); 46 } 47 48 private void lock(boolean readWrite) { 49 ((BaseObjectHandler) javaClass).repository().beginTrans(readWrite); 50 } 51 52 private void unlock() { 53 unlock(false); 54 } 55 56 private void unlock(boolean fail) { 57 ((BaseObjectHandler) javaClass).repository().endTrans(fail); 58 } 59 60 public int size() { 61 lock(); 62 try { 63 int size = 0; 64 Object features[]=javaClass.getContents().toArray(); 65 for (int i=0;i<features.length;i++) { 66 Object temp = features[i]; 67 if (temp instanceof FieldGroup) { 68 size += ((FieldGroup) temp).getFields().size(); 69 } else { 70 size++; 71 } 72 } 73 return size; 74 } finally { 75 unlock(); 76 } 77 } 78 79 public boolean isEmpty() { 80 lock(); 81 try { 82 return javaClass.getContents().isEmpty(); 83 } finally { 84 unlock(); 85 } 86 } 87 88 public boolean contains(Object o) { 89 lock(); 90 try { 91 if (o instanceof FieldGroup) { 92 return false; 93 } 94 return javaClass.equals(((RefObject) o).refImmediateComposite()); 95 } finally { 96 unlock(); 97 } 98 } 99 100 public Iterator iterator() { 101 return listIterator(); 102 } 103 104 public Object [] toArray() { 105 lock(); 106 try { 107 Object features[]=javaClass.getContents().toArray(); 108 List result=new ArrayList (100); 109 boolean hasFieldGroup=false; 110 111 for (int i=0;i<features.length;i++) { 112 Object feature=features[i]; 113 114 if (feature instanceof FieldGroup) { 115 FieldGroup group=(FieldGroup)feature; 116 117 hasFieldGroup=true; 118 result.addAll(Arrays.asList(group.getFields().toArray())); 119 } else 120 result.add(feature); 121 } 122 if (hasFieldGroup) 123 return result.toArray(); 124 return features; 125 } finally { 126 unlock(); 127 } 128 } 129 130 public Object [] toArray(Object a[]) { 131 Object arr[]=toArray(); 132 int size = arr.length; 133 if (a.length < size) { 134 a = (Object []) Array.newInstance(a.getClass().getComponentType(), size); 135 } 136 System.arraycopy(arr, 0, a, 0, size); 137 if (a.length > size) 138 a[size] = null; 139 return a; 140 } 141 142 public boolean add(Object o) { 143 if (o instanceof FieldGroup) throw new TypeMismatchException(Feature.class, o, null); 144 boolean fail = true; 145 lock(true); 146 try { 147 boolean result = javaClass.getContents().add(o); 148 fail = false; 149 return result; 150 } finally { 151 unlock(fail); 152 } 153 } 154 155 public boolean remove(Object o) { 156 if (!(o instanceof Feature)) return false; 157 boolean fail = true; 158 lock(true); 159 try { 160 boolean result = false; 161 Object parent = ((Feature) o).refImmediateComposite(); 162 if (javaClass.equals(parent)) { 163 result = javaClass.getContents().remove(o); 164 } else if (parent instanceof FieldGroup 165 && javaClass.equals(((FieldGroup) parent).refImmediateComposite())) { 166 result = ((FieldGroup) parent).getFields().remove(o); 167 separate((Field) o, (FieldGroup) parent); 168 } 169 170 fail = false; 171 return result; 172 } finally { 173 unlock(fail); 174 } 175 } 176 177 private void separate(Field field, FieldGroup fg) { 178 field.setModifiers(((FieldGroupImpl)fg).getSourceModifiers()); 179 field.setTypeName((TypeReference) fg.getTypeName().duplicate()); 180 } 181 182 public boolean containsAll(Collection c) { 183 lock(); 184 try { 185 for (Iterator it = c.iterator(); it.hasNext();) { 186 if (!contains(it.next())) { 187 return false; 188 } 189 } 190 return true; 191 } finally { 192 unlock(); 193 } 194 } 195 196 public boolean addAll(Collection c) { 197 boolean fail = true; 198 lock(true); 199 try { 200 boolean result = false; 201 for (Iterator it = c.iterator(); it.hasNext();) { 202 result |= add(it.next()); 203 } 204 fail = false; 205 return result; 206 } finally { 207 unlock(fail); 208 } 209 } 210 211 public boolean addAll(int index, Collection c) { 212 boolean fail = true; 213 lock(true); 214 try { 215 ListIterator cm = listIterator(index); 216 for (Iterator it = c.iterator(); it.hasNext();) { 217 cm.add(it.next()); 218 } 219 fail = false; 220 return !c.isEmpty(); 221 } finally { 222 unlock(fail); 223 } 224 } 225 226 public boolean removeAll(Collection c) { 227 boolean fail = true; 228 lock(true); 229 try { 230 boolean result = false; 231 for (Iterator it = c.iterator(); it.hasNext();) { 232 result |= remove(it.next()); 233 } 234 fail = false; 235 return result; 236 } finally { 237 unlock(fail); 238 } 239 } 240 241 public boolean retainAll(Collection c) { 242 boolean fail = true; 243 lock(true); 244 try { 245 boolean result = false; 246 for (Iterator it = iterator(); it.hasNext();) { 247 if (!c.contains(it.next())) { 248 it.remove(); 249 result = true; 250 } 251 } 252 fail = false; 253 return result; 254 } finally { 255 unlock(fail); 256 } 257 } 258 259 public void clear() { 260 boolean fail = true; 261 lock(true); 262 try { 263 javaClass.getContents().clear(); 264 fail = false; 265 } finally { 266 unlock(fail); 267 } 268 } 269 270 public Object get(int index) { 271 lock(); 272 try { 273 return listIterator(index).next(); 274 } finally { 275 unlock(); 276 } 277 } 278 279 public Object set(int index, Object element) { 280 boolean fail = true; 281 lock(true); 282 try { 283 ListIterator it = listIterator(index); 284 Object result = it.next(); 285 it.set(element); 286 fail = false; 287 return result; 288 } finally { 289 unlock(fail); 290 } 291 } 292 293 public void add(int index, Object element) { 294 boolean fail = true; 295 lock(true); 296 try { 297 listIterator(index).add(element); 298 fail = false; 299 } finally { 300 unlock(fail); 301 } 302 } 303 304 public Object remove(int index) { 305 boolean fail = true; 306 lock(true); 307 try { 308 ListIterator it = listIterator(index); 309 Object result = it.next(); 310 it.remove(); 311 fail = false; 312 return result; 313 } finally { 314 unlock(fail); 315 } 316 } 317 318 public int indexOf(Object o) { 319 lock(); 320 try { 321 for (ListIterator it = listIterator(); it.hasNext();) { 322 if (it.next().equals(o)) { 323 return it.previousIndex(); 324 } 325 } 326 return -1; 327 } finally { 328 unlock(); 329 } 330 } 331 332 public int lastIndexOf(Object o) { 333 lock(); 334 try { 335 int result = -1; 336 for (ListIterator it = listIterator(); it.hasNext();) { 337 if (it.next().equals(o)) { 338 result = it.previousIndex(); 339 } 340 } 341 return result; 342 } finally { 343 unlock(); 344 } 345 } 346 347 public ListIterator listIterator() { 348 lock(); 349 try { 350 return new FeatureListIterator(javaClass.getContents().listIterator()); 351 } finally { 352 unlock(); 353 } 354 } 355 356 public ListIterator listIterator(int index) { 357 lock(); 358 try { 359 ListIterator result = listIterator(); 360 while (result.nextIndex() < index) { 361 result.next(); 362 } 363 return result; 364 } finally { 365 unlock(); 366 } 367 } 368 369 public boolean equals(Object o) { 370 lock(); 371 try { 372 if (o == this) return true; 373 if (!(o instanceof List )) return false; 374 375 ListIterator e1 = listIterator(); 376 ListIterator e2 = ((List ) o).listIterator(); 377 while(e1.hasNext() && e2.hasNext()) { 378 Object o1 = e1.next(); 379 Object o2 = e2.next(); 380 if (o1 != o2 && !o1.equals(o2)) 381 return false; 382 } 383 return !(e1.hasNext() || e2.hasNext()); 384 } finally { 385 unlock(); 386 } 387 } 388 389 public int hashCode() { 390 lock(); 391 try { 392 int hashCode = 1; 393 Iterator it = iterator(); 394 while (it.hasNext()) { 395 hashCode = 31 * hashCode + it.next().hashCode(); 396 } 397 return hashCode; 398 } finally { 399 unlock(); 400 } 401 } 402 403 public String toString() { 404 lock(); 405 try { 406 StringBuffer buf = new StringBuffer (); 407 buf.append("["); 409 Iterator i = iterator(); 410 boolean hasNext = i.hasNext(); 411 while (hasNext) { 412 Object o = i.next(); 413 buf.append(o == this ? "(this Collection)" : String.valueOf(o)); hasNext = i.hasNext(); 415 if (hasNext) 416 buf.append(", "); } 418 419 buf.append("]"); return buf.toString(); 421 } finally { 422 unlock(); 423 } 424 } 425 426 public List subList(int fromIndex, int toIndex) { 428 throw new UnsupportedOperationException (); 429 } 430 431 private class FeatureListIterator implements ListIterator { 432 private final ListIterator contents; 433 private ListIterator fieldGroup = null; 434 private int index = 0; 435 private boolean forward = true; 436 private boolean lastForward = true; 437 private FieldGroup parentGroup = null; 438 private Field currentField = null; 439 440 public FeatureListIterator(ListIterator contents) { 441 this.contents = contents; 442 } 443 444 public boolean hasNext() { 445 lock(); 446 try { 447 if (!forward) { 448 contents.next(); 449 forward = true; 450 } 451 return contents.hasNext() || (fieldGroup != null && fieldGroup.hasNext()); 452 } finally { 453 unlock(); 454 } 455 } 456 457 public Object next() { 458 lock(); 459 try { 460 if (fieldGroup != null) { 461 if (fieldGroup.hasNext()) { 462 index++; 463 lastForward = true; 464 return currentField = (Field) fieldGroup.next(); 465 } else { 466 fieldGroup = null; 467 parentGroup = null; 468 if (!forward) contents.next(); 469 } 470 } 471 Object next = contents.next(); 472 if (next instanceof FieldGroup) { 473 ListIterator it = ((FieldGroup) next).getFields().listIterator(); 474 ListIterator temp = fieldGroup; 475 FieldGroup tmpGroup = parentGroup; 476 fieldGroup = it; 477 parentGroup = (FieldGroup) next; 478 forward = true; 479 Object result; 480 try { 481 result = currentField = (Field) next(); 482 } catch (RuntimeException e) { 483 fieldGroup = temp; 484 parentGroup = tmpGroup; 485 throw e; 486 } 487 index++; 488 lastForward = true; 489 return result; 490 } 491 index++; 492 lastForward = true; 493 return next; 494 } finally { 495 unlock(); 496 } 497 } 498 499 public boolean hasPrevious() { 500 lock(); 501 try { 502 if (forward) { 503 contents.previous(); 504 forward = false; 505 } 506 return contents.hasPrevious() || (fieldGroup != null && fieldGroup.hasPrevious()); 507 } finally { 508 unlock(); 509 } 510 } 511 512 public Object previous() { 513 lock(); 514 try { 515 if (fieldGroup != null) { 516 if (fieldGroup.hasPrevious()) { 517 index--; 518 lastForward = false; 519 return currentField = (Field) fieldGroup.previous(); 520 } else { 521 fieldGroup = null; 522 parentGroup = null; 523 if (forward) contents.previous(); 524 } 525 } 526 Object prev = contents.previous(); 527 if (prev instanceof FieldGroup) { 528 ListIterator it = ((FieldGroup) prev).getFields().listIterator(); 529 while (it.hasNext()) it.next(); 530 forward = false; 531 ListIterator temp = fieldGroup; 532 FieldGroup tmpGroup = parentGroup; 533 fieldGroup = it; 534 parentGroup = (FieldGroup) prev; 535 Object result; 536 try { 537 result = currentField = (Field) previous(); 538 } catch (RuntimeException e) { 539 fieldGroup = temp; 540 parentGroup = tmpGroup; 541 throw e; 542 } 543 index--; 544 lastForward = false; 545 return result; 546 } 547 index--; 548 lastForward = false; 549 return prev; 550 } finally { 551 unlock(); 552 } 553 } 554 555 public int nextIndex() { 556 return index; 557 } 558 559 public int previousIndex() { 560 return index - 1; 561 } 562 563 public void remove() { 564 boolean fail = true; 565 lock(true); 566 try { 567 if (fieldGroup != null) { 568 fieldGroup.remove(); 569 separate(currentField, parentGroup); 570 if (!(fieldGroup.hasNext() || fieldGroup.hasPrevious())) { 571 contents.remove(); 572 fieldGroup = null; 573 } 574 } else { 575 contents.remove(); 576 } 577 if (lastForward) { 578 index--; 579 } 580 fail = false; 581 } finally { 582 unlock(fail); 583 } 584 } 585 586 public void set(Object o) { 587 boolean fail = true; 588 lock(true); 589 try { 590 remove(); 591 add(o); 592 fail = false; 593 } finally { 594 unlock(fail); 595 } 596 } 597 598 public void add(Object o) { 599 if (o instanceof FieldGroup) throw new TypeMismatchException(Feature.class, o, null); 600 boolean fail = true; 601 lock(fail); 602 try { 603 if (fieldGroup == null || !fieldGroup.hasNext()) { 604 contents.add(o); 605 } else if (!fieldGroup.hasPrevious()) { 606 contents.previous(); 607 contents.add(o); 608 contents.next(); 609 } else { 610 FieldGroup fg; 611 if (forward) { 612 fg = (FieldGroup) contents.previous(); 613 } else { 614 fg = (FieldGroup) contents.next(); 615 } 616 forward = !forward; 617 JavaModelPackage pkg = (JavaModelPackage) javaClass.refImmediatePackage(); 618 FieldGroup newGroup = pkg.getFieldGroup().createFieldGroup(null, null, ((FieldGroupImpl)fg).getSourceModifiers(), 619 null, null, null, null); 620 newGroup.setType(fg.getType()); 621 while (fieldGroup.hasNext()) { 622 Object field = fieldGroup.next(); 623 fieldGroup.remove(); 624 newGroup.getFields().add(field); 625 } 626 if (!forward) { 627 contents.next(); 628 } 629 contents.add(o); 630 contents.add(newGroup); 631 fieldGroup = newGroup.getFields().listIterator(); 632 forward = true; 633 index++; 634 } 635 fail = false; 636 } finally { 637 unlock(fail); 638 } 639 } 640 } 641 } 642 | Popular Tags |