1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 import java.io.File;
25 import java.io.FilenameFilter;
26 
27 
28 /**
29  * An implementation of the <code>java.io.FilenameFilter</code> which accepts
30  * files whose name ends with a certain string. This makes it useful for
31  * accepting files with a certain extension.
32  */
33 
34 
35 public class ExtensionFilenameFilter implements FilenameFilter{
36 
37 
38   /**
39    * The strings with one of which a file's name must end in order to pass the
40    * filter.
41    */
42 
43   private final String [] endStrings;
44 
45 
46 
47 
48   /**
49    * Creates a new ExtensionFilenameFilter with the given string. Only files
50    * ending with that string will be accepted. Note that in order to use this
51    * class to accept only files with a certain extension, you must also provide
52    * the '.' character before the extension. For example, to accept only 'txt'
53    * files, you must pass ".txt".
54    */
55 
ExtensionFilenameFilter(String endString)56   public ExtensionFilenameFilter(String endString){
57     this(new String[]{endString});
58   }
59 
60 
61 
62 
63   /**
64    * Creates a new ExtensionFilenameFilter with the given string array. Only
65    * files ending with one of the strings in the given string array will be
66    * accepted by the created ExtensionFilenameFilter.  Note that in order to use
67    * this class to accept only files with a certain extension, you must also
68    * provide the '.' character before the extension. For example, to accept
69    * only 'txt' files, you must pass ".txt".
70    */
71 
ExtensionFilenameFilter(String [] endStrings)72   public ExtensionFilenameFilter(String [] endStrings){
73     this.endStrings = new String[endStrings.length];
74     for (int i=0;i<endStrings.length;i++){
75       this.endStrings[i] = endStrings[i];
76     }
77   }
78 
79 
80 
81 
82   /**
83    * Tests whether the specified file passes the filter. Returns true if the
84    * file's name ends with one of the string specified in the constructor.
85    */
86 
accept(File dir, String filename)87   public boolean accept(File dir, String filename){
88     for (int i=0;i<endStrings.length;i++){
89       if (filename.endsWith(endStrings[i]))
90         return true;
91     }
92     return false;
93   }
94 
95 
96 }
97