1 17 package org.eclipse.emf.ecore.impl; 18 19 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 import java.util.Map ; 26 27 import org.eclipse.emf.common.util.BasicEList; 28 import org.eclipse.emf.common.util.ECollections; 29 import org.eclipse.emf.common.util.EList; 30 import org.eclipse.emf.common.util.URI; 31 import org.eclipse.emf.ecore.EClass; 32 import org.eclipse.emf.ecore.EObject; 33 import org.eclipse.emf.ecore.EReference; 34 import org.eclipse.emf.ecore.EStructuralFeature; 35 import org.eclipse.emf.ecore.InternalEObject; 36 import org.eclipse.emf.ecore.resource.Resource; 37 import org.eclipse.emf.ecore.util.DelegatingEcoreEList; 38 import org.eclipse.emf.ecore.util.DelegatingFeatureMap; 39 import org.eclipse.emf.ecore.util.EcoreEList; 40 import org.eclipse.emf.ecore.util.FeatureMap; 41 import org.eclipse.emf.ecore.util.FeatureMapUtil; 42 43 44 47 public class EStoreEObjectImpl extends EObjectImpl implements EStructuralFeature.Internal.DynamicValueHolder 48 { 49 52 protected static class EStoreEPropertiesHolderImpl implements BasicEObjectImpl.EPropertiesHolder 53 { 54 protected EClass eClass; 55 protected URI eProxyURI; 56 protected Resource.Internal eResource; 57 protected EList eContents; 58 protected EList eCrossReferences; 59 60 public EClass getEClass() 61 { 62 return eClass; 63 } 64 65 public void setEClass(EClass eClass) 66 { 67 this.eClass = eClass; 68 } 69 70 public URI getEProxyURI() 71 { 72 return eProxyURI; 73 } 74 75 public void setEProxyURI(URI eProxyURI) 76 { 77 this.eProxyURI = eProxyURI; 78 } 79 80 public Resource.Internal getEResource() 81 { 82 return eResource; 83 } 84 85 public void setEResource(Resource.Internal eResource) 86 { 87 this.eResource = eResource; 88 } 89 90 public EList getEContents() 91 { 92 return eContents; 93 } 94 95 public void setEContents(EList eContents) 96 { 97 this.eContents = eContents; 98 } 99 100 public EList getECrossReferences() 101 { 102 return eCrossReferences; 103 } 104 105 public void setECrossReferences(EList eCrossReferences) 106 { 107 this.eCrossReferences = eCrossReferences; 108 } 109 110 public boolean hasSettings() 111 { 112 throw new UnsupportedOperationException (); 113 } 114 115 public void allocateSettings(int maximumDynamicFeatureID) 116 { 117 throw new UnsupportedOperationException (); 118 } 119 120 public Object dynamicGet(int dynamicFeatureID) 121 { 122 throw new UnsupportedOperationException (); 123 } 124 125 public void dynamicSet(int dynamicFeatureID, Object value) 126 { 127 throw new UnsupportedOperationException (); 128 } 129 130 public void dynamicUnset(int dynamicFeatureID) 131 { 132 throw new UnsupportedOperationException (); 133 } 134 } 135 136 139 public static class EStoreEList extends DelegatingEcoreEList.Dynamic 140 { 141 protected InternalEObject.EStore store; 142 143 public EStoreEList(InternalEObject owner, EStructuralFeature eStructuralFeature, InternalEObject.EStore store) 144 { 145 super(owner, eStructuralFeature); 146 this.store = store; 147 } 148 149 protected List delegateList() 150 { 151 throw new UnsupportedOperationException (); 152 } 153 154 public EStructuralFeature getEStructuralFeature() 155 { 156 return eStructuralFeature; 157 } 158 159 protected void delegateAdd(int index, Object object) 160 { 161 store.add(owner, eStructuralFeature, index, object); 162 } 163 164 protected void delegateAdd(Object object) 165 { 166 delegateAdd(delegateSize(), object); 167 } 168 169 protected List delegateBasicList() 170 { 171 int size = delegateSize(); 172 if (size == 0) 173 { 174 return ECollections.EMPTY_ELIST; 175 } 176 else 177 { 178 Object [] data = store.toArray(owner, eStructuralFeature); 179 return new EcoreEList.UnmodifiableEList(owner, eStructuralFeature, data.length, data); 180 } 181 } 182 183 protected void delegateClear() 184 { 185 store.clear(owner, eStructuralFeature); 186 } 187 188 protected boolean delegateContains(Object object) 189 { 190 return store.contains(owner, eStructuralFeature, object); 191 } 192 193 protected boolean delegateContainsAll(Collection collection) 194 { 195 for (Iterator i = collection.iterator(); i.hasNext(); ) 196 { 197 if (!delegateContains(i.next())) 198 { 199 return false; 200 } 201 } 202 return true; 203 } 204 205 protected Object delegateGet(int index) 206 { 207 return store.get(owner, eStructuralFeature, index); 208 } 209 210 protected int delegateHashCode() 211 { 212 return store.hashCode(owner, eStructuralFeature); 213 } 214 215 protected int delegateIndexOf(Object object) 216 { 217 return store.indexOf(owner, eStructuralFeature, object); 218 } 219 220 protected boolean delegateIsEmpty() 221 { 222 return store.isEmpty(owner, eStructuralFeature); 223 } 224 225 protected Iterator delegateIterator() 226 { 227 return iterator(); 228 } 229 230 protected int delegateLastIndexOf(Object object) 231 { 232 return store.lastIndexOf(owner, eStructuralFeature, object); 233 } 234 235 protected ListIterator delegateListIterator() 236 { 237 return listIterator(); 238 } 239 240 protected Object delegateRemove(int index) 241 { 242 return store.remove(owner, eStructuralFeature, index); 243 } 244 245 protected Object delegateSet(int index, Object object) 246 { 247 return store.set(owner, eStructuralFeature, index, object); 248 } 249 250 protected int delegateSize() 251 { 252 return store.size(owner, eStructuralFeature); 253 } 254 255 protected Object [] delegateToArray() 256 { 257 return store.toArray(owner, eStructuralFeature); 258 } 259 260 protected Object [] delegateToArray(Object [] array) 261 { 262 return store.toArray(owner, eStructuralFeature, array); 263 } 264 265 protected boolean delegateEquals(Object object) 266 { 267 if (object == this) 268 { 269 return true; 270 } 271 272 if (!(object instanceof List )) 273 { 274 return false; 275 } 276 277 List list = (List )object; 278 if (list.size() != delegateSize()) 279 { 280 return false; 281 } 282 283 284 for (ListIterator i = list.listIterator(); i.hasNext(); ) 285 { 286 Object element= i.next(); 287 if (element == null ? get(i.previousIndex()) != null : !element.equals(get(i.previousIndex()))) 288 { 289 return false; 290 } 291 } 292 293 return true; 294 } 295 296 protected String delegateToString() 297 { 298 StringBuffer stringBuffer = new StringBuffer (); 299 stringBuffer.append("["); 300 for (int i = 0, size = size(); i < size; ) 301 { 302 Object value = delegateGet(i); 303 stringBuffer.append(String.valueOf(value)); 304 if (++i < size) 305 { 306 stringBuffer.append(", "); 307 } 308 } 309 stringBuffer.append("]"); 310 return stringBuffer.toString(); 311 } 312 } 313 314 317 public static class EStoreFeatureMap extends DelegatingFeatureMap 318 { 319 protected final InternalEObject.EStore store; 320 321 public EStoreFeatureMap(InternalEObject owner, EStructuralFeature eStructuralFeature, InternalEObject.EStore store) 322 { 323 super(owner, eStructuralFeature); 324 this.store = store; 325 } 326 327 protected List delegateList() 328 { 329 throw new UnsupportedOperationException (); 330 } 331 332 public EStructuralFeature getEStructuralFeature() 333 { 334 return eStructuralFeature; 335 } 336 337 protected void delegateAdd(int index, Object object) 338 { 339 store.add(owner, eStructuralFeature, index, object); 340 } 341 342 protected void delegateAdd(Object object) 343 { 344 delegateAdd(delegateSize(), object); 345 } 346 347 protected List delegateBasicList() 348 { 349 int size = delegateSize(); 350 if (size == 0) 351 { 352 return ECollections.EMPTY_ELIST; 353 } 354 else 355 { 356 Object [] data = store.toArray(owner, eStructuralFeature); 357 return new EcoreEList.UnmodifiableEList(owner, eStructuralFeature, data.length, data); 358 } 359 } 360 361 protected void delegateClear() 362 { 363 store.clear(owner, eStructuralFeature); 364 } 365 366 protected boolean delegateContains(Object object) 367 { 368 return store.contains(owner, eStructuralFeature, object); 369 } 370 371 protected boolean delegateContainsAll(Collection collection) 372 { 373 for (Iterator i = collection.iterator(); i.hasNext(); ) 374 { 375 if (!delegateContains(i.next())) 376 { 377 return false; 378 } 379 } 380 return true; 381 } 382 383 protected Object delegateGet(int index) 384 { 385 return store.get(owner, eStructuralFeature, index); 386 } 387 388 protected int delegateHashCode() 389 { 390 return store.hashCode(owner, eStructuralFeature); 391 } 392 393 protected int delegateIndexOf(Object object) 394 { 395 return store.indexOf(owner, eStructuralFeature, object); 396 } 397 398 protected boolean delegateIsEmpty() 399 { 400 return store.isEmpty(owner, eStructuralFeature); 401 } 402 403 protected Iterator delegateIterator() 404 { 405 return iterator(); 406 } 407 408 protected int delegateLastIndexOf(Object object) 409 { 410 return store.lastIndexOf(owner, eStructuralFeature, object); 411 } 412 413 protected ListIterator delegateListIterator() 414 { 415 return listIterator(); 416 } 417 418 protected Object delegateRemove(int index) 419 { 420 return store.remove(owner, eStructuralFeature, index); 421 } 422 423 protected Object delegateSet(int index, Object object) 424 { 425 return store.set(owner, eStructuralFeature, index, object); 426 } 427 428 protected int delegateSize() 429 { 430 return store.size(owner, eStructuralFeature); 431 } 432 433 protected Object [] delegateToArray() 434 { 435 return store.toArray(owner, eStructuralFeature); 436 } 437 438 protected Object [] delegateToArray(Object [] array) 439 { 440 return store.toArray(owner, eStructuralFeature, array); 441 } 442 443 protected String delegateToString() 444 { 445 StringBuffer stringBuffer = new StringBuffer (); 446 stringBuffer.append("["); 447 for (int i = 0, size = size(); i < size; ) 448 { 449 Object value = delegateGet(i); 450 stringBuffer.append(String.valueOf(value)); 451 if (++i < size) 452 { 453 stringBuffer.append(", "); 454 } 455 } 456 stringBuffer.append("]"); 457 return stringBuffer.toString(); 458 } 459 } 460 461 protected static final Object [] ENO_SETTINGS = new Object [0]; 462 protected static final InternalEObject EUNINITIALIZED_CONTAINER = new EObjectImpl(); 463 464 protected Object [] eSettings; 465 protected InternalEObject.EStore eStore; 466 467 470 public EStoreEObjectImpl() 471 { 472 super(); 473 eContainer = EUNINITIALIZED_CONTAINER; 474 } 475 476 479 public EStoreEObjectImpl(InternalEObject.EStore eStore) 480 { 481 super(); 482 eSetStore(eStore); 483 eContainer = EUNINITIALIZED_CONTAINER; 484 } 485 486 489 public EStoreEObjectImpl(EClass eClass) 490 { 491 super(); 492 eSetClass(eClass); 493 eContainer = EUNINITIALIZED_CONTAINER; 494 } 495 496 499 public EStoreEObjectImpl(EClass eClass, InternalEObject.EStore eStore) 500 { 501 super(); 502 eSetClass(eClass); 503 eSetStore(eStore); 504 eContainer = EUNINITIALIZED_CONTAINER; 505 } 506 507 protected boolean eIsCaching() 508 { 509 return true; 510 } 511 512 public Object dynamicGet(int dynamicFeatureID) 513 { 514 Object result = eSettings[dynamicFeatureID]; 515 if (result == null) 516 { 517 EStructuralFeature eStructuralFeature = eDynamicFeature(dynamicFeatureID); 518 if (!eStructuralFeature.isTransient()) 519 { 520 if (FeatureMapUtil.isFeatureMap(eStructuralFeature)) 521 { 522 eSettings[dynamicFeatureID] = result = createFeatureMap(eStructuralFeature); 523 } 524 else if (eStructuralFeature.isMany()) 525 { 526 eSettings[dynamicFeatureID] = result = createList(eStructuralFeature); 527 } 528 else 529 { 530 result = eStore().get(this, eStructuralFeature, InternalEObject.EStore.NO_INDEX); 531 if (eIsCaching()) 532 { 533 eSettings[dynamicFeatureID] = result; 534 } 535 } 536 } 537 } 538 return result; 539 } 540 541 public void dynamicSet(int dynamicFeatureID, Object value) 542 { 543 EStructuralFeature eStructuralFeature = eDynamicFeature(dynamicFeatureID); 544 if (eStructuralFeature.isTransient()) 545 { 546 eSettings[dynamicFeatureID] = value; 547 } 548 else 549 { 550 eStore().set(this, eStructuralFeature, InternalEObject.EStore.NO_INDEX, value == NIL ? null : value); 551 if (eIsCaching()) 552 { 553 eSettings[dynamicFeatureID] = value; 554 } 555 } 556 } 557 558 public void dynamicUnset(int dynamicFeatureID) 559 { 560 eStore().unset(this, eDynamicFeature(dynamicFeatureID)); 561 eSettings[dynamicFeatureID] = null; 562 } 563 564 public boolean eDynamicIsSet(EStructuralFeature eStructuralFeature) 565 { 566 return 567 eStructuralFeature.isTransient() ? 568 super.eDynamicIsSet(eStructuralFeature) : 569 eStore().isSet(this, eStructuralFeature); 570 } 571 572 protected EList createList(EStructuralFeature eStructuralFeature) 573 { 574 return new EStoreEList(this, eStructuralFeature, eStore()); 575 } 576 577 protected FeatureMap createFeatureMap(EStructuralFeature eStructuralFeature) 578 { 579 return new EStoreFeatureMap(this, eStructuralFeature, eStore()); 580 } 581 582 public EObject eContainer() 583 { 584 if (eContainer == EUNINITIALIZED_CONTAINER) 585 { 586 eInitializeContainer(); 587 } 588 589 return eContainer; 590 } 591 592 public int eContainerFeatureID() 593 { 594 if (eContainer == EUNINITIALIZED_CONTAINER) 595 { 596 eInitializeContainer(); 597 } 598 599 return eContainerFeatureID; 600 } 601 602 protected void eInitializeContainer() 603 { 604 eContainer = eStore().getContainer(this); 605 if (eContainer != null) 606 { 607 EStructuralFeature eContainingFeature = eStore().getContainingFeature(this); 608 if (eContainingFeature instanceof EReference) 609 { 610 EReference eContainingReference = (EReference)eContainingFeature; 611 EReference eOpposite = eContainingReference.getEOpposite(); 612 if (eOpposite != null) 613 { 614 eContainerFeatureID = eClass().getFeatureID(eOpposite); 615 return; 616 } 617 } 618 619 eContainerFeatureID = EOPPOSITE_FEATURE_BASE - eContainer.eClass().getFeatureID(eContainingFeature); 620 } 621 } 622 623 public InternalEObject.EStore eStore() 624 { 625 return eStore; 626 } 627 628 public void eSetStore(InternalEObject.EStore store) 629 { 630 this.eStore = store; 631 } 632 633 protected int eStaticFeatureCount() 634 { 635 return 0; 636 } 637 638 public int eDerivedStructuralFeatureID(EStructuralFeature eStructuralFeature) 639 { 640 return eClass().getFeatureID(eStructuralFeature); 641 } 642 643 protected BasicEObjectImpl.EPropertiesHolder eProperties() 644 { 645 if (eProperties == null) 646 { 647 eProperties = new EStoreEPropertiesHolderImpl(); 648 } 649 return eProperties; 650 } 651 652 protected boolean eHasSettings() 653 { 654 return eSettings != null; 655 } 656 657 protected EStructuralFeature.Internal.DynamicValueHolder eSettings() 658 { 659 if (eSettings == null) 660 { 661 int size = eClass().getFeatureCount() - eStaticFeatureCount(); 662 eSettings = size == 0 ? ENO_SETTINGS : new Object [size]; 663 } 664 665 return this; 666 } 667 668 676 677 678 681 public static class EStoreImpl implements InternalEObject.EStore 682 { 683 686 protected Map map = new HashMap (); 687 688 public static class Entry 689 { 690 protected EObject eObject; 691 protected EStructuralFeature eStructuralFeature; 692 693 public Entry(InternalEObject eObject, EStructuralFeature eStructuralFeature) 694 { 695 this.eObject = eObject; 696 this.eStructuralFeature = eStructuralFeature; 697 } 698 699 public boolean equals(Object that) 700 { 701 if (that instanceof Entry) 702 { 703 Entry entry = (Entry)that; 704 return eObject == entry.eObject && eStructuralFeature == entry.eStructuralFeature; 705 } 706 else 707 { 708 return false; 709 } 710 } 711 712 public int hashCode() 713 { 714 return eObject.hashCode() ^ eStructuralFeature.hashCode(); 715 } 716 } 717 718 protected EList getList(Entry entry) 719 { 720 EList result = (EList)map.get(entry); 721 if (result == null) 722 { 723 result = new BasicEList(); 724 map.put(entry, result); 725 } 726 return result; 727 } 728 729 public Object get(InternalEObject eObject, EStructuralFeature feature, int index) 730 { 731 Entry entry = new Entry(eObject, feature); 732 if (index == NO_INDEX) 733 { 734 return map.get(entry); 735 } 736 else 737 { 738 return getList(entry).get(index); 739 } 740 } 741 742 public Object set(InternalEObject eObject, EStructuralFeature feature, int index, Object value) 743 { 744 752 Entry entry = new Entry(eObject, feature); 753 if (index == NO_INDEX) 754 { 755 return map.put(entry, value); 756 } 757 else 758 { 759 List list = (List )map.get(entry); 760 return list.set(index, value); 761 } 762 } 763 764 public void add(InternalEObject eObject, EStructuralFeature feature, int index, Object value) 765 { 766 Entry entry = new Entry(eObject, feature); 774 getList(entry).add(index, value); 775 } 776 777 public Object remove(InternalEObject eObject, EStructuralFeature feature, int index) 778 { 779 Entry entry = new Entry(eObject, feature); 780 return getList(entry).remove(index); 781 } 782 783 public Object move(InternalEObject eObject, EStructuralFeature feature, int targetIndex, int sourceIndex) 784 { 785 Entry entry = new Entry(eObject, feature); 786 return getList(entry).move(targetIndex, sourceIndex); 787 } 788 789 public void clear(InternalEObject eObject, EStructuralFeature feature) 790 { 791 Entry entry = new Entry(eObject, feature); 792 map.remove(entry); 793 } 795 796 public boolean isSet(InternalEObject eObject, EStructuralFeature feature) 797 { 798 Entry entry = new Entry(eObject, feature); 799 return map.containsKey(entry); 800 } 801 802 public void unset(InternalEObject eObject, EStructuralFeature feature) 803 { 804 Entry entry = new Entry(eObject, feature); 805 map.remove(entry); 806 } 807 808 public int size(InternalEObject eObject, EStructuralFeature feature) 809 { 810 Entry entry = new Entry(eObject, feature); 811 return getList(entry).size(); 812 } 813 814 public int indexOf(InternalEObject eObject, EStructuralFeature feature, Object value) 815 { 816 Entry entry = new Entry(eObject, feature); 817 return getList(entry).indexOf(value); 818 } 819 820 public int lastIndexOf(InternalEObject eObject, EStructuralFeature feature, Object value) 821 { 822 Entry entry = new Entry(eObject, feature); 823 return getList(entry).lastIndexOf(value); 824 } 825 826 public Object [] toArray(InternalEObject eObject, EStructuralFeature feature) 827 { 828 Entry entry = new Entry(eObject, feature); 829 return getList(entry).toArray(); 830 } 831 832 public Object [] toArray(InternalEObject eObject, EStructuralFeature feature, Object [] array) 833 { 834 Entry entry = new Entry(eObject, feature); 835 return getList(entry).toArray(array); 836 } 837 838 public boolean isEmpty(InternalEObject eObject, EStructuralFeature feature) 839 { 840 Entry entry = new Entry(eObject, feature); 841 return getList(entry).isEmpty(); 842 } 843 844 public boolean contains(InternalEObject eObject, EStructuralFeature feature, Object value) 845 { 846 Entry entry = new Entry(eObject, feature); 847 return getList(entry).contains(value); 848 } 849 850 public int hashCode(InternalEObject eObject, EStructuralFeature feature) 851 { 852 Entry entry = new Entry(eObject, feature); 853 return getList(entry).hashCode(); 854 } 855 856 public InternalEObject getContainer(InternalEObject eObject) 857 { 858 return null; 859 860 } 863 864 public EStructuralFeature getContainingFeature(InternalEObject eObject) 865 { 866 throw new UnsupportedOperationException (); 869 } 872 873 public EObject create(EClass eClass) 874 { 875 InternalEObject result = new EStoreEObjectImpl(eClass, this); 876 return result; 877 } 878 } 879 } 880 | Popular Tags |