Coverage Report - org.apache.commons.io.filefilter.RegexFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexFileFilter
100%
24/24
83%
10/12
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.regex.Pattern;
 22  
 
 23  
 import org.apache.commons.io.IOCase;
 24  
 
 25  
 /**
 26  
  * Filters files using supplied regular expression(s).
 27  
  * <p>
 28  
  * See java.util.regex.Pattern for regex matching rules
 29  
  * </p>
 30  
  *
 31  
  * <p>
 32  
  * e.g.
 33  
  * <pre>
 34  
  * File dir = new File(".");
 35  
  * FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
 36  
  * File[] files = dir.listFiles(fileFilter);
 37  
  * for (int i = 0; i &lt; files.length; i++) {
 38  
  *   System.out.println(files[i]);
 39  
  * }
 40  
  * </pre>
 41  
  *
 42  
  * @version $Id: RegexFileFilter.java 1563227 2014-01-31 19:45:30Z ggregory $
 43  
  * @since 1.4
 44  
  */
 45  
 public class RegexFileFilter extends AbstractFileFilter implements Serializable {
 46  
 
 47  
     /** The regular expression pattern that will be used to match filenames */
 48  
     private final Pattern pattern;
 49  
 
 50  
     /**
 51  
      * Construct a new regular expression filter.
 52  
      *
 53  
      * @param pattern regular string expression to match
 54  
      * @throws IllegalArgumentException if the pattern is null
 55  
      */
 56  6
     public RegexFileFilter(final String pattern) {
 57  6
         if (pattern == null) {
 58  2
             throw new IllegalArgumentException("Pattern is missing");
 59  
         }
 60  
 
 61  4
         this.pattern = Pattern.compile(pattern);
 62  4
     }
 63  
 
 64  
     /**
 65  
      * Construct a new regular expression filter with the specified flags case sensitivity.
 66  
      *
 67  
      * @param pattern regular string expression to match
 68  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 69  
      * @throws IllegalArgumentException if the pattern is null
 70  
      */
 71  4
     public RegexFileFilter(final String pattern, final IOCase caseSensitivity) {
 72  4
         if (pattern == null) {
 73  2
             throw new IllegalArgumentException("Pattern is missing");
 74  
         }
 75  2
         int flags = 0;
 76  2
         if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
 77  2
             flags = Pattern.CASE_INSENSITIVE;
 78  
         }
 79  2
         this.pattern = Pattern.compile(pattern, flags);
 80  2
     }
 81  
 
 82  
     /**
 83  
      * Construct a new regular expression filter with the specified flags.
 84  
      *
 85  
      * @param pattern regular string expression to match
 86  
      * @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
 87  
      * @throws IllegalArgumentException if the pattern is null
 88  
      */
 89  4
     public RegexFileFilter(final String pattern, final int flags) {
 90  4
         if (pattern == null) {
 91  2
             throw new IllegalArgumentException("Pattern is missing");
 92  
         }
 93  2
         this.pattern = Pattern.compile(pattern, flags);
 94  2
     }
 95  
 
 96  
     /**
 97  
      * Construct a new regular expression filter for a compiled regular expression
 98  
      *
 99  
      * @param pattern regular expression to match
 100  
      * @throws IllegalArgumentException if the pattern is null
 101  
      */
 102  4
     public RegexFileFilter(final Pattern pattern) {
 103  4
         if (pattern == null) {
 104  2
             throw new IllegalArgumentException("Pattern is missing");
 105  
         }
 106  
 
 107  2
         this.pattern = pattern;
 108  2
     }
 109  
 
 110  
     /**
 111  
      * Checks to see if the filename matches one of the regular expressions.
 112  
      *
 113  
      * @param dir   the file directory (ignored)
 114  
      * @param name  the filename
 115  
      * @return true if the filename matches one of the regular expressions
 116  
      */
 117  
     @Override
 118  
     public boolean accept(final File dir, final String name) {
 119  30
         return pattern.matcher(name).matches();
 120  
     }
 121  
 
 122  
 }