Coverage Report - org.apache.commons.io.filefilter.WildcardFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
WildcardFilter
92%
26/28
90%
18/20
4.4
 
 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.Serializable;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.commons.io.FilenameUtils;
 24  
 
 25  
 /**
 26  
  * Filters files using the supplied wildcards.
 27  
  * <p>
 28  
  * This filter selects files, but not directories, based on one or more wildcards
 29  
  * and using case-sensitive comparison.
 30  
  * <p>
 31  
  * The wildcard matcher uses the characters '?' and '*' to represent a
 32  
  * single or multiple wildcard characters.
 33  
  * This is the same as often found on Dos/Unix command lines.
 34  
  * The extension check is case-sensitive.
 35  
  * See {@link FilenameUtils#wildcardMatch(String, String)} for more information.
 36  
  * <p>
 37  
  * For example:
 38  
  * <pre>
 39  
  * File dir = new File(".");
 40  
  * FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
 41  
  * File[] files = dir.listFiles(fileFilter);
 42  
  * for (int i = 0; i &lt; files.length; i++) {
 43  
  *   System.out.println(files[i]);
 44  
  * }
 45  
  * </pre>
 46  
  *
 47  
  * @version $Id: WildcardFilter.java 1563227 2014-01-31 19:45:30Z ggregory $
 48  
  * @since 1.1
 49  
  * @deprecated Use WilcardFileFilter. Deprecated as this class performs directory
 50  
  * filtering which it shouldn't do, but that can't be removed due to compatability.
 51  
  */
 52  
 @Deprecated
 53  
 public class WildcardFilter extends AbstractFileFilter implements Serializable {
 54  
 
 55  
     /** The wildcards that will be used to match filenames. */
 56  
     private final String[] wildcards;
 57  
 
 58  
     /**
 59  
      * Construct a new case-sensitive wildcard filter for a single wildcard.
 60  
      *
 61  
      * @param wildcard  the wildcard to match
 62  
      * @throws IllegalArgumentException if the pattern is null
 63  
      */
 64  8
     public WildcardFilter(final String wildcard) {
 65  8
         if (wildcard == null) {
 66  2
             throw new IllegalArgumentException("The wildcard must not be null");
 67  
         }
 68  6
         this.wildcards = new String[] { wildcard };
 69  6
     }
 70  
 
 71  
     /**
 72  
      * Construct a new case-sensitive wildcard filter for an array of wildcards.
 73  
      *
 74  
      * @param wildcards  the array of wildcards to match
 75  
      * @throws IllegalArgumentException if the pattern array is null
 76  
      */
 77  4
     public WildcardFilter(final String[] wildcards) {
 78  4
         if (wildcards == null) {
 79  2
             throw new IllegalArgumentException("The wildcard array must not be null");
 80  
         }
 81  2
         this.wildcards = new String[wildcards.length];
 82  2
         System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
 83  2
     }
 84  
 
 85  
     /**
 86  
      * Construct a new case-sensitive wildcard filter for a list of wildcards.
 87  
      *
 88  
      * @param wildcards  the list of wildcards to match
 89  
      * @throws IllegalArgumentException if the pattern list is null
 90  
      * @throws ClassCastException if the list does not contain Strings
 91  
      */
 92  4
     public WildcardFilter(final List<String> wildcards) {
 93  4
         if (wildcards == null) {
 94  2
             throw new IllegalArgumentException("The wildcard list must not be null");
 95  
         }
 96  2
         this.wildcards = wildcards.toArray(new String[wildcards.size()]);
 97  2
     }
 98  
 
 99  
     //-----------------------------------------------------------------------
 100  
     /**
 101  
      * Checks to see if the filename matches one of the wildcards.
 102  
      *
 103  
      * @param dir  the file directory
 104  
      * @param name  the filename
 105  
      * @return true if the filename matches one of the wildcards
 106  
      */
 107  
     @Override
 108  
     public boolean accept(final File dir, final String name) {
 109  6
         if (dir != null && new File(dir, name).isDirectory()) {
 110  0
             return false;
 111  
         }
 112  
 
 113  18
         for (final String wildcard : wildcards) {
 114  14
             if (FilenameUtils.wildcardMatch(name, wildcard)) {
 115  2
                 return true;
 116  
             }
 117  
         }
 118  
 
 119  4
         return false;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Checks to see if the filename matches one of the wildcards.
 124  
      *
 125  
      * @param file the file to check
 126  
      * @return true if the filename matches one of the wildcards
 127  
      */
 128  
     @Override
 129  
     public boolean accept(final File file) {
 130  32
         if (file.isDirectory()) {
 131  0
             return false;
 132  
         }
 133  
 
 134  68
         for (final String wildcard : wildcards) {
 135  54
             if (FilenameUtils.wildcardMatch(file.getName(), wildcard)) {
 136  18
                 return true;
 137  
             }
 138  
         }
 139  
 
 140  14
         return false;
 141  
     }
 142  
 
 143  
 }