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