001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.io.filefilter;
018    
019    import java.io.File;
020    import java.io.FileFilter;
021    import java.io.FilenameFilter;
022    import java.io.Serializable;
023    
024    /**
025     * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
026     * 
027     * @since Commons IO 1.0
028     * @version $Revision: 591058 $ $Date: 2007-11-01 15:47:05 +0000 (Thu, 01 Nov 2007) $
029     * 
030     * @author Stephen Colebourne
031     */
032    public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
033    
034        /** The Filename filter */
035        private final FilenameFilter filenameFilter;
036        /** The File filter */
037        private final FileFilter fileFilter;
038    
039        /**
040         * Constructs a delegate file filter around an existing FilenameFilter.
041         * 
042         * @param filter  the filter to decorate
043         */
044        public DelegateFileFilter(FilenameFilter filter) {
045            if (filter == null) {
046                throw new IllegalArgumentException("The FilenameFilter must not be null");
047            }
048            this.filenameFilter = filter;
049            this.fileFilter = null;
050        }
051    
052        /**
053         * Constructs a delegate file filter around an existing FileFilter.
054         * 
055         * @param filter  the filter to decorate
056         */
057        public DelegateFileFilter(FileFilter filter) {
058            if (filter == null) {
059                throw new IllegalArgumentException("The FileFilter must not be null");
060            }
061            this.fileFilter = filter;
062            this.filenameFilter = null;
063        }
064    
065        /**
066         * Checks the filter.
067         * 
068         * @param file  the file to check
069         * @return true if the filter matches
070         */
071        public boolean accept(File file) {
072            if (fileFilter != null) {
073                return fileFilter.accept(file);
074            } else {
075                return super.accept(file);
076            }
077        }
078    
079        /**
080         * Checks the filter.
081         * 
082         * @param dir  the directory
083         * @param name  the filename in the directory
084         * @return true if the filter matches
085         */
086        public boolean accept(File dir, String name) {
087            if (filenameFilter != null) {
088                return filenameFilter.accept(dir, name);
089            } else {
090                return super.accept(dir, name);
091            }
092        }
093    
094        /**
095         * Provide a String representaion of this file filter.
096         *
097         * @return a String representaion
098         */
099        public String toString() {
100            String delegate = (fileFilter != null ? fileFilter.toString() : filenameFilter.toString()); 
101            return super.toString() + "(" + delegate + ")";
102        }
103        
104    }