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 1642757 2014-12-01 21:09:30Z sebb $
 43  
  * @since 1.4
 44  
  */
 45  
 public class RegexFileFilter extends AbstractFileFilter implements Serializable {
 46  
 
 47  
     private static final long serialVersionUID = 4269646126155225062L;
 48  
     /** The regular expression pattern that will be used to match filenames */
 49  
     private final Pattern pattern;
 50  
 
 51  
     /**
 52  
      * Construct a new regular expression filter.
 53  
      *
 54  
      * @param pattern regular string expression to match
 55  
      * @throws IllegalArgumentException if the pattern is null
 56  
      */
 57  3
     public RegexFileFilter(final String pattern) {
 58  3
         if (pattern == null) {
 59  1
             throw new IllegalArgumentException("Pattern is missing");
 60  
         }
 61  
 
 62  2
         this.pattern = Pattern.compile(pattern);
 63  2
     }
 64  
 
 65  
     /**
 66  
      * Construct a new regular expression filter with the specified flags case sensitivity.
 67  
      *
 68  
      * @param pattern regular string expression to match
 69  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 70  
      * @throws IllegalArgumentException if the pattern is null
 71  
      */
 72  2
     public RegexFileFilter(final String pattern, final IOCase caseSensitivity) {
 73  2
         if (pattern == null) {
 74  1
             throw new IllegalArgumentException("Pattern is missing");
 75  
         }
 76  1
         int flags = 0;
 77  1
         if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
 78  1
             flags = Pattern.CASE_INSENSITIVE;
 79  
         }
 80  1
         this.pattern = Pattern.compile(pattern, flags);
 81  1
     }
 82  
 
 83  
     /**
 84  
      * Construct a new regular expression filter with the specified flags.
 85  
      *
 86  
      * @param pattern regular string expression to match
 87  
      * @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
 88  
      * @throws IllegalArgumentException if the pattern is null
 89  
      */
 90  2
     public RegexFileFilter(final String pattern, final int flags) {
 91  2
         if (pattern == null) {
 92  1
             throw new IllegalArgumentException("Pattern is missing");
 93  
         }
 94  1
         this.pattern = Pattern.compile(pattern, flags);
 95  1
     }
 96  
 
 97  
     /**
 98  
      * Construct a new regular expression filter for a compiled regular expression
 99  
      *
 100  
      * @param pattern regular expression to match
 101  
      * @throws IllegalArgumentException if the pattern is null
 102  
      */
 103  2
     public RegexFileFilter(final Pattern pattern) {
 104  2
         if (pattern == null) {
 105  1
             throw new IllegalArgumentException("Pattern is missing");
 106  
         }
 107  
 
 108  1
         this.pattern = pattern;
 109  1
     }
 110  
 
 111  
     /**
 112  
      * Checks to see if the filename matches one of the regular expressions.
 113  
      *
 114  
      * @param dir   the file directory (ignored)
 115  
      * @param name  the filename
 116  
      * @return true if the filename matches one of the regular expressions
 117  
      */
 118  
     @Override
 119  
     public boolean accept(final File dir, final String name) {
 120  15
         return pattern.matcher(name).matches();
 121  
     }
 122  
 
 123  
 }