Package org.patternfly.filter
Class Filter<T>
java.lang.Object
org.patternfly.filter.Filter<T>
- Type Parameters:
T- The type of object that this filter will be applied to.
- All Implemented Interfaces:
Iterable<FilterAttribute<T,?>>
A class representing a filter that can be applied to a list of objects (
filter(List)) or to a single object
(match(Object)).
A filter is made up of a list of FilterAttributes. These attributes can be assigned a value, evaluated, and reset.
Multiple FilterAttributes are combined with FilterOperator.AND or FilterOperator.OR.
The filter class supports change notifications whenever the filter state changes via the
onChange(FilterChangeHandler) method and can be persisted using save() and load(String) methods.
Filter<String> filter = new Filter<>(FilterOperator.AND);
filter.add(new FilterAttribute<String, String>("startsWith", String::startsWith));
filter.add(new FilterAttribute<String, String>("contains", String::contains));
filter.add(new FilterAttribute<String, String>("endsWith", String::endsWith));
filter.add(new FilterAttribute<String, Boolean>("lowercase",
(string, lowercase) -> lowercase == string.toLowerCase().equals(string)));
filter.set("startsWith", "f");
filter.set("contains", "o");
filter.set("endsWith", "o");
filter.set("lowercase", true);
List<String> filtered = filter.filter(asList("foo", "fOo", "FoO", "bar")); // [foo]
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(FilterAttribute<T, V> attribute) booleandefined()Checks if any of the filter attributes in this filter is defined.booleanChecks if a filter attribute with the specified name is defined.Filters a list of objects based on the attributes defined in this filter.Filters a list of objects based on the attributes defined in this filter and calls the specified callback for each object indicating whether it matches the filter criteria.<V> FilterAttribute<T, V> Retrieves the filter attribute associated with the given name.Iterator<FilterAttribute<T, ?>> iterator()voidLoads and applies filter attributes from a string representation.booleanMatches an object against the filter criteria defined in the filter attributes.onChange(FilterChangeHandler<T> changeHandler) Registers a change handler that will be called whenever the filter is changed.voidResets the filter attribute with the specified name to its initial value.voidResets the filter attribute with the specified name to its initial value.voidresetAll()Resets all filter attributes to their initial values.voidResets all filter attributes to their initial values.save()Serializes the persistent and defined filter attributes into a string representation.<V> voidSets the value of the filter attribute with the specified name.<V> voidSets the value of the filter attribute with the specified name.<V> voidset(String name, V value, FilterAttributeModifier<V> modifier) Sets the value of the filter attribute with the specified name using a custom modifier.<V> voidset(String name, V value, FilterAttributeModifier<V> modifier, String origin) Sets the value of the filter attribute with the specified name using a custom modifier.toString()Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
DEFAULT_ORIGIN
- See Also:
-
-
Constructor Details
-
Filter
-
-
Method Details
-
add
-
toString
-
iterator
-
filter
Filters a list of objects based on the attributes defined in this filter.- Parameters:
objects- the list of objects to be filtered- Returns:
- a list of objects that match the filter criteria
-
filter
Filters a list of objects based on the attributes defined in this filter and calls the specified callback for each object indicating whether it matches the filter criteria.- Parameters:
objects- the list of objects to be filteredcallback- a callback that is executed for each object, receiving the object and a boolean indicating whether the object matches the filter criteria- Returns:
- a list of objects that match the filter criteria
-
match
Matches an object against the filter criteria defined in the filter attributes.- Parameters:
object- the object to be tested against the filter criteria- Returns:
- true if the object matches the filter criteria, false otherwise
-
defined
public boolean defined()Checks if any of the filter attributes in this filter is defined.- Returns:
- true if at least one filter attribute is defined, false otherwise
-
defined
Checks if a filter attribute with the specified name is defined.- Parameters:
name- the name of the filter attribute to check- Returns:
- true if the filter attribute is defined, false otherwise
-
get
Retrieves the filter attribute associated with the given name.- Type Parameters:
V- the type of the value for the filter attribute- Parameters:
name- the name of the filter attribute to retrieve- Returns:
- the filter attribute associated with the given name, or null if no such attribute exists
-
set
Sets the value of the filter attribute with the specified name. If the attribute exists and its value is updated, the change handlers are notified.- Type Parameters:
V- the type of the value for the filter attribute- Parameters:
name- the name of the filter attribute to setvalue- the new value to be set for the filter attribute
-
set
Sets the value of the filter attribute with the specified name. If the attribute exists and its value is updated, the change handlers are notified.- Type Parameters:
V- the type of the value for the filter attribute- Parameters:
name- the name of the filter attribute to setvalue- the new value to be set for the filter attributeorigin- the origin of the change which will be passed to the change handlers
-
set
Sets the value of the filter attribute with the specified name using a custom modifier. If the attribute exists and its value is updated, the change handlers are notified.- Type Parameters:
V- the type of the value for the filter attribute- Parameters:
name- the name of the filter attribute to setvalue- the new value to be set for the filter attributemodifier- the modifier that defines custom logic for how the attribute value should be set
-
set
Sets the value of the filter attribute with the specified name using a custom modifier. If the attribute exists and its value is updated, the change handlers are notified.- Type Parameters:
V- the type of the value for the filter attribute- Parameters:
name- the name of the filter attribute to setvalue- the new value to be set for the filter attributemodifier- the modifier that defines custom logic for how the attribute value should be setorigin- the origin of the change which will be passed to the change handlers
-
reset
Resets the filter attribute with the specified name to its initial value. If the attribute is successfully reset, the change handlers are notified.- Parameters:
name- the name of the filter attribute to reset
-
reset
Resets the filter attribute with the specified name to its initial value. If the attribute is successfully reset, the change handlers are notified.- Parameters:
name- the name of the filter attribute to resetorigin- the origin of the change which will be passed to the change handlers
-
resetAll
public void resetAll()Resets all filter attributes to their initial values. If any attributes were reset, the change handlers are notified. -
resetAll
Resets all filter attributes to their initial values. If any attributes were reset, the change handlers are notified.- Parameters:
origin- the origin of the change which will be passed to the change handlers
-
save
Serializes the persistent and defined filter attributes into a string representation. The format of the string includes each attribute's name and its corresponding value, separated by a predefined name-value separator. Each attribute pair is joined by a filter attribute separator.- Returns:
- a string representation of the persistent and defined filter attributes
-
load
Loads and applies filter attributes from a string representation. The input string should contain name-value pairs separated by a predefined separator. Each name-value pair is split using a specified name-value separator. If a specified attribute is found in this filter, it will have its value loaded from the corresponding value in the name-value pair.- Parameters:
filter- a string representation of the filter attributes, where each attribute is represented as a name-value pair and pairs are separated by a specific separator.
-
onChange
Registers a change handler that will be called whenever the filter is changed.- Parameters:
changeHandler- the change handler to be registered- Returns:
- the current filter instance with the registered change handler
-