KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > impl > ESuperAdapter


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-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: ESuperAdapter.java,v 1.4 2005/06/08 06:20:10 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.impl;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.emf.common.notify.Notification;
24 import org.eclipse.emf.common.notify.impl.AdapterImpl;
25 import org.eclipse.emf.common.util.EList;
26 import org.eclipse.emf.common.util.UniqueEList;
27 import org.eclipse.emf.ecore.EClass;
28 import org.eclipse.emf.ecore.EcorePackage;
29
30
31 /**
32  * An adapter implementation for maintaining {@link EClass}es
33  */

34 public class ESuperAdapter extends AdapterImpl
35 {
36   public interface Holder
37   {
38     ESuperAdapter getESuperAdapter();
39     boolean isFrozen();
40   }
41
42   /*
43    * @deprecated
44    */

45   public static ESuperAdapter getESuperAdapter(EClass eClass)
46   {
47     return ((Holder)eClass).getESuperAdapter();
48   }
49
50   protected EList subclasses;
51
52   protected static final int ATTRIBUTES_MODIFIED = 0x0001;
53   protected static final int REFERENCES_MODIFIED = 0x0002;
54   protected static final int STRUCTURAL_FEATURES_MODIFIED = 0x0004;
55   protected static final int CONTAINMENTS_MODIFIED = 0x0008;
56   protected static final int OPERATIONS_MODIFIED = 0x0010;
57   protected static final int SUPERS_MODIFIED = 0x0020;
58   protected static final int LAST_ESUPER_ADAPTER_MODIFIED = SUPERS_MODIFIED;
59
60   protected int modifiedState;
61
62   protected final static int SUPERS = 0;
63   protected final static int ATTRIBUTES = 1;
64   protected final static int REFERENCES = 2;
65   protected final static int OPERATIONS = 3;
66   protected final static int STRUCTURAL_FEATURES = 4;
67
68   protected static int getFeatureID(Notification notification)
69   {
70     int featureID = notification.getFeatureID(null);
71     switch (featureID)
72     {
73       case EcorePackage.ECLASS__ESUPER_TYPES: return SUPERS;
74       case EcorePackage.ECLASS__EATTRIBUTES: return ATTRIBUTES;
75       case EcorePackage.ECLASS__EREFERENCES: return REFERENCES;
76       case EcorePackage.ECLASS__EOPERATIONS: return OPERATIONS;
77       case EcorePackage.ECLASS__ESTRUCTURAL_FEATURES: return STRUCTURAL_FEATURES;
78     }
79     return -1;
80   }
81
82   public ESuperAdapter()
83   {
84     super();
85   }
86
87   public boolean isModified()
88   {
89     return modifiedState != 0;
90   }
91
92   public boolean isAdapterForType(Object JavaDoc type)
93   {
94     return type == ESuperAdapter.class;
95   }
96
97   public void notifyChanged(Notification notification)
98   {
99     int eventType = notification.getEventType();
100     if (eventType != Notification.REMOVING_ADAPTER)
101     {
102       int featureID = getFeatureID(notification);
103       if (featureID == SUPERS)
104       {
105         switch (eventType)
106         {
107           case Notification.SET:
108           {
109             Object JavaDoc oldValue = notification.getOldValue();
110             if (oldValue != null)
111             {
112               ESuperAdapter eSuperAdapter = ((Holder)oldValue).getESuperAdapter();
113               eSuperAdapter.getSubclasses().remove(notification.getNotifier());
114             }
115             Object JavaDoc newValue = notification.getNewValue();
116             if (newValue != null)
117             {
118               Holder holder = (Holder)newValue;
119               if (!holder.isFrozen())
120               {
121                 ESuperAdapter eSuperAdapter = holder.getESuperAdapter();
122                 eSuperAdapter.getSubclasses().add(notification.getNotifier());
123               }
124             }
125             break;
126           }
127           case Notification.RESOLVE:
128           case Notification.ADD:
129           {
130             Object JavaDoc newValue = notification.getNewValue();
131             if (newValue != null)
132             {
133               Holder holder = (Holder)newValue;
134               if (!holder.isFrozen())
135               {
136                 ESuperAdapter eSuperAdapter = holder.getESuperAdapter();
137                 eSuperAdapter.getSubclasses().add(notification.getNotifier());
138               }
139             }
140             break;
141           }
142           case Notification.ADD_MANY:
143           {
144             Object JavaDoc newValue = notification.getNewValue();
145             if (newValue != null)
146             {
147               for (Iterator JavaDoc i = ((Collection JavaDoc)newValue).iterator(); i.hasNext(); )
148               {
149                 Holder holder = (Holder)i.next();
150                 if (!holder.isFrozen())
151                 {
152                   ESuperAdapter eSuperAdapter = holder.getESuperAdapter();
153                   eSuperAdapter.getSubclasses().add(notification.getNotifier());
154                 }
155               }
156             }
157             break;
158           }
159           case Notification.REMOVE:
160           {
161             Object JavaDoc oldValue = notification.getOldValue();
162             if (oldValue != null)
163             {
164               Holder holder = (Holder)oldValue;
165               if (!holder.isFrozen())
166               {
167                 ESuperAdapter eSuperAdapter = holder.getESuperAdapter();
168                 eSuperAdapter.getSubclasses().remove(notification.getNotifier());
169               }
170             }
171             break;
172           }
173           case Notification.REMOVE_MANY:
174           {
175             Object JavaDoc oldValue = notification.getOldValue();
176             if (oldValue != null)
177             {
178               for (Iterator JavaDoc i = ((Collection JavaDoc)oldValue).iterator(); i.hasNext(); )
179               {
180                 Holder holder = (Holder)i.next();
181                 if (!holder.isFrozen())
182                 {
183                   ESuperAdapter eSuperAdapter = holder.getESuperAdapter();
184                   eSuperAdapter.getSubclasses().remove(notification.getNotifier());
185                 }
186               }
187             }
188             break;
189           }
190         }
191       }
192
193       setFlags(featureID);
194     }
195   }
196
197   public boolean isAllAttributesCollectionModified()
198   {
199     return (modifiedState & ATTRIBUTES_MODIFIED) != 0;
200   }
201
202   public void setAllAttributesCollectionModified(boolean set)
203   {
204     if (set)
205     {
206       modifiedState |= ATTRIBUTES_MODIFIED;
207     }
208     else
209     {
210       modifiedState &= ~ATTRIBUTES_MODIFIED;
211     }
212   }
213
214   public boolean isAllContainmentsCollectionModified()
215   {
216     return (modifiedState & CONTAINMENTS_MODIFIED) != 0;
217   }
218
219   public void setAllContainmentsCollectionModified(boolean set)
220   {
221     if (set)
222     {
223       modifiedState |= CONTAINMENTS_MODIFIED;
224     }
225     else
226     {
227       modifiedState &= ~CONTAINMENTS_MODIFIED;
228     }
229   }
230
231   public boolean isAllReferencesCollectionModified()
232   {
233     return (modifiedState & REFERENCES_MODIFIED) != 0;
234   }
235
236   public void setAllReferencesCollectionModified(boolean set)
237   {
238     if (set)
239     {
240       modifiedState |= REFERENCES_MODIFIED;
241     }
242     else
243     {
244       modifiedState &= ~REFERENCES_MODIFIED;
245     }
246   }
247
248   public boolean isAllOperationsCollectionModified()
249   {
250     return (modifiedState & OPERATIONS_MODIFIED) != 0;
251   }
252
253   public void setAllOperationsCollectionModified(boolean set)
254   {
255     if (set)
256     {
257       modifiedState |= OPERATIONS_MODIFIED;
258     }
259     else
260     {
261       modifiedState &= ~OPERATIONS_MODIFIED;
262     }
263   }
264
265   public boolean isAllStructuralFeaturesCollectionModified()
266   {
267     return (modifiedState & STRUCTURAL_FEATURES_MODIFIED) != 0;
268   }
269
270   public void setAllStructuralFeaturesCollectionModified(boolean set)
271   {
272     if (set)
273     {
274       modifiedState |= STRUCTURAL_FEATURES_MODIFIED;
275     }
276     else
277     {
278       modifiedState &= ~STRUCTURAL_FEATURES_MODIFIED;
279     }
280   }
281
282   public boolean isAllSuperCollectionModified()
283   {
284     return (modifiedState & SUPERS_MODIFIED) != 0;
285   }
286
287   public void setAllSuperCollectionModified(boolean set)
288   {
289     if (set)
290     {
291       modifiedState |= SUPERS_MODIFIED;
292     }
293     else
294     {
295       modifiedState &= ~SUPERS_MODIFIED;
296     }
297   }
298
299   public EList getSubclasses()
300   {
301     if (subclasses == null)
302     {
303       subclasses =
304         new UniqueEList()
305         {
306           protected Object JavaDoc [] newData(int capacity)
307           {
308             return new EClass [capacity];
309           }
310
311           protected boolean useEquals()
312           {
313             return false;
314           }
315         };
316     }
317     return subclasses;
318   }
319
320   void setFlags(int featureId)
321   {
322     switch (featureId)
323     {
324       case ATTRIBUTES:
325       {
326         setAllAttributesCollectionModified(true);
327         setAllStructuralFeaturesCollectionModified(true);
328         setAllContainmentsCollectionModified(true);
329         break;
330       }
331       case REFERENCES:
332       {
333         setAllReferencesCollectionModified(true);
334         setAllStructuralFeaturesCollectionModified(true);
335         setAllContainmentsCollectionModified(true);
336         break;
337       }
338       case STRUCTURAL_FEATURES:
339       {
340         setAllAttributesCollectionModified(true);
341         setAllReferencesCollectionModified(true);
342         setAllStructuralFeaturesCollectionModified(true);
343         setAllContainmentsCollectionModified(true);
344         break;
345       }
346       case OPERATIONS:
347       {
348         setAllOperationsCollectionModified(true);
349         setAllContainmentsCollectionModified(true);
350         break;
351       }
352       case SUPERS:
353       {
354         setAllSuperCollectionModified(true);
355         setAllOperationsCollectionModified(true);
356         setAllContainmentsCollectionModified(true);
357         setAllAttributesCollectionModified(true);
358         setAllReferencesCollectionModified(true);
359         setAllStructuralFeaturesCollectionModified(true);
360         break;
361       }
362     }
363
364     if (subclasses != null)
365     {
366       for (Iterator JavaDoc i = subclasses.iterator(); i.hasNext(); )
367       {
368         Holder subclass = (Holder)i.next();
369         ESuperAdapter eSuperAdapter = subclass.getESuperAdapter();
370         eSuperAdapter.setFlags(featureId);
371       }
372     }
373   }
374 }
375
Popular Tags