KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22
23 import javax.jmi.reflect.*;
24 import org.netbeans.jmi.javamodel.Element;
25 import org.netbeans.mdr.handlers.BaseObjectHandler;
26 import org.netbeans.mdr.persistence.MOFID;
27 import org.netbeans.mdr.persistence.StorageException;
28 import org.netbeans.mdr.storagemodel.*;
29 import org.netbeans.mdr.util.DebugException;
30 import org.openide.ErrorManager;
31
32 public class DeferredAttrList implements List {
33
34     private List innerList;
35     //private final Set innerSet; in new JavMetamodel we don't check for duplications
36

37     private final StorableFeatured mdrObject;
38 // private final StorableClass.AttributeDescriptor attrDesc;
39
private final MOFID metaMofId;
40     private final boolean isRefObject;
41     
42     public DeferredAttrList(StorableFeatured mdrObject, StorableClass.AttributeDescriptor attrDesc, List innerList) {
43         this.mdrObject = mdrObject;
44 // this.attrDesc = attrDesc;
45
this.innerList = innerList;
46         metaMofId = attrDesc.getMofId();
47         isRefObject = RefObject.class.isAssignableFrom(attrDesc.getType());
48         
49         //innerSet = new HashSet();
50
setInnerList(innerList);
51     }
52     
53     public void setInnerList(List innerList) {
54         this.innerList = innerList;
55 // //innerSet.clear();
56
// Iterator iter = innerList.iterator();
57
// while(iter.hasNext()) {
58
// Object obj = iter.next();
59
// if (!innerSet.add(obj)) {
60
//// // throw new DuplicateException(obj, getMetaElement());
61
//// //}
62
//// } // while
63
}
64
65     // ..........................................................................
66

67     private void setAttribComposite(RefObject object) {
68         try {
69             StorableObject storable = (StorableObject) ((BaseObjectHandler) object)._getDelegate();
70             storable.setComposite(mdrObject, storable.getMofId(), metaMofId);
71         } catch (StorageException e) {
72             ErrorManager.getDefault().notify(e);
73         }
74     }
75     
76     private void clearAttribComposite(RefObject object) {
77         try {
78             StorableObject storable = (StorableObject) ((BaseObjectHandler) object)._getDelegate();
79             storable.clearComposite();
80         } catch (StorageException e) {
81             ErrorManager.getDefault().notify(e);
82         }
83     }
84     
85 // private RefObject getMetaElement() {
86
// try {
87
// return (RefObject) mdrObject.getMdrStorage().getRepository().getHandler(mdrObject.getMdrStorage().getObject(metaMofId));
88
// } catch (Exception e) {
89
// return null;
90
// }
91
// }
92

93     // ..........................................................................
94

95     public boolean remove(Object JavaDoc obj) {
96         boolean res = innerList.remove(obj);
97         if (res) {
98             //innerSet.remove(obj);
99
if (isRefObject) {
100                 clearAttribComposite((RefObject) obj);
101             }
102         }
103         return res;
104     }
105     
106     public Object JavaDoc set(int param, Object JavaDoc obj) {
107         Object JavaDoc old = innerList.set(param, obj);
108         if (isRefObject) {
109             clearAttribComposite((RefObject) old);
110             setAttribComposite((RefObject) obj);
111         }
112         //innerSet.remove(old);
113
//if (!innerSet.add(obj)) {
114
// throw new DuplicateException(obj, getMetaElement());
115
//}
116
return old;
117     }
118     
119     public Object JavaDoc remove(int param) {
120         Object JavaDoc old = innerList.remove(param);
121         if (isRefObject) {
122             clearAttribComposite ((RefObject) old);
123         }
124         //innerSet.remove(old);
125
return old;
126     }
127     
128     public void add(int param, Object JavaDoc obj) {
129         innerList.add(param, obj);
130         if (isRefObject) {
131             setAttribComposite((RefObject) obj);
132         }
133         //if (!innerSet.add(obj)) {
134
// throw new DuplicateException(obj, getMetaElement());
135
//}
136
}
137     
138     public boolean add(Object JavaDoc obj) {
139         boolean res = innerList.add(obj);
140         if (isRefObject && res) {
141             setAttribComposite((RefObject) obj);
142         }
143         //if (!innerSet.add(obj)) {
144
// throw new DuplicateException(obj, getMetaElement());
145
//}
146
return res;
147     }
148     
149     public ListIterator listIterator(int param) {
150         return new DeferredAttrListIterator(innerList.listIterator(param));
151     }
152     
153     public Iterator iterator() {
154         return new DeferredAttrListIterator(innerList.listIterator());
155     }
156     
157     public ListIterator listIterator() {
158         return new DeferredAttrListIterator(innerList.listIterator());
159     }
160     
161     public List subList(int param, int param1) {
162         return innerList.subList(param, param1);
163     }
164     
165     public boolean contains(Object JavaDoc obj) {
166         return innerList.contains(obj);
167     }
168     
169     public boolean containsAll(Collection collection) {
170         return innerList.containsAll(collection);
171     }
172     
173     public boolean addAll(Collection c) {
174         // should not be called
175
throw new DebugException();
176     }
177     
178     public void clear() {
179         // should not be called
180
throw new DebugException();
181     }
182     
183     public boolean isEmpty() {
184         return innerList.isEmpty();
185     }
186     
187     public boolean removeAll(Collection c) {
188         // should not be called
189
throw new DebugException();
190     }
191     
192     public boolean retainAll(Collection c) {
193         // should not be called
194
throw new DebugException();
195     }
196     
197     public int size() {
198         return innerList.size();
199     }
200     
201     public Object JavaDoc[] toArray() {
202         return innerList.toArray();
203     }
204     
205     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
206         return innerList.toArray(a);
207     }
208     
209     public boolean addAll(int index, Collection c) {
210         // should not be called
211
throw new DebugException();
212     }
213     
214     public Object JavaDoc get(int index) {
215         return innerList.get(index);
216     }
217     
218     public int indexOf(Object JavaDoc o) {
219         return innerList.indexOf(o);
220     }
221     
222     public int lastIndexOf(Object JavaDoc o) {
223         return innerList.lastIndexOf(o);
224     }
225
226     // DeferredAttrListIterator .................................................
227
class DeferredAttrListIterator implements ListIterator {
228         
229         private Object JavaDoc lastRead;
230         private ListIterator innerIterator;
231         
232         DeferredAttrListIterator(ListIterator iterator) {
233             this.innerIterator = iterator;
234         }
235         
236         public void remove() {
237             innerIterator.remove();
238             //innerSet.remove(lastRead);
239
if (isRefObject) {
240                 clearAttribComposite((RefObject) lastRead);
241             }
242         }
243         
244         public void add(Object JavaDoc obj) {
245             innerIterator.add(obj);
246             //if (!innerSet.add(obj)) {
247
// throw new DuplicateException(obj, getMetaElement());
248
//}
249
if (isRefObject) {
250                 setAttribComposite((RefObject) obj);
251             }
252         }
253         
254         public void set(Object JavaDoc obj) {
255             innerIterator.set(obj);
256             //innerSet.remove(lastRead);
257
//if (!innerSet.add(obj)) {
258
// throw new DuplicateException(obj, getMetaElement());
259
//}
260
if (isRefObject) {
261                 if (((Element) lastRead).isValid()) {
262                     clearAttribComposite((RefObject) lastRead);
263                 }
264                 setAttribComposite((RefObject) obj);
265             }
266         }
267         
268         public boolean hasNext() {
269             return innerIterator.hasNext();
270         }
271         
272         public boolean hasPrevious() {
273             return innerIterator.hasPrevious();
274         }
275         
276         public Object JavaDoc next() {
277             return lastRead = innerIterator.next();
278         }
279
280         public int nextIndex() {
281             return innerIterator.nextIndex();
282         }
283         
284         public Object JavaDoc previous() {
285             return lastRead = innerIterator.previous();
286         }
287         
288         public int previousIndex() {
289             return innerIterator.previousIndex();
290         }
291         
292     } // DeferredAttrListIterator
293
}
294
Popular Tags