KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.eclipse.emf.ecore.impl;
18
19
20 import org.eclipse.emf.common.util.EList;
21 import org.eclipse.emf.ecore.EClass;
22 import org.eclipse.emf.ecore.EObject;
23 import org.eclipse.emf.ecore.EReference;
24 import org.eclipse.emf.ecore.EStructuralFeature;
25 import org.eclipse.emf.ecore.InternalEObject;
26 import org.eclipse.emf.ecore.util.FeatureMap;
27 import org.eclipse.emf.ecore.util.FeatureMapUtil;
28
29
30 /**
31  * An implementation of '<em><b>EObject</b></em>' that delegates to a {@link org.eclipse.emf.ecore.InternalEObject.EStore store}.
32  */

33 public class DynamicEStoreEObjectImpl extends DynamicEObjectImpl
34 {
35   protected static final InternalEObject EUNINITIALIZED_CONTAINER = new EObjectImpl();
36
37   protected InternalEObject.EStore eStore;
38
39   /**
40    * Creates a store-based EObject.
41    */

42   public DynamicEStoreEObjectImpl()
43   {
44     super();
45     eContainer = EUNINITIALIZED_CONTAINER;
46   }
47
48   /**
49    * Creates a store-based EObject.
50    */

51   public DynamicEStoreEObjectImpl(InternalEObject.EStore eStore)
52   {
53     super();
54     eSetStore(eStore);
55     eContainer = EUNINITIALIZED_CONTAINER;
56   }
57
58   /**
59    * Creates a store-based EObject.
60    */

61   public DynamicEStoreEObjectImpl(EClass eClass)
62   {
63     super(eClass);
64     eContainer = EUNINITIALIZED_CONTAINER;
65   }
66
67   /**
68    * Creates a store-based EObject.
69    */

70   public DynamicEStoreEObjectImpl(EClass eClass, InternalEObject.EStore eStore)
71   {
72     super(eClass);
73     eSetStore(eStore);
74     eContainer = EUNINITIALIZED_CONTAINER;
75   }
76
77   protected boolean eIsCaching()
78   {
79     return true;
80   }
81
82   public Object JavaDoc dynamicGet(int dynamicFeatureID)
83   {
84     Object JavaDoc result = eSettings[dynamicFeatureID];
85     if (result == null)
86     {
87       EStructuralFeature eStructuralFeature = eDynamicFeature(dynamicFeatureID);
88       if (!eStructuralFeature.isTransient())
89       {
90         if (FeatureMapUtil.isFeatureMap(eStructuralFeature))
91         {
92           eSettings[dynamicFeatureID] = result = createFeatureMap(eStructuralFeature);
93         }
94         else if (eStructuralFeature.isMany())
95         {
96           eSettings[dynamicFeatureID] = result = createList(eStructuralFeature);
97         }
98         else
99         {
100           result = eStore().get(this, eStructuralFeature, InternalEObject.EStore.NO_INDEX);
101           if (eIsCaching())
102           {
103             eSettings[dynamicFeatureID] = result;
104           }
105         }
106       }
107     }
108     return result;
109   }
110
111   public void dynamicSet(int dynamicFeatureID, Object JavaDoc value)
112   {
113     EStructuralFeature eStructuralFeature = eDynamicFeature(dynamicFeatureID);
114     if (eStructuralFeature.isTransient())
115     {
116       eSettings[dynamicFeatureID] = value;
117     }
118     else
119     {
120       eStore().set(this, eStructuralFeature, InternalEObject.EStore.NO_INDEX, value == NIL ? null : value);
121       if (eIsCaching())
122       {
123         eSettings[dynamicFeatureID] = value;
124       }
125     }
126   }
127
128   public void dynamicUnset(int dynamicFeatureID)
129   {
130     eStore().unset(this, eDynamicFeature(dynamicFeatureID));
131     eSettings[dynamicFeatureID] = null;
132   }
133
134   public boolean eDynamicIsSet(EStructuralFeature eStructuralFeature)
135   {
136     return
137       eStructuralFeature.isTransient() ?
138         super.eDynamicIsSet(eStructuralFeature) :
139         eStore().isSet(this, eStructuralFeature);
140   }
141
142   protected EList createList(EStructuralFeature eStructuralFeature)
143   {
144     return new EStoreEObjectImpl.EStoreEList(this, eStructuralFeature, eStore());
145   }
146
147   protected FeatureMap createFeatureMap(EStructuralFeature eStructuralFeature)
148   {
149     return new EStoreEObjectImpl.EStoreFeatureMap(this, eStructuralFeature, eStore());
150   }
151
152   public EObject eContainer()
153   {
154     if (eContainer == EUNINITIALIZED_CONTAINER)
155     {
156       eInitializeContainer();
157     }
158
159     return eContainer;
160   }
161
162   public int eContainerFeatureID()
163   {
164     if (eContainer == EUNINITIALIZED_CONTAINER)
165     {
166       eInitializeContainer();
167     }
168
169     return eContainerFeatureID;
170   }
171
172   protected void eInitializeContainer()
173   {
174     eContainer = eStore().getContainer(this);
175     if (eContainer != null)
176     {
177       EStructuralFeature eContainingFeature = eStore().getContainingFeature(this);
178       if (eContainingFeature instanceof EReference)
179       {
180         EReference eContainingReference = (EReference)eContainingFeature;
181         EReference eOpposite = eContainingReference.getEOpposite();
182         if (eOpposite != null)
183         {
184           eContainerFeatureID = eClass().getFeatureID(eOpposite);
185           return;
186         }
187       }
188
189       eContainerFeatureID = EOPPOSITE_FEATURE_BASE - eContainer.eClass().getFeatureID(eContainingFeature);
190     }
191   }
192
193   public InternalEObject.EStore eStore()
194   {
195     return eStore;
196   }
197
198   public void eSetStore(InternalEObject.EStore store)
199   {
200     this.eStore = store;
201   }
202
203 /*
204   public String toString()
205   {
206     String result = super.toString();
207     int index = result.indexOf("DynamicEStoreEObjectImpl");
208     return index == -1 ? result : result.substring(0, index) + result.substring(index + 13);
209   }
210 */

211 }
212
Popular Tags