1 17 package org.eclipse.emf.ecore.change.impl; 18 19 20 import java.util.Collection ; 21 import java.util.List ; 22 23 import org.eclipse.emf.common.notify.Notification; 24 import org.eclipse.emf.common.notify.NotificationChain; 25 26 import org.eclipse.emf.common.util.DelegatingEList; 27 import org.eclipse.emf.common.util.EList; 28 import org.eclipse.emf.ecore.EAttribute; 29 import org.eclipse.emf.ecore.EClass; 30 import org.eclipse.emf.ecore.EDataType; 31 import org.eclipse.emf.ecore.EObject; 32 import org.eclipse.emf.ecore.EStructuralFeature; 33 import org.eclipse.emf.ecore.InternalEObject; 34 import org.eclipse.emf.ecore.change.ChangeFactory; 35 import org.eclipse.emf.ecore.change.ChangeKind; 36 import org.eclipse.emf.ecore.change.ChangePackage; 37 import org.eclipse.emf.ecore.change.FeatureMapEntry; 38 import org.eclipse.emf.ecore.change.FeatureChange; 39 import org.eclipse.emf.ecore.change.ListChange; 40 import org.eclipse.emf.ecore.impl.ENotificationImpl; 41 import org.eclipse.emf.ecore.impl.EObjectImpl; 42 import org.eclipse.emf.ecore.util.EDataTypeUniqueEList; 43 import org.eclipse.emf.ecore.util.EObjectContainmentEList; 44 import org.eclipse.emf.ecore.util.EObjectResolvingEList; 45 import org.eclipse.emf.ecore.util.FeatureMap; 46 import org.eclipse.emf.ecore.util.FeatureMapUtil; 47 import org.eclipse.emf.ecore.util.InternalEList; 48 49 import org.eclipse.emf.ecore.util.EcoreUtil; 50 51 52 72 public class ListChangeImpl extends EObjectImpl implements ListChange 73 { 74 82 protected static final ChangeKind KIND_EDEFAULT = ChangeKind.ADD_LITERAL; 83 84 92 protected ChangeKind kind = KIND_EDEFAULT; 93 94 102 protected EList dataValues = null; 103 104 112 protected static final int INDEX_EDEFAULT = -1; 113 114 122 protected int index = INDEX_EDEFAULT; 123 124 132 protected static final int MOVE_TO_INDEX_EDEFAULT = 0; 133 134 142 protected int moveToIndex = MOVE_TO_INDEX_EDEFAULT; 143 144 152 protected EList referenceValues = null; 153 154 162 protected EStructuralFeature feature = null; 163 164 172 protected EList featureMapEntryValues = null; 173 174 179 protected EList dataValueDelegatingList = null; 180 181 186 protected ListChangeImpl() 187 { 188 super(); 189 } 190 191 196 protected EClass eStaticClass() 197 { 198 return ChangePackage.eINSTANCE.getListChange(); 199 } 200 201 206 public ChangeKind getKind() 207 { 208 return kind; 209 } 210 211 216 public void setKind(ChangeKind newKind) 217 { 218 ChangeKind oldKind = kind; 219 kind = newKind == null ? KIND_EDEFAULT : newKind; 220 if (eNotificationRequired()) 221 eNotify(new ENotificationImpl(this, Notification.SET, ChangePackage.LIST_CHANGE__KIND, oldKind, kind)); 222 } 223 224 229 public EList getDataValues() 230 { 231 if (dataValues == null) 232 { 233 dataValues = new EDataTypeUniqueEList(String .class, this, ChangePackage.LIST_CHANGE__DATA_VALUES); 234 } 235 return dataValues; 236 } 237 238 241 protected EList createDataValueDelegatingList() 242 { 243 if (FeatureMapUtil.isFeatureMap(getFeature())) 244 { 245 return 246 new DelegatingEList() 247 { 248 protected List delegateList() 249 { 250 return getFeatureMapEntryValues(); 251 } 252 253 protected Object validate(int index, Object object) 254 { 255 if (object instanceof FeatureMapEntry) 256 { 257 return object; 258 } 259 else 260 { 261 FeatureMap.Entry entry = (FeatureMap.Entry)object; 262 return createFeatureMapEntry(entry.getEStructuralFeature(), entry.getValue()); 263 } 264 } 265 }; 266 } 267 else 268 { 269 return 270 new DelegatingEList() 271 { 272 protected List delegateList() 273 { 274 return getDataValues(); 275 } 276 277 protected Object resolve(int index, Object object) 278 { 279 EDataType type = (EDataType)getFeature().getEType(); 280 return EcoreUtil.createFromString(type, (String )object); 281 } 282 283 protected Object validate(int index, Object object) 284 { 285 EDataType type = (EDataType)getFeature().getEType(); 286 return EcoreUtil.convertToString(type, object); 287 } 288 }; 289 } 290 } 291 292 protected FeatureMapEntry createFeatureMapEntry(EStructuralFeature feature, Object value) 293 { 294 return ChangeFactory.eINSTANCE.createFeatureMapEntry(feature, value); 295 } 296 297 302 public int getIndex() 303 { 304 return index; 305 } 306 307 312 public void setIndex(int newIndex) 313 { 314 int oldIndex = index; 315 index = newIndex; 316 if (eNotificationRequired()) 317 eNotify(new ENotificationImpl(this, Notification.SET, ChangePackage.LIST_CHANGE__INDEX, oldIndex, index)); 318 } 319 320 325 public int getMoveToIndex() 326 { 327 return moveToIndex; 328 } 329 330 335 public void setMoveToIndex(int newMoveToIndex) 336 { 337 int oldMoveToIndex = moveToIndex; 338 moveToIndex = newMoveToIndex; 339 if (eNotificationRequired()) 340 eNotify(new ENotificationImpl(this, Notification.SET, ChangePackage.LIST_CHANGE__MOVE_TO_INDEX, oldMoveToIndex, moveToIndex)); 341 } 342 343 348 public EList getReferenceValues() 349 { 350 if (referenceValues == null) 351 { 352 referenceValues = new EObjectResolvingEList(EObject.class, this, ChangePackage.LIST_CHANGE__REFERENCE_VALUES); 353 } 354 return referenceValues; 355 } 356 357 362 public EStructuralFeature getFeatureGen() 363 { 364 if (feature != null && feature.eIsProxy()) 365 { 366 EStructuralFeature oldFeature = feature; 367 feature = (EStructuralFeature)eResolveProxy((InternalEObject)feature); 368 if (feature != oldFeature) 369 { 370 if (eNotificationRequired()) 371 eNotify(new ENotificationImpl(this, Notification.RESOLVE, ChangePackage.LIST_CHANGE__FEATURE, oldFeature, feature)); 372 } 373 } 374 return feature; 375 } 376 377 public EStructuralFeature getFeature() 378 { 379 EStructuralFeature result = getFeatureGen(); 380 if (result == null) 381 { 382 EObject container = eContainer(); 383 if (container instanceof FeatureChange) 384 { 385 result = ((FeatureChange)container).getFeature(); 386 } 387 } 388 return result; 389 } 390 391 396 public EStructuralFeature basicGetFeature() 397 { 398 return feature; 399 } 400 401 406 public void setFeature(EStructuralFeature newFeature) 407 { 408 EStructuralFeature oldFeature = feature; 409 feature = newFeature; 410 if (eNotificationRequired()) 411 eNotify(new ENotificationImpl(this, Notification.SET, ChangePackage.LIST_CHANGE__FEATURE, oldFeature, feature)); 412 } 413 414 419 public EList getFeatureMapEntryValues() 420 { 421 if (featureMapEntryValues == null) 422 { 423 featureMapEntryValues = new EObjectContainmentEList(FeatureMapEntry.class, this, ChangePackage.LIST_CHANGE__FEATURE_MAP_ENTRY_VALUES); 424 } 425 return featureMapEntryValues; 426 } 427 428 433 public EList getValues() 434 { 435 EStructuralFeature feature = getFeature(); 436 if (feature instanceof EAttribute) 437 { 438 if (dataValueDelegatingList == null) 439 { 440 dataValueDelegatingList = createDataValueDelegatingList(); 441 } 442 return dataValueDelegatingList; 443 } 444 else 445 { 446 return getReferenceValues(); 447 } 448 } 449 450 455 public void setValues(EList values) 456 { 457 EList featureValues = getValues(); 458 featureValues.clear(); 459 featureValues.addAll(values); 460 } 461 462 467 public void apply(EList originalList) 468 { 469 switch (getKind().getValue()) 470 { 471 case ChangeKind.ADD: 472 if (index == -1) 473 { 474 originalList.addAll(getValues()); 475 } 476 else 477 { 478 originalList.addAll(index, getValues()); 479 } 480 break; 481 case ChangeKind.REMOVE: 482 int removeCount = getValues().isEmpty() ? 1 : getValues().size(); 483 for (int i = 0; i < removeCount; ++i) 484 { 485 originalList.remove(index); 486 } 487 break; 488 case ChangeKind.MOVE: 489 originalList.move(moveToIndex, index); 490 break; 491 } 492 } 493 494 499 public void applyAndReverse(EList originalList) 500 { 501 switch (getKind().getValue()) 502 { 503 case ChangeKind.ADD: 504 if (index == -1) 505 { 506 index = originalList.size(); 507 originalList.addAll(getValues()); 508 } 509 else 510 { 511 originalList.addAll(index, getValues()); 512 } 513 if (getValues().size() == 1) 514 { 515 getValues().clear(); 516 } 517 setKind(ChangeKind.REMOVE_LITERAL); 518 break; 519 case ChangeKind.REMOVE: 520 if (getValues().isEmpty()) 521 { 522 getValues().add(originalList.get(getIndex())); 523 } 524 int removeCount = getValues().size(); 525 for (int i = 0; i < removeCount; ++i) 526 { 527 originalList.remove(index); 528 } 529 setKind(ChangeKind.ADD_LITERAL); 530 break; 531 case ChangeKind.MOVE: 532 originalList.move(moveToIndex, index); 533 int temp = moveToIndex; 534 setMoveToIndex(index); 535 setIndex(temp); 536 break; 537 } 538 } 539 540 545 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 546 { 547 if (featureID >= 0) 548 { 549 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 550 { 551 case ChangePackage.LIST_CHANGE__FEATURE_MAP_ENTRY_VALUES: 552 return ((InternalEList)getFeatureMapEntryValues()).basicRemove(otherEnd, msgs); 553 default: 554 return eDynamicInverseRemove(otherEnd, featureID, baseClass, msgs); 555 } 556 } 557 return eBasicSetContainer(null, featureID, msgs); 558 } 559 560 565 public Object eGet(EStructuralFeature eFeature, boolean resolve) 566 { 567 switch (eDerivedStructuralFeatureID(eFeature)) 568 { 569 case ChangePackage.LIST_CHANGE__KIND: 570 return getKind(); 571 case ChangePackage.LIST_CHANGE__DATA_VALUES: 572 return getDataValues(); 573 case ChangePackage.LIST_CHANGE__INDEX: 574 return new Integer (getIndex()); 575 case ChangePackage.LIST_CHANGE__MOVE_TO_INDEX: 576 return new Integer (getMoveToIndex()); 577 case ChangePackage.LIST_CHANGE__VALUES: 578 return getValues(); 579 case ChangePackage.LIST_CHANGE__REFERENCE_VALUES: 580 return getReferenceValues(); 581 case ChangePackage.LIST_CHANGE__FEATURE: 582 if (resolve) return getFeature(); 583 return basicGetFeature(); 584 case ChangePackage.LIST_CHANGE__FEATURE_MAP_ENTRY_VALUES: 585 return getFeatureMapEntryValues(); 586 } 587 return eDynamicGet(eFeature, resolve); 588 } 589 590 595 public void eSet(EStructuralFeature eFeature, Object newValue) 596 { 597 switch (eDerivedStructuralFeatureID(eFeature)) 598 { 599 case ChangePackage.LIST_CHANGE__KIND: 600 setKind((ChangeKind)newValue); 601 return; 602 case ChangePackage.LIST_CHANGE__DATA_VALUES: 603 getDataValues().clear(); 604 getDataValues().addAll((Collection )newValue); 605 return; 606 case ChangePackage.LIST_CHANGE__INDEX: 607 setIndex(((Integer )newValue).intValue()); 608 return; 609 case ChangePackage.LIST_CHANGE__MOVE_TO_INDEX: 610 setMoveToIndex(((Integer )newValue).intValue()); 611 return; 612 case ChangePackage.LIST_CHANGE__VALUES: 613 getValues().clear(); 614 getValues().addAll((Collection )newValue); 615 return; 616 case ChangePackage.LIST_CHANGE__REFERENCE_VALUES: 617 getReferenceValues().clear(); 618 getReferenceValues().addAll((Collection )newValue); 619 return; 620 case ChangePackage.LIST_CHANGE__FEATURE: 621 setFeature((EStructuralFeature)newValue); 622 return; 623 case ChangePackage.LIST_CHANGE__FEATURE_MAP_ENTRY_VALUES: 624 getFeatureMapEntryValues().clear(); 625 getFeatureMapEntryValues().addAll((Collection )newValue); 626 return; 627 } 628 eDynamicSet(eFeature, newValue); 629 } 630 631 636 public void eUnset(EStructuralFeature eFeature) 637 { 638 switch (eDerivedStructuralFeatureID(eFeature)) 639 { 640 case ChangePackage.LIST_CHANGE__KIND: 641 setKind(KIND_EDEFAULT); 642 return; 643 case ChangePackage.LIST_CHANGE__DATA_VALUES: 644 getDataValues().clear(); 645 return; 646 case ChangePackage.LIST_CHANGE__INDEX: 647 setIndex(INDEX_EDEFAULT); 648 return; 649 case ChangePackage.LIST_CHANGE__MOVE_TO_INDEX: 650 setMoveToIndex(MOVE_TO_INDEX_EDEFAULT); 651 return; 652 case ChangePackage.LIST_CHANGE__VALUES: 653 getValues().clear(); 654 return; 655 case ChangePackage.LIST_CHANGE__REFERENCE_VALUES: 656 getReferenceValues().clear(); 657 return; 658 case ChangePackage.LIST_CHANGE__FEATURE: 659 setFeature((EStructuralFeature)null); 660 return; 661 case ChangePackage.LIST_CHANGE__FEATURE_MAP_ENTRY_VALUES: 662 getFeatureMapEntryValues().clear(); 663 return; 664 } 665 eDynamicUnset(eFeature); 666 } 667 668 673 public boolean eIsSet(EStructuralFeature eFeature) 674 { 675 switch (eDerivedStructuralFeatureID(eFeature)) 676 { 677 case ChangePackage.LIST_CHANGE__KIND: 678 return kind != KIND_EDEFAULT; 679 case ChangePackage.LIST_CHANGE__DATA_VALUES: 680 return dataValues != null && !dataValues.isEmpty(); 681 case ChangePackage.LIST_CHANGE__INDEX: 682 return index != INDEX_EDEFAULT; 683 case ChangePackage.LIST_CHANGE__MOVE_TO_INDEX: 684 return moveToIndex != MOVE_TO_INDEX_EDEFAULT; 685 case ChangePackage.LIST_CHANGE__VALUES: 686 return !getValues().isEmpty(); 687 case ChangePackage.LIST_CHANGE__REFERENCE_VALUES: 688 return referenceValues != null && !referenceValues.isEmpty(); 689 case ChangePackage.LIST_CHANGE__FEATURE: 690 return feature != null; 691 case ChangePackage.LIST_CHANGE__FEATURE_MAP_ENTRY_VALUES: 692 return featureMapEntryValues != null && !featureMapEntryValues.isEmpty(); 693 } 694 return eDynamicIsSet(eFeature); 695 } 696 697 702 public String toString() 703 { 704 if (eIsProxy()) return super.toString(); 705 706 StringBuffer result = new StringBuffer (super.toString()); 707 result.append(" (kind: "); 708 result.append(kind); 709 result.append(", dataValues: "); 710 result.append(dataValues); 711 result.append(", index: "); 712 result.append(index); 713 result.append(", moveToIndex: "); 714 result.append(moveToIndex); 715 result.append(')'); 716 return result.toString(); 717 } 718 719 } | Popular Tags |