bb.io.filefilter
Class CompoundFilter

java.lang.Object
  extended by javax.swing.filechooser.FileFilter
      extended by bb.io.filefilter.CompoundFilter
All Implemented Interfaces:
FileFilter

public class CompoundFilter
extends FileFilter
implements FileFilter

Meta filter whose accept method incorporates all the constraints of all the file filters that it was constructed with.

Note that RegexFilter and SuffixFilter can be viewed as specialized types of compound filters: each has a set of criteria that are connected by an implicit logical OR. Using/subclassing RegexFilter or SuffixFilter is therefore more convenient than this class if that is the desired behavior.

In contrast, this class offers much more flexibility: any type of filters can be used, and the filters can be connected by logical AND as well logical OR.

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 if all the elements in its filters field are multithread safe, since other than possible deep state in those elements, it is immutable. In particular, it is always properly constructed, all of its fields are final, and none of their state can be changed after construction except possibly any deep state inside filters' elements. See p. 53 of Java Concurrency In Practice for more discussion.

Author:
Brent Boyer

Nested Class Summary
private static class CompoundFilter.LogicMode
          Enum of all the supported modes for applying the individual filters.
static class CompoundFilter.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  Set<FileFilter> filters
          FileFilters which will be used in accept and getDescription.
private  CompoundFilter.LogicMode logicMode
           
 
Constructor Summary
protected CompoundFilter(CompoundFilter.LogicMode logicMode, FileFilter... filters)
          Constructor.
 
Method Summary
 boolean accept(File file)
          Reports whether or not file is accepted by this instance.
 String getDescription()
          Returns a description of this filter.
private  String getDescription(FileFilter filter)
           
private  String getLogicModeDescription()
           
static CompoundFilter makeAnd(FileFilter... filters)
          Returns a new CompoundFilter instance that connects the elements of filters via logical AND.
static CompoundFilter makeOr(FileFilter... filters)
          Returns a new CompoundFilter instance that connects the elements of filters via logical OR.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logicMode

private final CompoundFilter.LogicMode logicMode

filters

private final Set<FileFilter> filters
FileFilters which will be used in accept and getDescription.

Constructor Detail

CompoundFilter

protected CompoundFilter(CompoundFilter.LogicMode logicMode,
                         FileFilter... filters)
                  throws IllegalArgumentException
Constructor.

Parameters:
logicMode - assigned to the logicMode field
filters - assigned to the filters field; for top performance, place the most frequently encountered patterns earliest in the array
Throws:
IllegalArgumentException - if logicMode == null; filters == null; filters.length == 0
Method Detail

makeAnd

public static CompoundFilter makeAnd(FileFilter... filters)
                              throws IllegalArgumentException
Returns a new CompoundFilter instance that connects the elements of filters via logical AND.

Parameters:
filters - assigned to the filters field; for top performance, place the most frequently encountered patterns earliest in the array
Throws:
IllegalArgumentException - if filters == null; filters.length == 0

makeOr

public static CompoundFilter makeOr(FileFilter... filters)
                             throws IllegalArgumentException
Returns a new CompoundFilter instance that connects the elements of filters via logical OR.

Parameters:
filters - assigned to the filters field; for top performance, place the most frequently encountered patterns earliest in the array
Throws:
IllegalArgumentException - if filters == null; filters.length == 0

accept

public boolean accept(File file)
               throws IllegalArgumentException,
                      SecurityException,
                      IllegalStateException
Reports whether or not file is accepted by this instance. Applies all the filters according to logicMode.

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
IllegalStateException - if the class finds itself in an unexpected state

getDescription

public String getDescription()
Returns a description of this filter. Is simply a concatenation of the descriptions of all the filters.

Specified by:
getDescription in class FileFilter

getLogicModeDescription

private String getLogicModeDescription()
                                throws IllegalStateException
Throws:
IllegalStateException

getDescription

private String getDescription(FileFilter filter)