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,?>>

public class Filter<T> extends Object implements 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
    Modifier and Type
    Field
    Description
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <V> Filter<T>
    add(FilterAttribute<T,V> attribute)
     
    boolean
    Checks if any of the filter attributes in this filter is defined.
    boolean
    Checks if a filter attribute with the specified name is defined.
    filter(List<T> objects)
    Filters a list of objects based on the attributes defined in this filter.
    filter(List<T> objects, BiConsumer<T,Boolean> callback)
    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.
    get(String name)
    Retrieves the filter attribute associated with the given name.
     
    void
    load(String filter)
    Loads and applies filter attributes from a string representation.
    boolean
    match(T object)
    Matches 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.
    void
    reset(String name)
    Resets the filter attribute with the specified name to its initial value.
    void
    reset(String name, String origin)
    Resets the filter attribute with the specified name to its initial value.
    void
    Resets all filter attributes to their initial values.
    void
    resetAll(String origin)
    Resets all filter attributes to their initial values.
    Serializes the persistent and defined filter attributes into a string representation.
    <V> void
    set(String name, V value)
    Sets the value of the filter attribute with the specified name.
    <V> void
    set(String name, V value, String origin)
    Sets the value of the filter attribute with the specified name.
    <V> void
    set(String name, V value, FilterAttributeModifier<V> modifier)
    Sets the value of the filter attribute with the specified name using a custom modifier.
    <V> void
    set(String name, V value, FilterAttributeModifier<V> modifier, String origin)
    Sets the value of the filter attribute with the specified name using a custom modifier.
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

  • Constructor Details

  • Method Details

    • add

      public <V> Filter<T> add(FilterAttribute<T,V> attribute)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public Iterator<FilterAttribute<T,?>> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • filter

      public List<T> filter(List<T> objects)
      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

      public List<T> filter(List<T> objects, BiConsumer<T,Boolean> callback)
      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 filtered
      callback - 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

      public boolean match(T object)
      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

      public boolean defined(String name)
      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

      public <V> FilterAttribute<T,V> get(String name)
      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

      public <V> void set(String name, V value)
      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 set
      value - the new value to be set for the filter attribute
    • set

      public <V> void set(String name, V value, String origin)
      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 set
      value - the new value to be set for the filter attribute
      origin - the origin of the change which will be passed to the change handlers
    • set

      public <V> void set(String name, V value, FilterAttributeModifier<V> modifier)
      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 set
      value - the new value to be set for the filter attribute
      modifier - the modifier that defines custom logic for how the attribute value should be set
    • set

      public <V> void set(String name, V value, FilterAttributeModifier<V> modifier, String origin)
      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 set
      value - the new value to be set for the filter attribute
      modifier - the modifier that defines custom logic for how the attribute value should be set
      origin - the origin of the change which will be passed to the change handlers
    • reset

      public void reset(String name)
      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

      public void reset(String name, String origin)
      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
      origin - 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

      public void resetAll(String origin)
      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

      public String 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

      public void load(String filter)
      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

      public Filter<T> onChange(FilterChangeHandler<T> changeHandler)
      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