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 1642757 2014-12-01 21:09:30Z sebb $
 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  
     private static final long serialVersionUID = -5037645902506953517L;
 56  
     /** The wildcards that will be used to match filenames. */
 57  
     private final String[] wildcards;
 58  
 
 59  
     /**
 60  
      * Construct a new case-sensitive wildcard filter for a single wildcard.
 61  
      *
 62  
      * @param wildcard  the wildcard to match
 63  
      * @throws IllegalArgumentException if the pattern is null
 64  
      */
 65  4
     public WildcardFilter(final String wildcard) {
 66  4
         if (wildcard == null) {
 67  1
             throw new IllegalArgumentException("The wildcard must not be null");
 68  
         }
 69  3
         this.wildcards = new String[] { wildcard };
 70  3
     }
 71  
 
 72  
     /**
 73  
      * Construct a new case-sensitive wildcard filter for an array of wildcards.
 74  
      *
 75  
      * @param wildcards  the array of wildcards to match
 76  
      * @throws IllegalArgumentException if the pattern array is null
 77  
      */
 78  2
     public WildcardFilter(final String[] wildcards) {
 79  2
         if (wildcards == null) {
 80  1
             throw new IllegalArgumentException("The wildcard array must not be null");
 81  
         }
 82  1
         this.wildcards = new String[wildcards.length];
 83  1
         System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
 84  1
     }
 85  
 
 86  
     /**
 87  
      * Construct a new case-sensitive wildcard filter for a list of wildcards.
 88  
      *
 89  
      * @param wildcards  the list of wildcards to match
 90  
      * @throws IllegalArgumentException if the pattern list is null
 91  
      * @throws ClassCastException if the list does not contain Strings
 92  
      */
 93  2
     public WildcardFilter(final List<String> wildcards) {
 94  2
         if (wildcards == null) {
 95  1
             throw new IllegalArgumentException("The wildcard list must not be null");
 96  
         }
 97  1
         this.wildcards = wildcards.toArray(new String[wildcards.size()]);
 98  1
     }
 99  
 
 100  
     //-----------------------------------------------------------------------
 101  
     /**
 102  
      * Checks to see if the filename matches one of the wildcards.
 103  
      *
 104  
      * @param dir  the file directory
 105  
      * @param name  the filename
 106  
      * @return true if the filename matches one of the wildcards
 107  
      */
 108  
     @Override
 109  
     public boolean accept(final File dir, final String name) {
 110  3
         if (dir != null && new File(dir, name).isDirectory()) {
 111  0
             return false;
 112  
         }
 113  
 
 114  9
         for (final String wildcard : wildcards) {
 115  7
             if (FilenameUtils.wildcardMatch(name, wildcard)) {
 116  1
                 return true;
 117  
             }
 118  
         }
 119  
 
 120  2
         return false;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Checks to see if the filename matches one of the wildcards.
 125  
      *
 126  
      * @param file the file to check
 127  
      * @return true if the filename matches one of the wildcards
 128  
      */
 129  
     @Override
 130  
     public boolean accept(final File file) {
 131  16
         if (file.isDirectory()) {
 132  0
             return false;
 133  
         }
 134  
 
 135  34
         for (final String wildcard : wildcards) {
 136  27
             if (FilenameUtils.wildcardMatch(file.getName(), wildcard)) {
 137  9
                 return true;
 138  
             }
 139  
         }
 140  
 
 141  7
         return false;
 142  
     }
 143  
 
 144  
 }