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 them
  • FilterAttribute - Represents a single filterable attribute with a name, value, and condition
  • FilterCondition - Functional interface that defines how to match an object against a value
  • FilterOperator - Enum for combining attributes (AND/OR)
  • FilterChangeHandler - Handler for filter state change notifications
  • FilterAttributeModifier - 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: