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