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 1471767 2013-04-24 23:24:19Z sebb $
 41  
  * @see FileFilterUtils#nameFileFilter(String)
 42  
  * @see FileFilterUtils#nameFileFilter(String, IOCase)
 43  
  */
 44  
 public class NameFileFilter extends AbstractFileFilter implements Serializable {
 45  
 
 46  
     /** The filenames to search for */
 47  
     private final String[] names;
 48  
     /** Whether the comparison is case sensitive. */
 49  
     private final IOCase caseSensitivity;
 50  
 
 51  
     /**
 52  
      * Constructs a new case-sensitive name file filter for a single name.
 53  
      *
 54  
      * @param name  the name to allow, must not be null
 55  
      * @throws IllegalArgumentException if the name is null
 56  
      */
 57  
     public NameFileFilter(final String name) {
 58  56
         this(name, null);
 59  54
     }
 60  
 
 61  
     /**
 62  
      * Construct a new name file filter specifying case-sensitivity.
 63  
      *
 64  
      * @param name  the name to allow, must not be null
 65  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 66  
      * @throws IllegalArgumentException if the name is null
 67  
      */
 68  60
     public NameFileFilter(final String name, final IOCase caseSensitivity) {
 69  60
         if (name == null) {
 70  4
             throw new IllegalArgumentException("The wildcard must not be null");
 71  
         }
 72  56
         this.names = new String[] {name};
 73  56
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 74  56
     }
 75  
 
 76  
     /**
 77  
      * Constructs a new case-sensitive name file filter for an array of names.
 78  
      * <p>
 79  
      * The array is not cloned, so could be changed after constructing the
 80  
      * instance. This would be inadvisable however.
 81  
      *
 82  
      * @param names  the names to allow, must not be null
 83  
      * @throws IllegalArgumentException if the names array is null
 84  
      */
 85  
     public NameFileFilter(final String[] names) {
 86  18
         this(names, null);
 87  16
     }
 88  
 
 89  
     /**
 90  
      * Constructs a new name file filter for an array of names specifying case-sensitivity.
 91  
      *
 92  
      * @param names  the names to allow, must not be null
 93  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 94  
      * @throws IllegalArgumentException if the names array is null
 95  
      */
 96  26
     public NameFileFilter(final String[] names, final IOCase caseSensitivity) {
 97  26
         if (names == null) {
 98  2
             throw new IllegalArgumentException("The array of names must not be null");
 99  
         }
 100  24
         this.names = new String[names.length];
 101  24
         System.arraycopy(names, 0, this.names, 0, names.length);
 102  24
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 103  24
     }
 104  
 
 105  
     /**
 106  
      * Constructs a new case-sensitive name file filter for a list of names.
 107  
      *
 108  
      * @param names  the names to allow, must not be null
 109  
      * @throws IllegalArgumentException if the name list is null
 110  
      * @throws ClassCastException if the list does not contain Strings
 111  
      */
 112  
     public NameFileFilter(final List<String> names) {
 113  4
         this(names, null);
 114  2
     }
 115  
 
 116  
     /**
 117  
      * Constructs a new name file filter for a list of names specifying case-sensitivity.
 118  
      *
 119  
      * @param names  the names to allow, must not be null
 120  
      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 121  
      * @throws IllegalArgumentException if the name list is null
 122  
      * @throws ClassCastException if the list does not contain Strings
 123  
      */
 124  4
     public NameFileFilter(final List<String> names, final IOCase caseSensitivity) {
 125  4
         if (names == null) {
 126  2
             throw new IllegalArgumentException("The list of names must not be null");
 127  
         }
 128  2
         this.names = names.toArray(new String[names.size()]);
 129  2
         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
 130  2
     }
 131  
 
 132  
     //-----------------------------------------------------------------------
 133  
     /**
 134  
      * Checks to see if the filename matches.
 135  
      *
 136  
      * @param file  the File to check
 137  
      * @return true if the filename matches
 138  
      */
 139  
     @Override
 140  
     public boolean accept(final File file) {
 141  2042
         final String name = file.getName();
 142  7096
         for (final String name2 : this.names) {
 143  5268
             if (caseSensitivity.checkEquals(name, name2)) {
 144  214
                 return true;
 145  
             }
 146  
         }
 147  1828
         return false;
 148  
     }
 149  
 
 150  
     /**
 151  
      * Checks to see if the filename matches.
 152  
      *
 153  
      * @param dir  the File directory (ignored)
 154  
      * @param name  the filename
 155  
      * @return true if the filename matches
 156  
      */
 157  
     @Override
 158  
     public boolean accept(final File dir, final String name) {
 159  44
         for (final String name2 : names) {
 160  28
             if (caseSensitivity.checkEquals(name, name2)) {
 161  12
                 return true;
 162  
             }
 163  
         }
 164  16
         return false;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Provide a String representaion of this file filter.
 169  
      *
 170  
      * @return a String representaion
 171  
      */
 172  
     @Override
 173  
     public String toString() {
 174  128
         final StringBuilder buffer = new StringBuilder();
 175  128
         buffer.append(super.toString());
 176  128
         buffer.append("(");
 177  128
         if (names != null) {
 178  300
             for (int i = 0; i < names.length; i++) {
 179  172
                 if (i > 0) {
 180  44
                     buffer.append(",");
 181  
                 }
 182  172
                 buffer.append(names[i]);
 183  
             }
 184  
         }
 185  128
         buffer.append(")");
 186  128
         return buffer.toString();
 187  
     }
 188  
 
 189  
 }