KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > change > impl > ListChangeImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2003-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: ListChangeImpl.java,v 1.7 2005/06/08 06:16:16 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.change.impl;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
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 /**
53  * <!-- begin-user-doc -->
54  * An implementation of the model object '<em><b>List Change</b></em>'.
55  * <!-- end-user-doc -->
56  * <p>
57  * The following features are implemented:
58  * <ul>
59  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getKind <em>Kind</em>}</li>
60  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getDataValues <em>Data Values</em>}</li>
61  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getIndex <em>Index</em>}</li>
62  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getMoveToIndex <em>Move To Index</em>}</li>
63  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getValues <em>Values</em>}</li>
64  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getReferenceValues <em>Reference Values</em>}</li>
65  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getFeature <em>Feature</em>}</li>
66  * <li>{@link org.eclipse.emf.ecore.change.impl.ListChangeImpl#getFeatureMapEntryValues <em>Feature Map Entry Values</em>}</li>
67  * </ul>
68  * </p>
69  *
70  * @generated
71  */

72 public class ListChangeImpl extends EObjectImpl implements ListChange
73 {
74   /**
75    * The default value of the '{@link #getKind() <em>Kind</em>}' attribute.
76    * <!-- begin-user-doc -->
77    * <!-- end-user-doc -->
78    * @see #getKind()
79    * @generated
80    * @ordered
81    */

82   protected static final ChangeKind KIND_EDEFAULT = ChangeKind.ADD_LITERAL;
83
84   /**
85    * The cached value of the '{@link #getKind() <em>Kind</em>}' attribute.
86    * <!-- begin-user-doc -->
87    * <!-- end-user-doc -->
88    * @see #getKind()
89    * @generated
90    * @ordered
91    */

92   protected ChangeKind kind = KIND_EDEFAULT;
93
94   /**
95    * The cached value of the '{@link #getDataValues() <em>Data Values</em>}' attribute list.
96    * <!-- begin-user-doc -->
97    * <!-- end-user-doc -->
98    * @see #getDataValues()
99    * @generated
100    * @ordered
101    */

102   protected EList dataValues = null;
103
104   /**
105    * The default value of the '{@link #getIndex() <em>Index</em>}' attribute.
106    * <!-- begin-user-doc -->
107    * <!-- end-user-doc -->
108    * @see #getIndex()
109    * @generated
110    * @ordered
111    */

112   protected static final int INDEX_EDEFAULT = -1;
113
114   /**
115    * The cached value of the '{@link #getIndex() <em>Index</em>}' attribute.
116    * <!-- begin-user-doc -->
117    * <!-- end-user-doc -->
118    * @see #getIndex()
119    * @generated
120    * @ordered
121    */

122   protected int index = INDEX_EDEFAULT;
123
124   /**
125    * The default value of the '{@link #getMoveToIndex() <em>Move To Index</em>}' attribute.
126    * <!-- begin-user-doc -->
127    * <!-- end-user-doc -->
128    * @see #getMoveToIndex()
129    * @generated
130    * @ordered
131    */

132   protected static final int MOVE_TO_INDEX_EDEFAULT = 0;
133
134   /**
135    * The cached value of the '{@link #getMoveToIndex() <em>Move To Index</em>}' attribute.
136    * <!-- begin-user-doc -->
137    * <!-- end-user-doc -->
138    * @see #getMoveToIndex()
139    * @generated
140    * @ordered
141    */

142   protected int moveToIndex = MOVE_TO_INDEX_EDEFAULT;
143
144   /**
145    * The cached value of the '{@link #getReferenceValues() <em>Reference Values</em>}' reference list.
146    * <!-- begin-user-doc -->
147    * <!-- end-user-doc -->
148    * @see #getReferenceValues()
149    * @generated
150    * @ordered
151    */

152   protected EList referenceValues = null;
153
154   /**
155    * The cached value of the '{@link #getFeature() <em>Feature</em>}' reference.
156    * <!-- begin-user-doc -->
157    * <!-- end-user-doc -->
158    * @see #getFeature()
159    * @generated
160    * @ordered
161    */

162   protected EStructuralFeature feature = null;
163   
164   /**
165    * The cached value of the '{@link #getFeatureMapEntryValues() <em>Feature Map Entry Values</em>}' containment reference list.
166    * <!-- begin-user-doc -->
167    * <!-- end-user-doc -->
168    * @see #getFeatureMapEntryValues()
169    * @generated
170    * @ordered
171    */

172   protected EList featureMapEntryValues = null;
173
174   /**
175    * The data value delegating list is used to ensure that the elements
176    * are properly converted to and from strings when added and removed
177    * from the dataValues list.
178    */

179   protected EList dataValueDelegatingList = null;
180
181   /**
182    * <!-- begin-user-doc -->
183    * <!-- end-user-doc -->
184    * @generated
185    */

186   protected ListChangeImpl()
187   {
188     super();
189   }
190
191   /**
192    * <!-- begin-user-doc -->
193    * <!-- end-user-doc -->
194    * @generated
195    */

196   protected EClass eStaticClass()
197   {
198     return ChangePackage.eINSTANCE.getListChange();
199   }
200
201   /**
202    * <!-- begin-user-doc -->
203    * <!-- end-user-doc -->
204    * @generated
205    */

206   public ChangeKind getKind()
207   {
208     return kind;
209   }
210
211   /**
212    * <!-- begin-user-doc -->
213    * <!-- end-user-doc -->
214    * @generated
215    */

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   /**
225    * <!-- begin-user-doc -->
226    * <!-- end-user-doc -->
227    * @generated
228    */

229   public EList getDataValues()
230   {
231     if (dataValues == null)
232     {
233       dataValues = new EDataTypeUniqueEList(String JavaDoc.class, this, ChangePackage.LIST_CHANGE__DATA_VALUES);
234     }
235     return dataValues;
236   }
237
238   /**
239    * Creates the data value delegating list
240    */

241   protected EList createDataValueDelegatingList()
242   {
243     if (FeatureMapUtil.isFeatureMap(getFeature()))
244     {
245       return
246         new DelegatingEList()
247         {
248           protected List JavaDoc delegateList()
249           {
250             return getFeatureMapEntryValues();
251           }
252           
253           protected Object JavaDoc validate(int index, Object JavaDoc 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 JavaDoc delegateList()
273           {
274             return getDataValues();
275           }
276             
277           protected Object JavaDoc resolve(int index, Object JavaDoc object)
278           {
279             EDataType type = (EDataType)getFeature().getEType();
280             return EcoreUtil.createFromString(type, (String JavaDoc)object);
281           }
282             
283           protected Object JavaDoc validate(int index, Object JavaDoc 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 JavaDoc value)
293   {
294     return ChangeFactory.eINSTANCE.createFeatureMapEntry(feature, value);
295   }
296
297   /**
298    * <!-- begin-user-doc -->
299    * <!-- end-user-doc -->
300    * @generated
301    */

302   public int getIndex()
303   {
304     return index;
305   }
306
307   /**
308    * <!-- begin-user-doc -->
309    * <!-- end-user-doc -->
310    * @generated
311    */

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   /**
321    * <!-- begin-user-doc -->
322    * <!-- end-user-doc -->
323    * @generated
324    */

325   public int getMoveToIndex()
326   {
327     return moveToIndex;
328   }
329
330   /**
331    * <!-- begin-user-doc -->
332    * <!-- end-user-doc -->
333    * @generated
334    */

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   /**
344    * <!-- begin-user-doc -->
345    * <!-- end-user-doc -->
346    * @generated
347    */

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   /**
358    * <!-- begin-user-doc -->
359    * <!-- end-user-doc -->
360    * @generated
361    */

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   /**
392    * <!-- begin-user-doc -->
393    * <!-- end-user-doc -->
394    * @generated
395    */

396   public EStructuralFeature basicGetFeature()
397   {
398     return feature;
399   }
400
401   /**
402    * <!-- begin-user-doc -->
403    * <!-- end-user-doc -->
404    * @generated NOT
405    */

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   /**
415    * <!-- begin-user-doc -->
416    * <!-- end-user-doc -->
417    * @generated
418    */

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   /**
429    * <!-- begin-user-doc -->
430    * <!-- end-user-doc -->
431    * @generated NOT
432    */

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   /**
451    * <!-- begin-user-doc -->
452    * <!-- end-user-doc -->
453    * @generated NOT
454    */

455   public void setValues(EList values)
456   {
457     EList featureValues = getValues();
458     featureValues.clear();
459     featureValues.addAll(values);
460   }
461
462   /**
463    * <!-- begin-user-doc -->
464    * <!-- end-user-doc -->
465    * @generated NOT
466    */

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   /**
495    * <!-- begin-user-doc -->
496    * <!-- end-user-doc -->
497    * @generated NOT
498    */

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   /**
541    * <!-- begin-user-doc -->
542    * <!-- end-user-doc -->
543    * @generated
544    */

545   public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class JavaDoc 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   /**
561    * <!-- begin-user-doc -->
562    * <!-- end-user-doc -->
563    * @generated
564    */

565   public Object JavaDoc 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 JavaDoc(getIndex());
575       case ChangePackage.LIST_CHANGE__MOVE_TO_INDEX:
576         return new Integer JavaDoc(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   /**
591    * <!-- begin-user-doc -->
592    * <!-- end-user-doc -->
593    * @generated
594    */

595   public void eSet(EStructuralFeature eFeature, Object JavaDoc 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 JavaDoc)newValue);
605         return;
606       case ChangePackage.LIST_CHANGE__INDEX:
607         setIndex(((Integer JavaDoc)newValue).intValue());
608         return;
609       case ChangePackage.LIST_CHANGE__MOVE_TO_INDEX:
610         setMoveToIndex(((Integer JavaDoc)newValue).intValue());
611         return;
612       case ChangePackage.LIST_CHANGE__VALUES:
613         getValues().clear();
614         getValues().addAll((Collection JavaDoc)newValue);
615         return;
616       case ChangePackage.LIST_CHANGE__REFERENCE_VALUES:
617         getReferenceValues().clear();
618         getReferenceValues().addAll((Collection JavaDoc)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 JavaDoc)newValue);
626         return;
627     }
628     eDynamicSet(eFeature, newValue);
629   }
630
631   /**
632    * <!-- begin-user-doc -->
633    * <!-- end-user-doc -->
634    * @generated
635    */

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   /**
669    * <!-- begin-user-doc -->
670    * <!-- end-user-doc -->
671    * @generated
672    */

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   /**
698    * <!-- begin-user-doc -->
699    * <!-- end-user-doc -->
700    * @generated
701    */

702   public String JavaDoc toString()
703   {
704     if (eIsProxy()) return super.toString();
705
706     StringBuffer JavaDoc result = new StringBuffer JavaDoc(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 } //ListChangeImpl
720
Popular Tags