KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.api.mdr.MDRObject;
22 import org.netbeans.jmi.javamodel.Feature;
23 import org.netbeans.jmi.javamodel.JavaModelPackage;
24 import org.netbeans.mdr.handlers.ClassProxyHandler;
25 import org.netbeans.mdr.storagemodel.StorableClass;
26 import javax.jmi.model.MofClass;
27 import javax.jmi.reflect.RefObject;
28 import java.lang.ref.WeakReference JavaDoc;
29 import java.util.*;
30
31
32 /** Root class for class proxies of classes that have transient or partially persistent
33  * instances. This class universally implements refAllOfType and refAllOfClass operations.
34  *
35  * @author Martin Matula
36  */

37 public abstract class MetadataElementClass extends ClassProxyHandler {
38     private WeakReference JavaDoc allOfClass = null;
39     private WeakReference JavaDoc allOfType = null;
40     
41     /** Creates a new instance of SemiPersistentClass */
42     public MetadataElementClass(StorableClass s) {
43         super(s);
44     }
45     
46     protected boolean _pushIterators(Collection iterators, Object JavaDoc obj, boolean includeSupertypes) {
47         boolean pushed = false;
48         if (obj == null)
49             return pushed;
50         
51 // if (obj instanceof Resource) {
52
// iterators.add(((Resource) obj).getClassifiers().iterator());
53
// iterators.add(((Resource) obj).getImports().iterator());
54
// } else if (obj instanceof ClassDefinition) {
55
// iterators.add(((ClassDefinition) obj).getFeatures().iterator());
56
// } else if (obj instanceof CallableFeature) {
57
// iterators.add(((CallableFeature) obj).getParameters().iterator());
58
// } else {
59
// pushed = false;
60
// }
61
//
62
// if (obj instanceof Feature) {
63
// JavaDoc javaDoc = ((Feature)obj).getJavadoc();
64
// List tags = (javaDoc != null) ? javaDoc.getTags() : null;
65
// if (tags != null && !tags.isEmpty()) {
66
// iterators.add(tags.iterator());
67
// pushed = true;
68
// }
69
// }
70
if (obj instanceof MetadataElement) {
71             Collection children = ((MetadataElement) obj).getChildren();
72             if (children != null && !children.isEmpty()) {
73                 iterators.add(children.iterator());
74                 pushed = true;
75             }
76         }
77         else if (obj instanceof List) {
78             iterators.add(((List) obj).iterator());
79             pushed = true;
80         }
81         
82         return pushed;
83     }
84
85     protected boolean _accept(Object JavaDoc obj, boolean includeSupertypes) {
86         if (obj == null || !(obj instanceof RefObject))
87             return false;
88         return ((RefObject) obj).refIsInstanceOf(refMetaObject(), includeSupertypes);
89     }
90
91     protected boolean _isAbstract() {
92         return ((MofClass) refMetaObject()).isAbstract();
93     }
94     
95     protected Collection _allOfClass(boolean includeSubtypes) {
96         // [TODO] include also new elements that have not been added to any resource yet
97
// they can be found in the registry of new elements
98
Collection result;
99         if (includeSubtypes) {
100             if (allOfType == null || (result = (Collection) allOfType.get()) == null) {
101                 result = new AllOfClassCollection(true, (JavaModelPackage) refImmediatePackage());
102                 allOfType = new WeakReference JavaDoc(result);
103             }
104         } else {
105             if (_isAbstract()) {
106                 result = Collections.EMPTY_LIST;
107             } else {
108                 if (allOfClass == null || (result = (Collection) allOfClass.get()) == null) {
109                     result = new AllOfClassCollection(false, (JavaModelPackage) refImmediatePackage());
110                     allOfClass = new WeakReference JavaDoc(result);
111                 }
112             }
113         }
114         return result;
115     }
116     
117     private class AllOfClassCollection extends AbstractCollection {
118         private final boolean includeSubtypes;
119         private final JavaModelPackage extent;
120         
121         public AllOfClassCollection(boolean includeSubtypes, JavaModelPackage extent) {
122             this.includeSubtypes = includeSubtypes;
123             this.extent = extent;
124         }
125         
126         public Iterator iterator() {
127             return new It();
128         }
129         
130         public int size() {
131             lock();
132             try {
133                 int size = 0;
134                 for (Iterator it = iterator(); it.hasNext(); it.next(), size++);
135                 return size;
136             } finally {
137                 unlock();
138             }
139         }
140         
141         private void lock() {
142             ((MDRObject) extent).repository().beginTrans(false);
143         }
144         
145         private void unlock() {
146             ((MDRObject) extent).repository().endTrans();
147         }
148         
149         private class It implements Iterator {
150             private final List iterators = new ArrayList();
151             private Iterator currentIterator;
152             private Object JavaDoc lastItem = null;
153             
154             public It() {
155                 currentIterator = extent.getResource().refAllOfClass().iterator();
156             }
157             
158             public boolean hasNext() {
159                 while (lastItem == null && (!iterators.isEmpty() || currentIterator.hasNext())) {
160                     while (lastItem == null && currentIterator.hasNext()) {
161                         Object JavaDoc temp = currentIterator.next();
162                         if (_pushIterators(iterators, temp, includeSubtypes)) {
163                             iterators.add(currentIterator);
164                             currentIterator = (Iterator) iterators.remove(0);
165                         }
166                         if (_accept(temp, includeSubtypes)) {
167                             lastItem = temp;
168                         }
169                         else if (temp instanceof Feature && _accept(((Feature)temp).getJavadoc(), includeSubtypes)) {
170                             lastItem = ((Feature)temp).getJavadoc();
171                         }
172                     }
173                     if (lastItem == null) {
174                         currentIterator = (Iterator) iterators.remove(0);
175                     }
176                 }
177                 return lastItem != null;
178             }
179             
180             public Object JavaDoc next() {
181                 lock();
182                 try {
183                     if (hasNext()) {
184                         Object JavaDoc result = lastItem;
185                         lastItem = null;
186                         return result;
187                     } else {
188                         throw new NoSuchElementException();
189                     }
190                 } finally {
191                     unlock();
192                 }
193             }
194             
195             public void remove() {
196                 throw new UnsupportedOperationException JavaDoc();
197             }
198         }
199     }
200 }
201
Popular Tags