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 1642757 2014-12-01 21:09:30Z sebb $
 49  
  * @since 1.3
 50  
  */
 51  
 public class WildcardFileFilter extends AbstractFileFilter implements Serializable {
 52  
 
 53  
     private static final long serialVersionUID = -7426486598995782105L;
 54  
     /** The wildcards that will be used to match filenames. */
 55  
     private final String[] wildcards;
 56  
     /** Whether the comparison is case sensitive. */
 57  
     private final IOCase caseSensitivity;
 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  
     public WildcardFileFilter(final String wildcard) {
 66  10
         this(wildcard, IOCase.SENSITIVE);
 67  9
     }
 68  
 
 69  
     /**
 70  
      * Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
 71  
      *
 72  
      * @param wildcard  the wildcard to match, not null
 73  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 74  
      * @throws IllegalArgumentException if the pattern is null
 75  
      */
 76  14
     public WildcardFileFilter(final String wildcard, final IOCase caseSensitivity) {
 77  14
         if (wildcard == null) {
 78  1
             throw new IllegalArgumentException("The wildcard must not be null");
 79  
         }
 80  13
         this.wildcards = new String[] { wildcard };
 81  13
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 82  13
     }
 83  
 
 84  
     /**
 85  
      * Construct a new case-sensitive wildcard filter for an array of wildcards.
 86  
      * <p>
 87  
      *
 88  
      * @param wildcards  the array of wildcards to match
 89  
      * @throws IllegalArgumentException if the pattern array is null
 90  
      */
 91  
     public WildcardFileFilter(final String[] wildcards) {
 92  2
         this(wildcards, IOCase.SENSITIVE);
 93  1
     }
 94  
 
 95  
     /**
 96  
      * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
 97  
      * <p>
 98  
      *
 99  
      * @param wildcards  the array of wildcards to match, not null
 100  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 101  
      * @throws IllegalArgumentException if the pattern array is null
 102  
      */
 103  6
     public WildcardFileFilter(final String[] wildcards, final IOCase caseSensitivity) {
 104  6
         if (wildcards == null) {
 105  1
             throw new IllegalArgumentException("The wildcard array must not be null");
 106  
         }
 107  5
         this.wildcards = new String[wildcards.length];
 108  5
         System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
 109  5
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 110  5
     }
 111  
 
 112  
     /**
 113  
      * Construct a new case-sensitive wildcard filter for a list of wildcards.
 114  
      *
 115  
      * @param wildcards  the list of wildcards to match, not null
 116  
      * @throws IllegalArgumentException if the pattern list is null
 117  
      * @throws ClassCastException if the list does not contain Strings
 118  
      */
 119  
     public WildcardFileFilter(final List<String> wildcards) {
 120  2
         this(wildcards, IOCase.SENSITIVE);
 121  1
     }
 122  
 
 123  
     /**
 124  
      * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
 125  
      *
 126  
      * @param wildcards  the list of wildcards to match, not null
 127  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 128  
      * @throws IllegalArgumentException if the pattern list is null
 129  
      * @throws ClassCastException if the list does not contain Strings
 130  
      */
 131  2
     public WildcardFileFilter(final List<String> wildcards, final IOCase caseSensitivity) {
 132  2
         if (wildcards == null) {
 133  1
             throw new IllegalArgumentException("The wildcard list must not be null");
 134  
         }
 135  1
         this.wildcards = wildcards.toArray(new String[wildcards.size()]);
 136  1
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 137  1
     }
 138  
 
 139  
     //-----------------------------------------------------------------------
 140  
     /**
 141  
      * Checks to see if the filename matches one of the wildcards.
 142  
      *
 143  
      * @param dir  the file directory (ignored)
 144  
      * @param name  the filename
 145  
      * @return true if the filename matches one of the wildcards
 146  
      */
 147  
     @Override
 148  
     public boolean accept(final File dir, final String name) {
 149  9
         for (final String wildcard : wildcards) {
 150  7
             if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) {
 151  1
                 return true;
 152  
             }
 153  
         }
 154  2
         return false;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Checks to see if the filename matches one of the wildcards.
 159  
      *
 160  
      * @param file  the file to check
 161  
      * @return true if the filename matches one of the wildcards
 162  
      */
 163  
     @Override
 164  
     public boolean accept(final File file) {
 165  52
         final String name = file.getName();
 166  82
         for (final String wildcard : wildcards) {
 167  66
             if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) {
 168  36
                 return true;
 169  
             }
 170  
         }
 171  16
         return false;
 172  
     }
 173  
 
 174  
     /**
 175  
      * Provide a String representation of this file filter.
 176  
      *
 177  
      * @return a String representation
 178  
      */
 179  
     @Override
 180  
     public String toString() {
 181  25
         final StringBuilder buffer = new StringBuilder();
 182  25
         buffer.append(super.toString());
 183  25
         buffer.append("(");
 184  25
         if (wildcards != null) {
 185  69
             for (int i = 0; i < wildcards.length; i++) {
 186  44
                 if (i > 0) {
 187  19
                     buffer.append(",");
 188  
                 }
 189  44
                 buffer.append(wildcards[i]);
 190  
             }
 191  
         }
 192  25
         buffer.append(")");
 193  25
         return buffer.toString();
 194  
     }
 195  
 
 196  
 }