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