KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > SemiPersistentAttrList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.Collection JavaDoc;
22 import java.util.ConcurrentModificationException JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ListIterator JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.jmi.reflect.DuplicateException;
30 import javax.jmi.reflect.RefObject;
31
32 import org.netbeans.mdr.handlers.BaseObjectHandler;
33 import org.netbeans.mdr.persistence.MOFID;
34 import org.netbeans.mdr.persistence.StorageException;
35 import org.netbeans.mdr.storagemodel.StorableClass;
36 import org.netbeans.mdr.storagemodel.StorableFeatured;
37 import org.netbeans.mdr.util.DebugException;
38 import org.openide.ErrorManager;
39
40 public class SemiPersistentAttrList implements List JavaDoc {
41
42     
43     private List JavaDoc innerList;
44     private final Set JavaDoc innerSet = new HashSet JavaDoc();
45     private int modCount = 0;
46     
47     private final StorableFeatured mdrObject;
48     private final StorableClass.AttributeDescriptor attrDesc;
49     private final MOFID metaMofId;
50     private final boolean isRefObject;
51     
52     private final SemiPersistentElement parent;
53     private final int changeMask;
54
55     
56     public SemiPersistentAttrList(StorableFeatured mdrObject, StorableClass.AttributeDescriptor attrDesc, List JavaDoc innerList, SemiPersistentElement parent, int changeMask) {
57         this.mdrObject = mdrObject;
58         this.attrDesc = attrDesc;
59         this.parent = parent;
60         this.changeMask = changeMask;
61         metaMofId = attrDesc.getMofId();
62         isRefObject = RefObject.class.isAssignableFrom(attrDesc.getType());
63         setInnerList(innerList);
64     }
65
66     public void setInnerList(List JavaDoc innerList) {
67         modCount++;
68         this.innerList = innerList;
69         innerSet.clear();
70         Iterator JavaDoc iter = innerList.iterator();
71         while(iter.hasNext()) {
72             Object JavaDoc obj = iter.next();
73             if (innerSet.contains(obj)) {
74                 throw new DuplicateException(obj, getMetaElement());
75             } else {
76                 innerSet.add(obj);
77                 if (isRefObject) {
78                     setAttribComposite((RefObject) obj);
79                 }
80             }
81         }
82     }
83
84     protected void objectChanged() {
85         if (parent != null)
86             parent.objectChanged(changeMask);
87     }
88     
89     // ..........................................................................
90

91     private void setAttribComposite(RefObject object) {
92         try {
93             DeferredObject storable = (DeferredObject) ((BaseObjectHandler) object)._getDelegate();
94             storable.setComposite(mdrObject, storable.getMofId(), metaMofId);
95         } catch (StorageException e) {
96             ErrorManager.getDefault().notify(e);
97         }
98     }
99     
100     private void clearAttribComposite(RefObject object) {
101         try {
102             DeferredObject storable = (DeferredObject) ((BaseObjectHandler) object)._getDelegate();
103             storable.clearComposite();
104         } catch (StorageException e) {
105             ErrorManager.getDefault().notify(e);
106         }
107     }
108     
109     private RefObject getMetaElement() {
110         try {
111             return (RefObject) mdrObject.getMdrStorage().getRepository().getHandler(mdrObject.getMdrStorage().getObject(metaMofId));
112         } catch (Exception JavaDoc e) {
113             return null;
114         }
115     }
116     
117     // ..........................................................................
118

119     public boolean remove(Object JavaDoc obj) {
120         boolean res = innerList.remove(obj);
121         if (res) {
122             innerSet.remove(obj);
123             if (isRefObject) {
124                 clearAttribComposite((RefObject) obj);
125             }
126             objectChanged();
127         }
128         return res;
129     }
130     
131     public Object JavaDoc set(int param, Object JavaDoc obj) {
132         objectChanged();
133         Object JavaDoc old = innerList.set(param, obj);
134         if (isRefObject) {
135             clearAttribComposite((RefObject) old);
136             setAttribComposite((RefObject) obj);
137         }
138         innerSet.remove(old);
139         if (!innerSet.add(obj)) {
140             throw new DuplicateException(obj, getMetaElement());
141         }
142         return old;
143     }
144     
145     public Object JavaDoc remove(int param) {
146         objectChanged();
147         Object JavaDoc old = innerList.remove(param);
148         if (isRefObject) {
149             clearAttribComposite ((RefObject) old);
150         }
151         innerSet.remove(old);
152         return old;
153     }
154     
155     public void add(int param, Object JavaDoc obj) {
156         objectChanged();
157         innerList.add(param, obj);
158         if (isRefObject) {
159             setAttribComposite((RefObject) obj);
160         }
161         if (!innerSet.add(obj)) {
162             throw new DuplicateException(obj, getMetaElement());
163         }
164     }
165     
166     public boolean add(Object JavaDoc obj) {
167         objectChanged();
168         boolean res = innerList.add(obj);
169         if (isRefObject && res) {
170             setAttribComposite((RefObject) obj);
171         }
172         if (!innerSet.add(obj)) {
173             throw new DuplicateException(obj, getMetaElement());
174         }
175         return res;
176     }
177     
178     public ListIterator JavaDoc listIterator(int param) {
179         return new SemiPersistentAttrListIterator(innerList.listIterator(param));
180     }
181     
182     public Iterator JavaDoc iterator() {
183         return new SemiPersistentAttrListIterator(innerList.listIterator());
184     }
185     
186     public ListIterator JavaDoc listIterator() {
187         return new SemiPersistentAttrListIterator(innerList.listIterator());
188     }
189     
190     public List JavaDoc subList(int param, int param1) {
191         return innerList.subList(param, param1);
192     }
193     
194     public boolean contains(Object JavaDoc obj) {
195         return innerSet.contains(obj);
196     }
197     
198     public boolean containsAll(Collection JavaDoc collection) {
199         return innerSet.containsAll(collection);
200     }
201     
202     public boolean addAll(Collection JavaDoc c) {
203         // should not be called
204
throw new DebugException();
205     }
206     
207     public void clear() {
208         // should not be called
209
throw new DebugException();
210     }
211     
212     public boolean isEmpty() {
213         return innerList.isEmpty();
214     }
215     
216     public boolean removeAll(Collection JavaDoc c) {
217         // should not be called
218
throw new DebugException();
219     }
220     
221     public boolean retainAll(Collection JavaDoc c) {
222         // should not be called
223
throw new DebugException();
224     }
225     
226     public int size() {
227         return innerList.size();
228     }
229     
230     public Object JavaDoc[] toArray() {
231         return innerList.toArray();
232     }
233     
234     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
235         return innerList.toArray(a);
236     }
237     
238     public boolean addAll(int index, Collection JavaDoc c) {
239         // should not be called
240
throw new DebugException();
241     }
242     
243     public Object JavaDoc get(int index) {
244         return innerList.get(index);
245     }
246     
247     public int indexOf(Object JavaDoc o) {
248         return innerList.indexOf(o);
249     }
250     
251     public int lastIndexOf(Object JavaDoc o) {
252         return innerList.lastIndexOf(o);
253     }
254
255     // ListIterator .................................................
256
class SemiPersistentAttrListIterator implements ListIterator JavaDoc {
257         
258         private Object JavaDoc lastRead;
259         private ListIterator JavaDoc innerIterator;
260         private final int modCount;
261         
262         SemiPersistentAttrListIterator(ListIterator JavaDoc iterator) {
263             this.innerIterator = iterator;
264             this.modCount = getParentModCount();
265         }
266         
267         private int getParentModCount() {
268             return SemiPersistentAttrList.this.modCount;
269         }
270         
271         private void testModCount() throws ConcurrentModificationException JavaDoc {
272             if (modCount != getParentModCount())
273                 throw new ConcurrentModificationException JavaDoc();
274         }
275         
276         public void remove() {
277             testModCount();
278             objectChanged();
279             innerIterator.remove();
280             innerSet.remove(lastRead);
281             if (isRefObject) {
282                 clearAttribComposite((RefObject) lastRead);
283             }
284         }
285         
286         public void add(Object JavaDoc obj) {
287             testModCount();
288             objectChanged();
289             innerIterator.add(obj);
290             if (!innerSet.add(obj)) {
291                 throw new DuplicateException(obj, getMetaElement());
292             }
293             if (isRefObject) {
294                 setAttribComposite((RefObject) obj);
295             }
296         }
297         
298         public void set(Object JavaDoc obj) {
299             testModCount();
300             objectChanged();
301             innerIterator.set(obj);
302             innerSet.remove(lastRead);
303             if (!innerSet.add(obj)) {
304                 throw new DuplicateException(obj, getMetaElement());
305             }
306             if (isRefObject) {
307                 clearAttribComposite((RefObject) lastRead);
308                 setAttribComposite((RefObject) obj);
309             }
310         }
311         
312         public boolean hasNext() {
313             testModCount();
314             return innerIterator.hasNext();
315         }
316         
317         public boolean hasPrevious() {
318             testModCount();
319             return innerIterator.hasPrevious();
320         }
321         
322         public Object JavaDoc next() {
323             testModCount();
324             return lastRead = innerIterator.next();
325         }
326
327         public int nextIndex() {
328             testModCount();
329             return innerIterator.nextIndex();
330         }
331         
332         public Object JavaDoc previous() {
333             testModCount();
334             return lastRead = innerIterator.previous();
335         }
336         
337         public int previousIndex() {
338             testModCount();
339             return innerIterator.previousIndex();
340         }
341     }
342 }
343
Popular Tags