Coverage Report - org.apache.commons.io.filefilter.WildcardFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
WildcardFileFilter
100%
44/44
92%
24/26
3
 
 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  
 import org.apache.commons.io.IOCase;
 25  
 
 26  
 /**
 27  
  * Filters files using the supplied wildcards.
 28  
  * <p>
 29  
  * This filter selects files and directories based on one or more wildcards.
 30  
  * Testing is case-sensitive by default, but this can be configured.
 31  
  * <p>
 32  
  * The wildcard matcher uses the characters '?' and '*' to represent a
 33  
  * single or multiple wildcard characters.
 34  
  * This is the same as often found on Dos/Unix command lines.
 35  
  * The check is case-sensitive by default.
 36  
  * See {@link FilenameUtils#wildcardMatchOnSystem} for more information.
 37  
  * <p>
 38  
  * For example:
 39  
  * <pre>
 40  
  * File dir = new File(".");
 41  
  * FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~");
 42  
  * File[] files = dir.listFiles(fileFilter);
 43  
  * for (int i = 0; i &lt; files.length; i++) {
 44  
  *   System.out.println(files[i]);
 45  
  * }
 46  
  * </pre>
 47  
  *
 48  
  * @version $Id: WildcardFileFilter.java 1563227 2014-01-31 19:45:30Z ggregory $
 49  
  * @since 1.3
 50  
  */
 51  
 public class WildcardFileFilter extends AbstractFileFilter implements Serializable {
 52  
 
 53  
     /** The wildcards that will be used to match filenames. */
 54  
     private final String[] wildcards;
 55  
     /** Whether the comparison is case sensitive. */
 56  
     private final IOCase caseSensitivity;
 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  
     public WildcardFileFilter(final String wildcard) {
 65  20
         this(wildcard, IOCase.SENSITIVE);
 66  18
     }
 67  
 
 68  
     /**
 69  
      * Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
 70  
      *
 71  
      * @param wildcard  the wildcard to match, not null
 72  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 73  
      * @throws IllegalArgumentException if the pattern is null
 74  
      */
 75  28
     public WildcardFileFilter(final String wildcard, final IOCase caseSensitivity) {
 76  28
         if (wildcard == null) {
 77  2
             throw new IllegalArgumentException("The wildcard must not be null");
 78  
         }
 79  26
         this.wildcards = new String[] { wildcard };
 80  26
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 81  26
     }
 82  
 
 83  
     /**
 84  
      * Construct a new case-sensitive wildcard filter for an array of wildcards.
 85  
      * <p>
 86  
      *
 87  
      * @param wildcards  the array of wildcards to match
 88  
      * @throws IllegalArgumentException if the pattern array is null
 89  
      */
 90  
     public WildcardFileFilter(final String[] wildcards) {
 91  4
         this(wildcards, IOCase.SENSITIVE);
 92  2
     }
 93  
 
 94  
     /**
 95  
      * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
 96  
      * <p>
 97  
      *
 98  
      * @param wildcards  the array of wildcards to match, not null
 99  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 100  
      * @throws IllegalArgumentException if the pattern array is null
 101  
      */
 102  12
     public WildcardFileFilter(final String[] wildcards, final IOCase caseSensitivity) {
 103  12
         if (wildcards == null) {
 104  2
             throw new IllegalArgumentException("The wildcard array must not be null");
 105  
         }
 106  10
         this.wildcards = new String[wildcards.length];
 107  10
         System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
 108  10
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 109  10
     }
 110  
 
 111  
     /**
 112  
      * Construct a new case-sensitive wildcard filter for a list of wildcards.
 113  
      *
 114  
      * @param wildcards  the list of wildcards to match, not null
 115  
      * @throws IllegalArgumentException if the pattern list is null
 116  
      * @throws ClassCastException if the list does not contain Strings
 117  
      */
 118  
     public WildcardFileFilter(final List<String> wildcards) {
 119  4
         this(wildcards, IOCase.SENSITIVE);
 120  2
     }
 121  
 
 122  
     /**
 123  
      * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
 124  
      *
 125  
      * @param wildcards  the list of wildcards to match, not null
 126  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 127  
      * @throws IllegalArgumentException if the pattern list is null
 128  
      * @throws ClassCastException if the list does not contain Strings
 129  
      */
 130  4
     public WildcardFileFilter(final List<String> wildcards, final IOCase caseSensitivity) {
 131  4
         if (wildcards == null) {
 132  2
             throw new IllegalArgumentException("The wildcard list must not be null");
 133  
         }
 134  2
         this.wildcards = wildcards.toArray(new String[wildcards.size()]);
 135  2
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 136  2
     }
 137  
 
 138  
     //-----------------------------------------------------------------------
 139  
     /**
 140  
      * Checks to see if the filename matches one of the wildcards.
 141  
      *
 142  
      * @param dir  the file directory (ignored)
 143  
      * @param name  the filename
 144  
      * @return true if the filename matches one of the wildcards
 145  
      */
 146  
     @Override
 147  
     public boolean accept(final File dir, final String name) {
 148  18
         for (final String wildcard : wildcards) {
 149  14
             if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) {
 150  2
                 return true;
 151  
             }
 152  
         }
 153  4
         return false;
 154  
     }
 155  
 
 156  
     /**
 157  
      * Checks to see if the filename matches one of the wildcards.
 158  
      *
 159  
      * @param file  the file to check
 160  
      * @return true if the filename matches one of the wildcards
 161  
      */
 162  
     @Override
 163  
     public boolean accept(final File file) {
 164  104
         final String name = file.getName();
 165  158
         for (final String wildcard : wildcards) {
 166  130
             if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) {
 167  76
                 return true;
 168  
             }
 169  
         }
 170  28
         return false;
 171  
     }
 172  
 
 173  
     /**
 174  
      * Provide a String representation of this file filter.
 175  
      *
 176  
      * @return a String representation
 177  
      */
 178  
     @Override
 179  
     public String toString() {
 180  50
         final StringBuilder buffer = new StringBuilder();
 181  50
         buffer.append(super.toString());
 182  50
         buffer.append("(");
 183  50
         if (wildcards != null) {
 184  138
             for (int i = 0; i < wildcards.length; i++) {
 185  88
                 if (i > 0) {
 186  38
                     buffer.append(",");
 187  
                 }
 188  88
                 buffer.append(wildcards[i]);
 189  
             }
 190  
         }
 191  50
         buffer.append(")");
 192  50
         return buffer.toString();
 193  
     }
 194  
 
 195  
 }