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

📄 directorypoller.java

📁 一个简易的轮训小程序 哈哈哈哈哈哈后 嘿嘿嘿嘿黑诶
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        public String toString() { return "null filter"; }
    }

    
        
    /**
     * This comparator can be used to list files based on modification time.
     * 
     * @author Cristiano Sadun
     */
    public static class ModificationTimeComparator extends BidirectionalComparator {

        /**
         * Create a comparator which will impose an ascending or descending order on
         * modification times depending on the value of the parameter
         * 
         * @param ascending if <b>true</b>, older files will be ordered before newer files.
         */
        public ModificationTimeComparator(boolean ascending) {
            super(ascending);
        }
        
        protected final  long getComparisonValue(File f1,  File f2) {
            return f1.lastModified()-f2.lastModified();
        }
        
    }
    
    /**
     * This comparator can be used to list files based on size.
     * 
     * @author Cristiano Sadun
     */
    public static class FileSizeComparator extends BidirectionalComparator {
        
        /**
         * Create a comparator which will impose an ascending or descending order on
         * modification times depending on the value of the parameter
         * 
         * @param ascending if <b>true</b>, older files will be ordered before newer files.
         */
        public FileSizeComparator(boolean ascending) {
            super(ascending);
        }
        
        
        protected final long getComparisonValue(File f1, File f2) {
            return f1.length()-f2.length();
        }

    }
    

    /**
     * Create a poller over the given directories, using the given filter.
     * <p>
     * SubDirectories are automatically filtered.
     * @param dirs an array of directories
     * @param filter a filter for files to look up
     */
    public DirectoryPoller(File []dirs, FilenameFilter filter) {
        this(dirs, filter, false);
    }

    /**
     * Create a poller over the given directories, which will match any file.
     * <p>
     * SubDirectories are automatically filtered.
     * @param dirs an array of directories
     */
    public DirectoryPoller(File []dirs) {
        this(dirs, new NullFilenameFilter());
    }


    /**
     * Create a poller over the given directory, using the given filter.
     * <p>
     * SubDirectories are automatically filtered.
     * @param dir a directory
     * @param filter a filter for files to look up
     */
    public DirectoryPoller(File directory, FilenameFilter filter) {
        this(new File[] { directory }, filter );
    }

    /**
     * Create a poller over the given directory, which will match any file.
     * <p>
     * SubDirectories are automatically filtered.
     * @param dir a directory
     */
    public DirectoryPoller(File directory) {
        this(new File[] { directory });
    }

    /**
     * Create a poller initially not bound to any directory, which uses the given filter.
     * <p>
     * SubDirectories are automatically filtered.
     * <p>
     * Before starting the poller, a single call to {@link DirectoryPoller#setDirectories(java.io.File[])}
     * must be done to bind the poller to a specific directory.
     *
     * @param filter a filter for files to look up
     */
    public DirectoryPoller(FilenameFilter filter) {
        this(filter, false);
    }


    /**
     * Create a poller initially not bound to any directory, which will match any file.
     * <p>
     * SubDirectories are automatically filtered.
     * <p>
     * Before starting the poller, a single call to {@link DirectoryPoller#setDirectories(java.io.File[])}
     * must be done to bind the poller to a specific directory.
     *
     * @param filter a filter for files to look up
     */
    public DirectoryPoller() {
        this(new NullFilenameFilter());
    }


    /**
     * Create a poller over the given directories, using the given filter
     * and time-based filtering as well (see class comment).
     * <p>
     * SubDirectories are automatically filtered.
     *
     * @param dirs an array of directories
     * @param filter a filter for files to look up
     * @param timeBased if <b>true</b>, the poller uses time-based lookup
     */
    public DirectoryPoller(File []dirs, FilenameFilter filter, boolean timeBased) {
        setName("directory-poller-"+(counter++));
        setDirectories(dirs);
        this.originalFilter=new DirectoryFilter(filter);
        setTimeBased(timeBased);
        this.baseTime=new long[dirs.length];
    }

    /**
     * Create a poller over the given directory, using the given filter
     * and time-based filtering as well (see class comment).
     * <p>
     * SubDirectories are automatically filtered.
     * @param dir a directory
     * @param filter a filter for files to look up
     */
    public DirectoryPoller(File directory, FilenameFilter filter, boolean timeBased) {

        this(new File[] { directory }, filter, timeBased);
    }

    /**
     * Create a poller initially not bound to any directory, which uses the given filter
     * and time-based filtering as well (see class comment).
     * <p>
     * SubDirectories are automatically filtered.
     * <p>
     * Before starting the poller, a single call to {@link DirectoryPoller#setDirectories(java.io.File[])}
     * must be done to bind the poller to a specific directory.
     *
     * @param filter a filter for files to look up
     */
    public DirectoryPoller(FilenameFilter filter, boolean timeBased) {
        this(new File[0], filter, timeBased);
    }

	/**
	 * Add one directory to the controlled set. It can be called
     * only if the poller thread hasn't started yet.
	 * @param dir the directory to add
	 * @exception IllegalStateException if the poller has already started.
     * @exception IllegalArgumentException if String does not contain a directory path
	 */
    public void addDirectory(File dir) {
    	File [] originalDirs=getDirectories();
    	
    	// Ignore duplicated directorues
    	for(int i=0;i<originalDirs.length;i++)
    		if (originalDirs[i].getAbsoluteFile().equals(dir.getAbsoluteFile())) return;
    	
    	File [] dirs = new File[getDirectories().length+1];
    	System.arraycopy(originalDirs,0,dirs,0,originalDirs.length);
    	dirs[originalDirs.length]=dir;
    	setDirectories(dirs);
    }

    /**
	 * Remove one directory from the controlled set. It can be called
     * only if the poller thread hasn't started yet.
	 * @param dir the directory to remove
	 * @exception IllegalStateException if the poller has already started.
     * @exception IllegalArgumentException if the directory is not among the controlled ones
	 */
     public void removeDirectory(File dir) {
     	File [] originalDirs=getDirectories();
    	File [] dirs = new File[originalDirs.length-1];
    	boolean removed=false;
    	int c=0;
    	for(int i=0;i<originalDirs.length;i++) {
    		if (originalDirs[i].equals(dir)) {
    			removed=true;
    		} else { dirs[c++]=originalDirs[i]; }
    	}
    	if (!removed)
    		throw new IllegalArgumentException(dir+" is not a controlled directory");
    	setDirectories(dirs);
     }


    /**
     * Set the directories controlled by the poller. It can be called
     * only if the poller thread hasn't started yet.
     *
     * @param dirs the directories to be controlled by the poller
     * @exception IllegalStateException if the poller has already started.
     * @exception IllegalArgumentException if any of the File objects is not a directory
     */
    public void setDirectories(File [] dirs) {
        if (isAlive())
            if (!isSleeping())
                throw new IllegalStateException("Can't call setDirectories when the poller is running and not sleeping");
        if (dirs!=null) {
            for(int i=0;i<dirs.length;i++) {
                if (! dirs[i].isDirectory())
                    throw new IllegalArgumentException(dirs[i]+" is not a directory");
            }
        }
        this.dirs=dirs;
        baseTime=new long[dirs.length];
    }

    /**
     * Return the directories controlled by the poller.
     * @return the directories controlled by the poller
     */
    public File [] getDirectories() { return dirs; }

    /**
     * Instruct the poller to automatically move the file to the
     * directory associated to each directory under control, which
     * can be set/retrieved by {@link DirectoryPoller#setAutoMoveDirectory(java.io.File,
     * java.io.File) setAutoMoveDirectory()}/{@link
     * DirectoryPoller#getAutoMoveDirectory(java.io.File) getAutoMoveDirectory()}
     * (see also class description).
     *
     * @param v if <b>true</b>, the poller will automatically move
     *          selected files in the "received" directory associated
     *          to each directory under control
     */
    public void setAutoMove(boolean v) {
        autoMove=v;
    }

    /**
     * Verify the autoMove mode (see {@link DirectoryPoller#setAutoMove(boolean)
     * setAutoMove()} and class description).
     *
     * @return <b>true</b> if autoMove mode is active
     */
    public boolean getAutoMove() {
        return autoMove;
    }

    /**
     * Returns the directory associated to the given controlled directory,
     * where files polled are automatically moved if the
     * autoMove mode is active (see {@link DirectoryPoller#setAutoMove(boolean)
     * setAutoMove()} and class description).
     * <p>
     * If no directory is associated by {@link DirectoryPoller#setAutoMoveDirectory(java.io.File,
     * java.io.File) setAutoMoveDirectory()}, the subdirectory {@link
     * DirectoryPoller#DEFAULT_AUTOMOVE_DIRECTORY DEFAULT_AUTOMOVE_DIRECTORY} is associated
     * automatically.
     *
     * @param directory the directory for which the associated "automove" directory
     *        is requested
     * @exception IllegalArgumentException if <tt>directory</tt> is not under control of
     *            the poller
     */
    public File getAutoMoveDirectory(File directory) throws IllegalArgumentException {
        directory=PathNormalizer.normalize(directory);
        File f = (File)autoMoveDirs.get(directory);
        if (f==null) {

⌨️ 快捷键说明

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