Coverage Report - org.apache.commons.io.filefilter.DelegateFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
DelegateFileFilter
95%
19/20
90%
9/10
2.8
 
 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 1.0
 28  
  * @version $Id: DelegateFileFilter.java 1471767 2013-04-24 23:24:19Z sebb $
 29  
  *
 30  
  * @see FileFilterUtils#asFileFilter(FileFilter)
 31  
  * @see FileFilterUtils#asFileFilter(FilenameFilter)
 32  
  */
 33  
 public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
 34  
 
 35  
     /** The Filename filter */
 36  
     private final FilenameFilter filenameFilter;
 37  
     /** The File filter */
 38  
     private final FileFilter fileFilter;
 39  
 
 40  
     /**
 41  
      * Constructs a delegate file filter around an existing FilenameFilter.
 42  
      *
 43  
      * @param filter  the filter to decorate
 44  
      */
 45  6
     public DelegateFileFilter(final FilenameFilter filter) {
 46  6
         if (filter == null) {
 47  2
             throw new IllegalArgumentException("The FilenameFilter must not be null");
 48  
         }
 49  4
         this.filenameFilter = filter;
 50  4
         this.fileFilter = null;
 51  4
     }
 52  
 
 53  
     /**
 54  
      * Constructs a delegate file filter around an existing FileFilter.
 55  
      *
 56  
      * @param filter  the filter to decorate
 57  
      */
 58  6
     public DelegateFileFilter(final FileFilter filter) {
 59  6
         if (filter == null) {
 60  2
             throw new IllegalArgumentException("The FileFilter must not be null");
 61  
         }
 62  4
         this.fileFilter = filter;
 63  4
         this.filenameFilter = null;
 64  4
     }
 65  
 
 66  
     /**
 67  
      * Checks the filter.
 68  
      *
 69  
      * @param file  the file to check
 70  
      * @return true if the filter matches
 71  
      */
 72  
     @Override
 73  
     public boolean accept(final File file) {
 74  4
         if (fileFilter != null) {
 75  2
             return fileFilter.accept(file);
 76  
         } else {
 77  2
             return super.accept(file);
 78  
         }
 79  
     }
 80  
 
 81  
     /**
 82  
      * Checks the filter.
 83  
      *
 84  
      * @param dir  the directory
 85  
      * @param name  the filename in the directory
 86  
      * @return true if the filter matches
 87  
      */
 88  
     @Override
 89  
     public boolean accept(final File dir, final String name) {
 90  2
         if (filenameFilter != null) {
 91  2
             return filenameFilter.accept(dir, name);
 92  
         } else {
 93  0
             return super.accept(dir, name);
 94  
         }
 95  
     }
 96  
 
 97  
     /**
 98  
      * Provide a String representaion of this file filter.
 99  
      *
 100  
      * @return a String representaion
 101  
      */
 102  
     @Override
 103  
     public String toString() {
 104  10
         final String delegate = fileFilter != null ? fileFilter.toString() : filenameFilter.toString();
 105  10
         return super.toString() + "(" + delegate + ")";
 106  
     }
 107  
 
 108  
 }