⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functionpathbroker.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
字号:
package jmathlib.core.functions;

import java.util.ArrayList;
import java.io.File;

/**
 * Using a base directory, uses a consistent search to find a function by its name
 */
public class FunctionPathBroker 
{
    private File      baseDir = null;
    private ArrayList paths   = new ArrayList();
    private boolean traverseChildren;
    
    public FunctionPathBroker(File _baseDir, boolean _traverseChildren) 
    {
        baseDir = _baseDir;
        traverseChildren = _traverseChildren;
        
        populateSearchPaths();      
    }
    
    private void populateSearchPaths() {
        paths.clear();
        if (traverseChildren) 
        {            
            if (baseDir.exists() && baseDir.isDirectory()) 
            {
              addSearchPath(baseDir);
            }
        }      
    }
    
    public File getBaseDirectory() 
    {
        return baseDir;
    }
    
    public void setBaseDirectory(File dir) {
       this.baseDir = dir;
       
       populateSearchPaths();
    }
    
    public File findFunction(String functionName) {
        File result = findClassOrMFile(baseDir, functionName);
        if(result == null)
        {            
            int size = paths.size();
            for(int index = 0; index < size && (result == null); index++)
            {
                result = findClassOrMFile((File)paths.get(index), functionName);               
            }
        }      
        return result;
    }
    
    /**Searchs a directory for the specified class
        @param path         - the directory to search
        @param functionName - the function to search for
        
        @return a File object representing the full path to the file that matches the fileName
      */
    private File findClassOrMFile(File path, String functionName)
    {
        //System.out.println("file search: "+fileName);
        File[] files = path.listFiles();
        
        File result = null;
        
        // only check non-empty directories
        if (files != null)
        {
            for(int fileNo = 0; fileNo < files.length; fileNo++)
            {
                String fileName = files[fileNo].getName();
               
                int index = fileName.lastIndexOf(".");
                //System.out.println("file: "+temp);
                if(index > -1)
                {                    
                    String tempFunction = fileName.substring(0, index);
                    if(tempFunction.equalsIgnoreCase(functionName) &&
                       (fileName.equalsIgnoreCase(functionName+".m")   ||
                        fileName.equalsIgnoreCase(functionName+".p")   ||
                        fileName.equalsIgnoreCase(functionName+".class") ) )
                    {
                        result = files[fileNo];
                        break;
                    }
                }
            }
        }
        return result;
    }    
    
    /**
     * build up the list of directories to search for functions
     */
    private void addSearchPath(File path)
    {
        String[] files = path.list();

        if(files != null)
        {
            for(int fileNo = 0; fileNo < files.length; fileNo++)
            {               
                String newPath = path + File.separator + files[fileNo];
                File temp = new File(newPath);
                if(temp.isDirectory() && newPath.indexOf("_private") == -1)
                {                    
                    paths.add(temp);
                    addSearchPath(temp);
                }
            }
        }
    }   
    
    /**
     * 
     * @return
     */
    public int getPathCount() 
    {
        return paths.size();
    }
    
    /**
     * 
     * @param index
     * @return
     */
    public File getPath(int index) 
    {
        return (File)paths.get(index);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -