Package org.patternfly.filter
package org.patternfly.filter
Flexible filtering API for objects with support for multiple attributes and operators.
This package provides a composable filtering system that allows you to define filter attributes, combine them with logical operators, and apply them to collections or individual objects. Filters support change notifications, persistence, and can be integrated with data providers and UI components.
Key Classes
Filter- Main filter class that holds multiple attributes and evaluates themFilterAttribute- Represents a single filterable attribute with a name, value, and conditionFilterCondition- Functional interface that defines how to match an object against a valueFilterOperator- Enum for combining attributes (AND/OR)FilterChangeHandler- Handler for filter state change notificationsFilterAttributeModifier- Function for custom attribute value modification logic
Basic Usage
Create a filter with multiple attributes, set values, and apply it to a collection:
// Create a filter combining attributes with AND
Filter<User> filter = new Filter<>(FilterOperator.AND);
// Add filter attributes with conditions
filter.add(new FilterAttribute<>("name", (user, value) -> user.name().contains(value)));
filter.add(new FilterAttribute<>("active", (user, value) -> user.isActive() == value));
// Set filter values
filter.set("name", "John");
filter.set("active", true);
// Apply filter to a list
List<User> filtered = filter.filter(allUsers);
// Or match a single object
boolean matches = filter.match(user);
Features
Logical Operators
Combine multiple filter attributes using FilterOperator.AND or
FilterOperator.OR:
// Match users where name contains "John" OR email contains "john"
Filter<User> filter = new Filter<>(FilterOperator.OR)
.add(new FilterAttribute<>("name", (u, v) -> u.name().contains(v)))
.add(new FilterAttribute<>("email", (u, v) -> u.email().contains(v)));
Change Notifications
Register handlers to be notified when filter state changes:
filter.onChange((f, origin) -> {
console.log("Filter changed from: " + origin);
updateUI(f.filter(users));
});
Persistence
Save and restore filter state using string serialization:
// Mark attributes as persistent
filter.add(new FilterAttribute<>("name", true, nameCondition));
// Save filter state
String state = filter.save();
// Later, restore filter state
filter.load(state);
Custom Attribute Modifiers
Control how attribute values are updated using FilterAttributeModifier:
// Toggle boolean values instead of replacing them
filter.set("flag", true, (oldValue, newValue) -> !oldValue);
Integration with Data Provider
Filters work seamlessly with DataProvider:
Filter<User> filter = new Filter<>(FilterOperator.AND)
.add(new FilterAttribute<>("search", (u, v) -> u.name().contains(v)));
DataProvider<User> dataProvider = new DataProvider<>(User::id);
filter.onChange((f, origin) -> {
dataProvider.addFilter("user-filter", user -> f.match(user));
});
- See Also:
-
ClassDescriptionFilter<T>A class representing a filter that can be applied to a list of objects (
Filter.filter(List)) or to a single object (Filter.match(Object)).FilterAttribute<T,V> Represents a filter attribute with a specified name, persistence flag, and filter condition.A functional interface for modifying filter attribute values.The FilterChangeHandler interface is designed to handle changes in filters.FilterCondition<T,V> Functional interface representing a condition used for filtering objects.Represents the logical operators used for combining multipleFilterAttributes of aFilter.