Coverage Report - org.jtheque.utils.io.SimpleFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleFilter
100%
17/17
100%
12/12
4
 
 1  
 package org.jtheque.utils.io;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileFilter;
 21  
 import java.util.ArrayList;
 22  
 import java.util.List;
 23  
 import java.util.Locale;
 24  
 import java.util.StringTokenizer;
 25  
 
 26  
 /**
 27  
  * A simple file filter.
 28  
  *
 29  
  * @author Baptiste Wicht
 30  
  */
 31  
 public class SimpleFilter implements FileFilter {
 32  
     private final String description;
 33  16
     private final List<String> acceptedExtensions = new ArrayList<String>(5);
 34  
 
 35  
     /**
 36  
      * Construct a new FilterSimple.
 37  
      *
 38  
      * @param description The description of the filter.
 39  
      * @param extensions  The extension accepted by the filter, in comma-separated format.
 40  
      */
 41  
     public SimpleFilter(String description, String extensions) {
 42  16
         super();
 43  
 
 44  16
         if (description == null || extensions == null) {
 45  6
             throw new IllegalArgumentException("Neither the description or the extension list can be null. ");
 46  
         }
 47  
 
 48  10
         this.description = description;
 49  
 
 50  10
         StringTokenizer token = new StringTokenizer(extensions, ",");
 51  
 
 52  26
         while (token.hasMoreTokens()) {
 53  16
             acceptedExtensions.add(token.nextToken());
 54  
         }
 55  10
     }
 56  
 
 57  
     @Override
 58  
     public final boolean accept(File file) {
 59  28
         String fileName = file.getName().toLowerCase(Locale.getDefault());
 60  
 
 61  28
         if (file.isDirectory()) {
 62  4
             return true;
 63  
         }
 64  
 
 65  24
         for (String extension : acceptedExtensions) {
 66  36
             if (fileName.endsWith(extension)) {
 67  10
                 return true;
 68  
             }
 69  
         }
 70  
 
 71  14
         return fileName.endsWith(acceptedExtensions.get(acceptedExtensions.size() - 1));
 72  
     }
 73  
 
 74  
     /**
 75  
      * Return the description of the filter.
 76  
      *
 77  
      * @return The description of the filter.
 78  
      */
 79  
     public final String getDescription() {
 80  2
         return description;
 81  
     }
 82  
 }