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-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.editor.ext.java;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import org.netbeans.editor.SettingsUtil;
import org.netbeans.editor.ext.ExtSettingsDefaults;
import org.netbeans.editor.ext.ExtSettingsNames;

/** Compound finder works over more finders and merge the search results.
 *  
 *
 *  @author Martin Roskanin, David Konecny
 *  @since 10/2002
 */
public class CompoundFinder implements JCFinder {
    
    private List finders;
    
    /** Kit class from which for which the settings like case sensitivity etc. will be read */
    private Class kitClass;

    private static JCField artificialField = new JavaCompletion.BaseField(
        JavaCompletion.CLASS_CLASS, "class", JavaCompletion.CLASS_TYPE, Modifier.PUBLIC); // NOI18N

    /** Creates a new instance of CompoundFinder 
     *
     *  @param finders the List of the finders from which search results will be merged
     *  @param kitClass kit class to which this finder is associated and for which
     *     the settings will be read
     */
    public CompoundFinder(List finders, Class kitClass) {
        this.finders = finders;
        this.kitClass = kitClass;
        Iterator it = finders.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            if (o instanceof JCBaseFinder) {
                ((JCBaseFinder)o).setParentFinder(this);
            } else {
                //System.out.println("TODO CompoundFinder   ");//assert false : "This should never happen"; // NOI18N
            }
        }
    }

    /** Find classes by name and possibly in some package.
     *  The search results result will be merged from all finders appended to the compound finder.
     * @param pkg package where the classes should be searched for. It can be null
     * @param begining of the name of the class. The package name must be omitted.
     * @param exactMatch whether the given name is the exact requested name
     *   of the class or not.
     * @return list of the matching classes
     *
     */
    public List findClasses(JCPackage pkg, String name, boolean exactMatch) {
        List ret = new ArrayList();
        for (int i = 0; i0; i--) {
            ret = ((JCFinder)finders.get(i-1)).findFields(c, name, exactMatch, staticOnly, inspectOuterClasses);
            if (ret != null && ret.size() > 0) {
                if (ret.size() == 1 && ret.get(0).equals(artificialField)) {
                    // hmmm, let's try other finders and if no one provide
                    // better results then return at least something
                    something = ret;
                } else {
                    return ret;
                }
            }
        }
        if (something != null) {
            return something;
        }
        return new ArrayList();
    }
    
    /** Find methods by name in a given class.
     * @param c class which is searched for the methods.
     * @param name start of the name of the method
     * @param exactMatch whether the given name of the method is exact
     * @param staticOnly whether search for the static methods only
     * @param inspectOuterClasses if the given class is inner class of some
     *   outer class, whether the methods of the outer class should be possibly
     *   added or not. This should be false when searching for 'this.'
     * @return list of the matching methods
     *
     */
    public List findMethods(JCClass c, String name, boolean exactMatch, boolean staticOnly, boolean inspectOuterClasses) {
        List ret;
        for (int i=finders.size(); i>0; i--) {
            ret = ((JCFinder)finders.get(i-1)).findMethods(c, name, exactMatch, staticOnly, inspectOuterClasses);
            if (ret != null && ret.size() > 0) {
                return ret;
            }
        }
        return new ArrayList();
    }
    
    /** Get the list of packages that start with the given name
     * @param name the start of the requested package(s) name
     * @return list of the matching packages
     *
     */
    public List findPackages(String name, boolean exactMatch, boolean subPackages) {
        TreeSet ret = getNaturalSort() ? new TreeSet(JCBaseFinder.INSENSITIVE_CLASS_NAME_COMPARATOR) : new TreeSet();
        for (int i = 0; i0; i--) {
            cls = ((JCFinder)finders.get(i-1)).getExactClass(classFullName);
            if (cls != null) {
                return cls;
            }
        }
        return null;
   }
    
    /** Get the package from the package name  */
    public JCPackage getExactPackage(String packageName) {
        JCPackage pkg;
        for (int i=finders.size(); i>0; i--) {
            pkg = ((JCFinder)finders.get(i-1)).getExactPackage(packageName);
            if (pkg != null)  {
                return pkg;
            }
        }
        return null;
    }

    private boolean getNaturalSort() {
        return SettingsUtil.getBoolean(kitClass,
            ExtSettingsNames.COMPLETION_NATURAL_SORT,
            ExtSettingsDefaults.defaultCompletionNaturalSort);
    }
    
}
... 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.