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