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 1642757 2014-12-01 21:09:30Z sebb $
 29  
  *
 30  
  * @see FileFilterUtils#asFileFilter(FileFilter)
 31  
  * @see FileFilterUtils#asFileFilter(FilenameFilter)
 32  
  */
 33  
 public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
 34  
 
 35  
     private static final long serialVersionUID = -8723373124984771318L;
 36  
     /** The Filename filter */
 37  
     private final FilenameFilter filenameFilter;
 38  
     /** The File filter */
 39  
     private final FileFilter fileFilter;
 40  
 
 41  
     /**
 42  
      * Constructs a delegate file filter around an existing FilenameFilter.
 43  
      *
 44  
      * @param filter  the filter to decorate
 45  
      */
 46  3
     public DelegateFileFilter(final FilenameFilter filter) {
 47  3
         if (filter == null) {
 48  1
             throw new IllegalArgumentException("The FilenameFilter must not be null");
 49  
         }
 50  2
         this.filenameFilter = filter;
 51  2
         this.fileFilter = null;
 52  2
     }
 53  
 
 54  
     /**
 55  
      * Constructs a delegate file filter around an existing FileFilter.
 56  
      *
 57  
      * @param filter  the filter to decorate
 58  
      */
 59  3
     public DelegateFileFilter(final FileFilter filter) {
 60  3
         if (filter == null) {
 61  1
             throw new IllegalArgumentException("The FileFilter must not be null");
 62  
         }
 63  2
         this.fileFilter = filter;
 64  2
         this.filenameFilter = null;
 65  2
     }
 66  
 
 67  
     /**
 68  
      * Checks the filter.
 69  
      *
 70  
      * @param file  the file to check
 71  
      * @return true if the filter matches
 72  
      */
 73  
     @Override
 74  
     public boolean accept(final File file) {
 75  2
         if (fileFilter != null) {
 76  1
             return fileFilter.accept(file);
 77  
         } else {
 78  1
             return super.accept(file);
 79  
         }
 80  
     }
 81  
 
 82  
     /**
 83  
      * Checks the filter.
 84  
      *
 85  
      * @param dir  the directory
 86  
      * @param name  the filename in the directory
 87  
      * @return true if the filter matches
 88  
      */
 89  
     @Override
 90  
     public boolean accept(final File dir, final String name) {
 91  1
         if (filenameFilter != null) {
 92  1
             return filenameFilter.accept(dir, name);
 93  
         } else {
 94  0
             return super.accept(dir, name);
 95  
         }
 96  
     }
 97  
 
 98  
     /**
 99  
      * Provide a String representaion of this file filter.
 100  
      *
 101  
      * @return a String representaion
 102  
      */
 103  
     @Override
 104  
     public String toString() {
 105  5
         final String delegate = fileFilter != null ? fileFilter.toString() : filenameFilter.toString();
 106  5
         return super.toString() + "(" + delegate + ")";
 107  
     }
 108  
 
 109  
 }