net.sourceforge.pmd.util.filter

Class Filters

public class Filters extends Object

Utility class for working with Filters. Contains builder style methods, apply methods, as well as mechanisms for adapting Filters and FilenameFilters.
Method Summary
static Filter<String>buildRegexFilterExcludeOverInclude(List<String> includeRegexes, List<String> excludeRegexes)
Construct a String Filter using set of include and exclude regular expressions.
static Filter<String>buildRegexFilterIncludeOverExclude(List<String> includeRegexes, List<String> excludeRegexes)
Construct a String Filter using set of include and exclude regular expressions.
static <T> List<T>filter(Filter<T> filter, Collection<T> collection)
Filter a given Collection.
static <T> Filter<T>fromStringFilter(Filter<String> filter)
Given a String Filter, expose as a Filter on another type.
static Filter<File>getDirectoryFilter()
Get a File Filter for directories.
static Filter<File>getFileExtensionFilter(String... extensions)
Get a File Filter for files with the given extensions, ignoring case.
static Filter<File>getFileExtensionOrDirectoryFilter(String... extensions)
Get a File Filter for directories or for files with the given extensions, ignoring case.
static Filter<File>toFileFilter(FilenameFilter filter)
Given a FilenameFilter, expose as a File Filter.
static FilenameFiltertoFilenameFilter(Filter<File> filter)
Given a File Filter, expose as a FilenameFilter.
static Filter<File>toNormalizedFileFilter(Filter<String> filter)
Given a String Filter, expose as a File Filter.

Method Detail

buildRegexFilterExcludeOverInclude

public static Filter<String> buildRegexFilterExcludeOverInclude(List<String> includeRegexes, List<String> excludeRegexes)
Construct a String Filter using set of include and exclude regular expressions. If there are no include regular expressions provide, then a regular expression is added which matches every String by default. A String is included as long as it matches an include regular expression and does not match an exclude regular expression.

In other words, exclude patterns override include patterns.

Parameters: includeRegexes The include regular expressions. May be null. excludeRegexes The exclude regular expressions. May be null.

Returns: A String Filter.

buildRegexFilterIncludeOverExclude

public static Filter<String> buildRegexFilterIncludeOverExclude(List<String> includeRegexes, List<String> excludeRegexes)
Construct a String Filter using set of include and exclude regular expressions. If there are no include regular expressions provide, then a regular expression is added which matches every String by default. A String is included as long as the case that there is an include which matches or there is not an exclude which matches.

In other words, include patterns override exclude patterns.

Parameters: includeRegexes The include regular expressions. May be null. excludeRegexes The exclude regular expressions. May be null.

Returns: A String Filter.

filter

public static <T> List<T> filter(Filter<T> filter, Collection<T> collection)
Filter a given Collection.

Parameters: Type of the Collection. filter A Filter upon the Type of objects in the Collection. collection The Collection to filter.

Returns: A List containing only those objects for which the Filter returned true.

fromStringFilter

public static <T> Filter<T> fromStringFilter(Filter<String> filter)
Given a String Filter, expose as a Filter on another type. The toString() method is called on the objects of the other type and delegated to the String Filter.

Parameters: The desired type. filter The existing String Filter.

Returns: A Filter on the desired type.

getDirectoryFilter

public static Filter<File> getDirectoryFilter()
Get a File Filter for directories.

Returns: A File Filter.

getFileExtensionFilter

public static Filter<File> getFileExtensionFilter(String... extensions)
Get a File Filter for files with the given extensions, ignoring case.

Parameters: extensions The extensions to filter.

Returns: A File Filter.

getFileExtensionOrDirectoryFilter

public static Filter<File> getFileExtensionOrDirectoryFilter(String... extensions)
Get a File Filter for directories or for files with the given extensions, ignoring case.

Parameters: extensions The extensions to filter.

Returns: A File Filter.

toFileFilter

public static Filter<File> toFileFilter(FilenameFilter filter)
Given a FilenameFilter, expose as a File Filter.

Parameters: filter The FilenameFilter.

Returns: A File Filter.

toFilenameFilter

public static FilenameFilter toFilenameFilter(Filter<File> filter)
Given a File Filter, expose as a FilenameFilter.

Parameters: filter The File Filter.

Returns: A FilenameFilter.

toNormalizedFileFilter

public static Filter<File> toNormalizedFileFilter(Filter<String> filter)
Given a String Filter, expose as a File Filter. The File paths are normalized to a standard pattern using / as a path separator which can be used cross platform easily in a regular expression based String Filter.

Parameters: filter A String Filter.

Returns: A File Filter.