Coverage Report - org.apache.commons.io.filefilter.NameFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
NameFileFilter
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.IOCase;
 24  
 
 25  
 /**
 26  
  * Filters filenames for a certain name.
 27  
  * <p>
 28  
  * For example, to print all files and directories in the
 29  
  * current directory whose name is <code>Test</code>:
 30  
  *
 31  
  * <pre>
 32  
  * File dir = new File(".");
 33  
  * String[] files = dir.list( new NameFileFilter("Test") );
 34  
  * for ( int i = 0; i &lt; files.length; i++ ) {
 35  
  *     System.out.println(files[i]);
 36  
  * }
 37  
  * </pre>
 38  
  *
 39  
  * @since 1.0
 40  
  * @version $Id: NameFileFilter.java 1642757 2014-12-01 21:09:30Z sebb $
 41  
  * @see FileFilterUtils#nameFileFilter(String)
 42  
  * @see FileFilterUtils#nameFileFilter(String, IOCase)
 43  
  */
 44  
 public class NameFileFilter extends AbstractFileFilter implements Serializable {
 45  
 
 46  
     private static final long serialVersionUID = 176844364689077340L;
 47  
     /** The filenames to search for */
 48  
     private final String[] names;
 49  
     /** Whether the comparison is case sensitive. */
 50  
     private final IOCase caseSensitivity;
 51  
 
 52  
     /**
 53  
      * Constructs a new case-sensitive name file filter for a single name.
 54  
      *
 55  
      * @param name  the name to allow, must not be null
 56  
      * @throws IllegalArgumentException if the name is null
 57  
      */
 58  
     public NameFileFilter(final String name) {
 59  28
         this(name, null);
 60  27
     }
 61  
 
 62  
     /**
 63  
      * Construct a new name file filter specifying case-sensitivity.
 64  
      *
 65  
      * @param name  the name to allow, must not be null
 66  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 67  
      * @throws IllegalArgumentException if the name is null
 68  
      */
 69  30
     public NameFileFilter(final String name, final IOCase caseSensitivity) {
 70  30
         if (name == null) {
 71  2
             throw new IllegalArgumentException("The wildcard must not be null");
 72  
         }
 73  28
         this.names = new String[] {name};
 74  28
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 75  28
     }
 76  
 
 77  
     /**
 78  
      * Constructs a new case-sensitive name file filter for an array of names.
 79  
      * <p>
 80  
      * The array is not cloned, so could be changed after constructing the
 81  
      * instance. This would be inadvisable however.
 82  
      *
 83  
      * @param names  the names to allow, must not be null
 84  
      * @throws IllegalArgumentException if the names array is null
 85  
      */
 86  
     public NameFileFilter(final String[] names) {
 87  9
         this(names, null);
 88  8
     }
 89  
 
 90  
     /**
 91  
      * Constructs a new name file filter for an array of names specifying case-sensitivity.
 92  
      *
 93  
      * @param names  the names to allow, must not be null
 94  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 95  
      * @throws IllegalArgumentException if the names array is null
 96  
      */
 97  13
     public NameFileFilter(final String[] names, final IOCase caseSensitivity) {
 98  13
         if (names == null) {
 99  1
             throw new IllegalArgumentException("The array of names must not be null");
 100  
         }
 101  12
         this.names = new String[names.length];
 102  12
         System.arraycopy(names, 0, this.names, 0, names.length);
 103  12
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 104  12
     }
 105  
 
 106  
     /**
 107  
      * Constructs a new case-sensitive name file filter for a list of names.
 108  
      *
 109  
      * @param names  the names to allow, must not be null
 110  
      * @throws IllegalArgumentException if the name list is null
 111  
      * @throws ClassCastException if the list does not contain Strings
 112  
      */
 113  
     public NameFileFilter(final List<String> names) {
 114  2
         this(names, null);
 115  1
     }
 116  
 
 117  
     /**
 118  
      * Constructs a new name file filter for a list of names specifying case-sensitivity.
 119  
      *
 120  
      * @param names  the names to allow, must not be null
 121  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 122  
      * @throws IllegalArgumentException if the name list is null
 123  
      * @throws ClassCastException if the list does not contain Strings
 124  
      */
 125  2
     public NameFileFilter(final List<String> names, final IOCase caseSensitivity) {
 126  2
         if (names == null) {
 127  1
             throw new IllegalArgumentException("The list of names must not be null");
 128  
         }
 129  1
         this.names = names.toArray(new String[names.size()]);
 130  1
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 131  1
     }
 132  
 
 133  
     //-----------------------------------------------------------------------
 134  
     /**
 135  
      * Checks to see if the filename matches.
 136  
      *
 137  
      * @param file  the File to check
 138  
      * @return true if the filename matches
 139  
      */
 140  
     @Override
 141  
     public boolean accept(final File file) {
 142  1090
         final String name = file.getName();
 143  3809
         for (final String name2 : this.names) {
 144  2824
             if (caseSensitivity.checkEquals(name, name2)) {
 145  105
                 return true;
 146  
             }
 147  
         }
 148  985
         return false;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Checks to see if the filename matches.
 153  
      *
 154  
      * @param dir  the File directory (ignored)
 155  
      * @param name  the filename
 156  
      * @return true if the filename matches
 157  
      */
 158  
     @Override
 159  
     public boolean accept(final File dir, final String name) {
 160  22
         for (final String name2 : names) {
 161  14
             if (caseSensitivity.checkEquals(name, name2)) {
 162  6
                 return true;
 163  
             }
 164  
         }
 165  8
         return false;
 166  
     }
 167  
 
 168  
     /**
 169  
      * Provide a String representaion of this file filter.
 170  
      *
 171  
      * @return a String representaion
 172  
      */
 173  
     @Override
 174  
     public String toString() {
 175  64
         final StringBuilder buffer = new StringBuilder();
 176  64
         buffer.append(super.toString());
 177  64
         buffer.append("(");
 178  64
         if (names != null) {
 179  150
             for (int i = 0; i < names.length; i++) {
 180  86
                 if (i > 0) {
 181  22
                     buffer.append(",");
 182  
                 }
 183  86
                 buffer.append(names[i]);
 184  
             }
 185  
         }
 186  64
         buffer.append(")");
 187  64
         return buffer.toString();
 188  
     }
 189  
 
 190  
 }