bb.io.filefilter
Class FilenameSuffixFilter

java.lang.Object
  extended by javax.swing.filechooser.FileFilter
      extended by bb.io.filefilter.FilenameSuffixFilter
All Implemented Interfaces:
FileFilter
Direct Known Subclasses:
CameraFileFilter, ClassFilter, CompoundFilter.UnitTest.TestFilter1, CompoundFilter.UnitTest.TestFilter2, FilenameSuffixFilter.UnitTest.TestFilter, HtmlFilter, JarFilter, JavaFilter, ShellScriptFileFilter, SoundFileFilter, TarFilter, ZipAndJarFilter, ZipFilter

public abstract class FilenameSuffixFilter
extends FileFilter
implements FileFilter

This abstract class is meant to provide all the base functionality for file filters which accept or reject Files depending on how their names end.

This class must be constructed with a String[] of file name suffix patterns. A normal file is always accepted if the suffix of its name matches one of the patterns in the array. The matching of the filename suffix to a pattern String is done char by char. A boolean arg to the constructor specifies whether or not the matching is case insensitive.

This class's constructor must also take an int arg that specifies how to treat directories. The possibilities are:

This class defines constants for each of the above behaviors (see the FilenameSuffixFilter(java.lang.String[], boolean, int) for details).

Concrete subclasses typically just need to define (and supply to this class's constructor) an array of suffixes, as well as implement the getDescription method.

Note that this class is both a subclass of javax.swing.filechooser.FileFilter (so that it can be passed to JFileChooser), as well as an implementation of java.io.FileFilter (so that it is suitable as an argument to File.listFiles).

This class is multithread safe: it is immutable (both its immediate state, as well as the deep state of its fields).

Author:
Brent Boyer

Nested Class Summary
static class FilenameSuffixFilter.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
static int acceptDirectories
          Specifies that every directory will be accepted, regardless of its name.
private  boolean caseSensitive
           
private  int directoryTreatment
           
private  String[] filenameSuffixes
           
static int filterDirectoriesBySuffix
          Specifies that every directory will be subjected to the same name suffix test as normal files.
static int rejectDirectories
          Specifies that every directory will be rejected, regardless of its name.
 
Constructor Summary
FilenameSuffixFilter(String[] filenameSuffixes, boolean caseSensitive, int directoryTreatment)
          Constructs a new FilenameSuffixFilter instance.
 
Method Summary
 boolean accept(File file)
          Reports whether or not the supplied File is accepted by this filter.
abstract  String getDescription()
          Returns a description of this filter.
 String[] getSuffixes()
          Returns a clone of the array of filenameSuffixes that are used to match files.
protected  boolean isValidDirectoryTreatment(int directoryTreatment)
          Reports whether or not the directoryTreatment arg is a valid value.
static String makeDescription(String intro, String[] suffixes)
          Returns a String that consists of the intro arg followed by the Strings in the suffixes arg.
protected  boolean suffixMatches(String fileName)
          Reports whether or not the supplied fileName's suffix matches one of the patterns.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rejectDirectories

public static final int rejectDirectories
Specifies that every directory will be rejected, regardless of its name.

See Also:
Constant Field Values

filterDirectoriesBySuffix

public static final int filterDirectoriesBySuffix
Specifies that every directory will be subjected to the same name suffix test as normal files.

See Also:
Constant Field Values

acceptDirectories

public static final int acceptDirectories
Specifies that every directory will be accepted, regardless of its name.

See Also:
Constant Field Values

filenameSuffixes

private final String[] filenameSuffixes

caseSensitive

private final boolean caseSensitive

directoryTreatment

private final int directoryTreatment
Constructor Detail

FilenameSuffixFilter

public FilenameSuffixFilter(String[] filenameSuffixes,
                            boolean caseSensitive,
                            int directoryTreatment)
                     throws IllegalArgumentException
Constructs a new FilenameSuffixFilter instance.

Parameters:
filenameSuffixes - String[] of filename suffix patterns that if a file matches, result in the file being accepted; for top performance, place the most frequently encountered patterns earliest in the array
caseSensitive - specifies whether or not the filename suffix comparison is to be case sensitive
directoryTreatment - an int that must be one of the following constants of this class: rejectDirectories, filterDirectoriesBySuffix, or acceptDirectories
Throws:
IllegalArgumentException - if filenameSuffixes is null or 0-length, or if directoryTreatment is invalid
Method Detail

getSuffixes

public String[] getSuffixes()
Returns a clone of the array of filenameSuffixes that are used to match files.


isValidDirectoryTreatment

protected boolean isValidDirectoryTreatment(int directoryTreatment)
Reports whether or not the directoryTreatment arg is a valid value.


accept

public boolean accept(File file)
               throws IllegalArgumentException,
                      SecurityException
Reports whether or not the supplied File is accepted by this filter.

Specified by:
accept in interface FileFilter
Specified by:
accept in class FileFilter
Throws:
IllegalArgumentException - if file is null
SecurityException - if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file

suffixMatches

protected boolean suffixMatches(String fileName)
Reports whether or not the supplied fileName's suffix matches one of the patterns.


getDescription

public abstract String getDescription()
Returns a description of this filter.

Specified by:
getDescription in class FileFilter

makeDescription

public static String makeDescription(String intro,
                                     String[] suffixes)
                              throws IllegalArgumentException
Returns a String that consists of the intro arg followed by the Strings in the suffixes arg. The suffixes are put into a single comma separated list that is enclosed by parentheses. Each suffix in that list is preceded by an asterisk, to indicate that the initial filename chars are a wildchar match.

This method is a convenience method to help subclasses generate a description of what file types they filter.

Throws:
IllegalArgumentException - if intro or arg suffixes is null