KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
22 import org.netbeans.jmi.javamodel.JavaClass;
23 import org.netbeans.jmi.javamodel.ClassDefinition;
24 import org.netbeans.jmi.javamodel.Resource;
25 import javax.jmi.reflect.TypeMismatchException;
26 import java.util.Collection JavaDoc;
27 import java.util.AbstractCollection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import org.netbeans.modules.javacore.internalapi.ProgressSupport;
31 import org.openide.util.Utilities;
32
33 /**
34  *
35  * @author Martin Matula
36  */

37 class SubClassesCollection extends AbstractCollection JavaDoc implements TypeVerifier {
38     protected final JavaClass superClass;
39     protected boolean deterministicProgress;
40     
41
42     protected SubClassesCollection(JavaClass superClass, boolean deterministicProgress) {
43         this.superClass = (JavaClass) ClassDefinitionImpl.getRealClassDefinition(superClass);
44         this.deterministicProgress = deterministicProgress;
45     }
46
47     private Resource[] getResources() {
48         // extract simple name of the superClass
49
int mods=superClass.getModifiers();
50         
51         if (Modifier.isFinal(mods))
52             return new Resource[0];
53         return ((FeatureImpl)superClass).findReferencedResources(true);
54     }
55
56     protected java.util.Iterator JavaDoc iterator(Resource[] resources) {
57         return new It(resources);
58     }
59
60     public Iterator JavaDoc iterator() {
61         return iterator(getResources());
62     }
63
64     public int size() {
65         Resource[] resources = getResources();
66         if (resources.length == 0) {
67             return 0;
68         }
69         int size = 0;
70         for (Iterator JavaDoc it = iterator(resources); it.hasNext(); size++, it.next());
71         return size;
72     }
73
74     public boolean isEmpty() {
75         Resource[] resources = getResources();
76         if (resources.length == 0) {
77             return true;
78         } else {
79             Iterator JavaDoc it = iterator(resources);
80             return !it.hasNext();
81         }
82     }
83
84     public boolean add(Object JavaDoc obj) {
85         throw new UnsupportedOperationException JavaDoc();
86     }
87
88     public void checkType(Object JavaDoc obj) throws TypeMismatchException {
89         throw new UnsupportedOperationException JavaDoc();
90     }
91
92     protected boolean acceptClass(ClassDefinition cls) {
93         return Utilities.compareObjects(superClass.getName(), getName(ClassDefinitionImpl.getRealClassDefinition(cls.getSuperClass())));
94     }
95     
96     protected static String JavaDoc getName(ClassDefinition cd) {
97         return cd == null ? null : cd.getName();
98     }
99
100     protected class It implements Iterator JavaDoc {
101         private Resource[] resources;
102         private int resourceIndex = 0;
103         private Iterator JavaDoc classes = null;
104         private ClassDefinition next = null;
105         private float step;
106         private int last;
107         private static final int MAX_COUNT=30;
108         private ProgressSupport progressSupport;
109
110         protected It(Resource[] resources) {
111             this.resources = resources;
112             
113             step = 1;
114             if (resources.length > MAX_COUNT) {
115                 step = (float) MAX_COUNT / resources.length;
116             }
117             last = 0;
118         
119             progressSupport = org.netbeans.modules.javacore.internalapi.JavaMetamodel.getManager().getProgressSupport();
120             
121         }
122
123         protected void addInnerClasses(Collection JavaDoc col, JavaClass cls) {
124             Object JavaDoc[] features=cls.getContents().toArray();
125             for (int i=0;i<features.length;i++) {
126                 Object JavaDoc tmp = features[i];
127                 if (tmp instanceof JavaClass) {
128                     col.add(tmp);
129                     addInnerClasses(col, (JavaClass) tmp);
130                 }
131             }
132         }
133
134         public boolean hasNext() {
135             while (next == null) {
136                 while (classes == null || !classes.hasNext()) {
137                     if (resourceIndex == 0) {
138                         if (deterministicProgress)
139                             progressSupport.fireProgressListenerStart(0, Math.min(resources.length,MAX_COUNT));
140                     }
141                     if (resourceIndex < resources.length) {
142                         ArrayList JavaDoc al = new ArrayList JavaDoc();
143                         for (Iterator JavaDoc it = resources[resourceIndex].getClassifiers().iterator(); it.hasNext();) {
144                             Object JavaDoc tmp = it.next();
145                             if (tmp instanceof JavaClass) {
146                                 al.add(tmp);
147                                 addInnerClasses(al, (JavaClass) tmp);
148                             }
149                         }
150                         classes = al.iterator();
151                         resourceIndex++;
152                         
153                         if (resourceIndex*step >= last + 1 ) {
154                             progressSupport.fireProgressListenerStep();
155                             last++;
156                         }
157
158                     } else {
159                         if (deterministicProgress)
160                             progressSupport.fireProgressListenerStop();
161                         return false;
162                     }
163                 }
164                 while (classes.hasNext()) {
165                     Object JavaDoc subClass = classes.next();
166                     if (subClass instanceof ClassDefinition) {
167                         if (acceptClass((ClassDefinition) subClass)) {
168                             next = (ClassDefinition) subClass;
169                             break;
170                         }
171                     }
172                 }
173             }
174             return true;
175         }
176
177         public Object JavaDoc next() {
178             if (!hasNext()) {
179                 throw new java.util.NoSuchElementException JavaDoc();
180             }
181             ClassDefinition last = next;
182             next = null;
183             return last;
184         }
185
186         public void remove() {
187             throw new UnsupportedOperationException JavaDoc();
188         }
189     }
190 }
191
Popular Tags