bb.io.filefilter
Class RegexFilter
java.lang.Object
javax.swing.filechooser.FileFilter
bb.io.filefilter.BaseFilter
bb.io.filefilter.RegexFilter
- All Implemented Interfaces:
- FileFilter
- Direct Known Subclasses:
- SuffixFilter
public class RegexFilter
- extends BaseFilter
Accepts any File which matches a set of regular expressions.
Instances are constructed
with a Set of regular expressions.
A File
is accepted if it matches at least one of the Patterns (i.e. the patterns are connected by an implicit OR).
This class is multithread safe: it is immutable.
In particular, it has no listener (see the ancestor class
javadocs for more discussion).
- Author:
- Brent Boyer
Nested Class Summary |
static class |
RegexFilter.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Constructor Summary |
RegexFilter(Set<Pattern> patterns)
Calls this (patterns, FileMode.test , DirectoryMode.reject ) . |
RegexFilter(Set<Pattern> patterns,
FileMode fileMode,
DirectoryMode directoryMode)
Calls this (patterns, fileMode, directoryMode, OtherMode.reject , PartMode.name ) . |
RegexFilter(Set<Pattern> patterns,
FileMode fileMode,
DirectoryMode directoryMode,
OtherMode otherMode,
PartMode partMode)
Constructs a new RegexFilter instance. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
patterns
private final Set<Pattern> patterns
RegexFilter
public RegexFilter(Set<Pattern> patterns)
throws IllegalArgumentException
- Calls
this
(patterns, FileMode.test
, DirectoryMode.reject
)
.
So, this filter will always test normal files (and only tests the name of them),
and always rejects directories and "other" file system elements.
- Throws:
IllegalArgumentException
RegexFilter
public RegexFilter(Set<Pattern> patterns,
FileMode fileMode,
DirectoryMode directoryMode)
throws IllegalArgumentException
- Calls
this
(patterns, fileMode, directoryMode, OtherMode.reject
, PartMode.name
)
.
So, this filter will always reject "other" file system elements,
and for normal files/directories, only tests the name of each File (assuming FileMode.test
/DirectoryMode.test
).
- Throws:
IllegalArgumentException
RegexFilter
public RegexFilter(Set<Pattern> patterns,
FileMode fileMode,
DirectoryMode directoryMode,
OtherMode otherMode,
PartMode partMode)
throws IllegalArgumentException
- Constructs a new RegexFilter instance.
Contract: this instance will preserve the iteration order of patterns.
- Parameters:
patterns
- Set of regular expressions that if a File's name/path (depending on partMode) matches, result in the File being accepted;
for top performance, use a Set implementation with predictable iteration order,
and ensure that the most frequently used regular expressions are iterated over firstfileMode
- a FileMode
instance that specifies how normal files will be handleddirectoryMode
- a DirectoryMode
instance that specifies how directories will be handledotherMode
- a OtherMode
instance that specifies how "other" file system elements will be handledpartMode
- a PartMode
instance that specifies what part of a File's full path will be tested
- Throws:
IllegalArgumentException
- if patterns is null or 0-length; fileMode, directoryMode, otherMode, or partMode are null
toSet
public static Set<String> toSet(String... strings)
throws IllegalArgumentException
- Converts strings into an equivalent
Set
of String
s.
Contract: if this method returns normally, the result will be non-null, non-empty,
and the Set type will be one which perfectly preserves the original iteration order of strings.
- Parameters:
strings
- String[] of arbitrary Strings
- Throws:
IllegalArgumentException
- if strings is null or 0-length
toPatterns
public static Set<Pattern> toPatterns(Set<String> regexes)
throws IllegalArgumentException,
PatternSyntaxException
- Converts a set of
String
regular expression specifications into a set of Pattern
s.
Contract: if this method returns normally, the result will be non-null, non-empty,
and the Set type will be one which perfectly preserves the original iteration order of regexes.
- Parameters:
regexes
- Set of regular expressions (each in the syntax of Pattern
);
for top performance, use a Set implementation with predictable iteration order,
and ensure that the most frequently used regular expressions are iterated over first
- Throws:
IllegalArgumentException
- if regexes is null or 0-length
PatternSyntaxException
- if any element of regexes has an invalid regular expression syntax
getPatterns
public Set<Pattern> getPatterns()
- Returns a read-only view of
patterns
.
passesTest
protected boolean passesTest(String part)
- Description copied from class:
BaseFilter
- Determines whether or not part (which is some part of a File's full path) passes this filter's test.
The implementation here always throws an IllegalStateException.
Subclasses must either override this method or ensure that it is never called.
There are two ways that they can achieve the latter.
First, they could make FileMode/DirectoryMode/OtherMode choices that cover all cases before any other detail of the file is considered.
This is the approach taken by DirectoryFilter
and NormalFileFilter
.
Second, they could override one or more of the methods in this method's call chain
(i.e. override BaseFilter.passesTest(File)
, acceptImpl
, accept
).
in a manner that does not call the implementation here.
This is the approach taken by many classes, such as TarableFilter
and VisibleFilter
.
This method is not abstract to avoid forcing subclasses which do not call it (as described above) to implement it.
- Overrides:
passesTest
in class BaseFilter
getDescription
public String getDescription()
-
The implementation here has some introductory text, followed by a comma separated list of all of the regexes present in patterns
.
- Specified by:
getDescription
in class BaseFilter
getDescriptionIntro
protected String getDescriptionIntro()