alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.jmiimpl.javamodel;

import java.lang.reflect.Modifier;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.ClassDefinition;
import org.netbeans.jmi.javamodel.Resource;
import org.netbeans.modules.javacore.ClassIndex;
import javax.jmi.reflect.TypeMismatchException;
import java.util.Collection;
import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.ArrayList;
import org.netbeans.modules.javacore.internalapi.ProgressSupport;
import org.openide.util.Utilities;

/**
 *
 * @author Martin Matula
 */
class SubClassesCollection extends AbstractCollection implements TypeVerifier {
    protected final JavaClass superClass;
    protected boolean deterministicProgress;
    

    protected SubClassesCollection(JavaClass superClass, boolean deterministicProgress) {
        this.superClass = (JavaClass) ClassDefinitionImpl.getRealClassDefinition(superClass);
        this.deterministicProgress = deterministicProgress;
    }

    private Resource[] getResources() {
        // extract simple name of the superClass
        int mods=superClass.getModifiers();
        
        if (Modifier.isFinal(mods))
            return new Resource[0];
        return ((FeatureImpl)superClass).findReferencedResources(true); 
    }

    protected java.util.Iterator iterator(Resource[] resources) {
        return new It(resources);
    }

    public Iterator iterator() {
        return iterator(getResources());
    }

    public int size() {
        Resource[] resources = getResources();
        if (resources.length == 0) {
            return 0;
        }
        int size = 0;
        for (Iterator it = iterator(resources); it.hasNext(); size++, it.next());
        return size;
    }

    public boolean isEmpty() {
        Resource[] resources = getResources();
        if (resources.length == 0) {
            return true;
        } else {
            Iterator it = iterator(resources);
            return !it.hasNext();
        }
    }

    public boolean add(Object obj) {
        throw new UnsupportedOperationException();
    }

    public void checkType(Object obj) throws TypeMismatchException {
        throw new UnsupportedOperationException();
    }

    protected boolean acceptClass(ClassDefinition cls) {
        return Utilities.compareObjects(superClass.getName(), getName(ClassDefinitionImpl.getRealClassDefinition(cls.getSuperClass())));
    }
    
    protected static String getName(ClassDefinition cd) {
        return cd == null ? null : cd.getName();
    }

    protected class It implements Iterator {
        private Resource[] resources;
        private int resourceIndex = 0;
        private Iterator classes = null;
        private ClassDefinition next = null;
        private float step;
        private int last;
        private static final int MAX_COUNT=30;
        private ProgressSupport progressSupport;

        protected It(Resource[] resources) {
            this.resources = resources;
            
            step = 1;
            if (resources.length > MAX_COUNT) {
                step = (float) MAX_COUNT / resources.length;
            }
            last = 0;
        
            progressSupport = org.netbeans.modules.javacore.internalapi.JavaMetamodel.getManager().getProgressSupport();
            
        }

        protected void addInnerClasses(Collection col, JavaClass cls) {
            for (Iterator it = cls.getContents().iterator(); it.hasNext();) {
                Object tmp = it.next();
                if (tmp instanceof JavaClass) {
                    col.add(tmp);
                    addInnerClasses(col, (JavaClass) tmp);
                }
            }
        }

        public boolean hasNext() {
            while (next == null) {
                while (classes == null || !classes.hasNext()) {
                    if (resourceIndex == 0) {
                        progressSupport.fireProgressListenerStart(0, deterministicProgress?Math.min(resources.length,MAX_COUNT):-1);
                    }
                    if (resourceIndex < resources.length) {
                        ArrayList al = new ArrayList();
                        for (Iterator it = resources[resourceIndex].getClassifiers().iterator(); it.hasNext();) {
                            Object tmp = it.next();
                            if (tmp instanceof JavaClass) {
                                al.add(tmp);
                                addInnerClasses(al, (JavaClass) tmp);
                            }
                        }
                        classes = al.iterator();
                        resourceIndex++;
                        
                        if (resourceIndex*step >= last) {
                            progressSupport.fireProgressListenerStep();
                            last++;
                        }

                    } else {
                        progressSupport.fireProgressListenerStop();
                        return false;
                    }
                }
                while (classes.hasNext()) {
                    Object subClass = classes.next();
                    if (subClass instanceof ClassDefinition) {
                        if (acceptClass((ClassDefinition) subClass)) {
                            next = (ClassDefinition) subClass;
                            break;
                        }
                    }
                }
            }
            return true;
        }

        public Object next() {
            if (!hasNext()) {
                throw new java.util.NoSuchElementException();
            }
            ClassDefinition last = next;
            next = null;
            return last;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.