Home | History | Annotate | Download | only in filefilter
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 package org.apache.commons.io.filefilter;
     18 
     19 import java.io.File;
     20 import java.io.FileFilter;
     21 import java.io.FilenameFilter;
     22 import java.io.Serializable;
     23 
     24 /**
     25  * This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
     26  *
     27  * @since Commons IO 1.0
     28  * @version $Revision: 591058 $ $Date: 2007-11-01 15:47:05 +0000 (Thu, 01 Nov 2007) $
     29  *
     30  * @author Stephen Colebourne
     31  */
     32 public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
     33 
     34     /** The Filename filter */
     35     private final FilenameFilter filenameFilter;
     36     /** The File filter */
     37     private final FileFilter fileFilter;
     38 
     39     /**
     40      * Constructs a delegate file filter around an existing FilenameFilter.
     41      *
     42      * @param filter  the filter to decorate
     43      */
     44     public DelegateFileFilter(FilenameFilter filter) {
     45         if (filter == null) {
     46             throw new IllegalArgumentException("The FilenameFilter must not be null");
     47         }
     48         this.filenameFilter = filter;
     49         this.fileFilter = null;
     50     }
     51 
     52     /**
     53      * Constructs a delegate file filter around an existing FileFilter.
     54      *
     55      * @param filter  the filter to decorate
     56      */
     57     public DelegateFileFilter(FileFilter filter) {
     58         if (filter == null) {
     59             throw new IllegalArgumentException("The FileFilter must not be null");
     60         }
     61         this.fileFilter = filter;
     62         this.filenameFilter = null;
     63     }
     64 
     65     /**
     66      * Checks the filter.
     67      *
     68      * @param file  the file to check
     69      * @return true if the filter matches
     70      */
     71     public boolean accept(File file) {
     72         if (fileFilter != null) {
     73             return fileFilter.accept(file);
     74         } else {
     75             return super.accept(file);
     76         }
     77     }
     78 
     79     /**
     80      * Checks the filter.
     81      *
     82      * @param dir  the directory
     83      * @param name  the filename in the directory
     84      * @return true if the filter matches
     85      */
     86     public boolean accept(File dir, String name) {
     87         if (filenameFilter != null) {
     88             return filenameFilter.accept(dir, name);
     89         } else {
     90             return super.accept(dir, name);
     91         }
     92     }
     93 
     94     /**
     95      * Provide a String representaion of this file filter.
     96      *
     97      * @return a String representaion
     98      */
     99     public String toString() {
    100         String delegate = (fileFilter != null ? fileFilter.toString() : filenameFilter.toString());
    101         return super.toString() + "(" + delegate + ")";
    102     }
    103 
    104 }
    105