Module MaterialFX

Class AbstractFilter<T,U>

java.lang.Object
io.github.palexdev.materialfx.filter.base.AbstractFilter<T,U>
Type Parameters:
T - the type of objects to filter
U - the objects' field on which to operate
Direct Known Subclasses:
BooleanFilter, EnumFilter, NumberFilter, StringFilter

public abstract class AbstractFilter<T,U> extends Object
Base class for all filters.

A filter is a class capable of operating on a given T object type for a given U field of that object.

In other words, it is capable of extracting a field U from an object T (this is the extractor function) and producing a Predicate given a certain input (also called query) and it's a String.

To make the filter system flexible and yet highly specialized, every implementation must specify a StringConverter which is used to convert the query to an object of type U.

At this point we have all the basic elements to describe how the Predicate is predicate is produced. Every implementation of this base class has some predefined BiPredicate which operate on U objects. The query is converted to an object of type U, and the extractor gets the U field from a T object, both U objects are fed to the BiPredicate. In code:
 
      // We have the query...
      String query = ...;
      U convertedQuery = converter.fromString(query);

      // We can build a Predicate<T> by doing this...
      Predicate<T> predicate = t -> biPredicate.test(extractor.apply(t), convertedQuery);
 
 

Filters are intended to be used with UI controls, they provide an interactive way to build a Predicate and filter a collection with generics, however you can also use them without an UI, however some other aspects needs to be discussed because they are strictly related to UI usage:

Every filter has a name, see MFXFilterPane documentation for an example

BiPredicates are wrapped in a BiPredicateBean

The BiPredicate to use is "selected" with an index property (ideal for comboboxes), see predicateFor(String).

  • Property Details

  • Field Details

  • Constructor Details

  • Method Details

    • defaultPredicates

      protected abstract ObservableList<BiPredicateBean<U,U>> defaultPredicates()
      Every implementation of AbstractFilter must define some default BiPredicates.
    • extend

      protected abstract AbstractFilter<T,U> extend(BiPredicateBean<U,U>... predicateBeans)
      Allows to add some extra BiPredicateBeans alongside the default ones.
    • getValue

      public U getValue(String input)
      Converts a given input String to an object of type U using the StringConverter specified by this filter.
    • predicateFor

      public Predicate<T> predicateFor(String input)
      Produces a Predicate from the given input.

      First checks if a BiPredicate is selected by checking the selected index property, see checkIndex().

      Then converts the input to an object of type U by using getValue(String), and then returns a Predicate that applies the selected BiPredicate to the extracted U field of T and the converted U input.

      In code:
       
            return t -> biPredicate.test(extractor.apply(t), convertedQuery);
       
       
    • predicateFor

      public Predicate<T> predicateFor(String input, BiPredicate<U,U> biPredicate)
      Produces a Predicate from the given input and BiPredicate.

      First converts the input to an object of type U by using getValue(String), and then returns a Predicate that applies the given BiPredicate to the extracted U field of T and the converted U input.

      In code:
       
            return t -> biPredicate.test(extractor.apply(t), convertedQuery);
       
       

      WARN: to be honest this method should have been removed but I wanted to keep it since it adds some flexibility to the filter system. Note that using this method may lead to inconsistencies in UI controls since the given argument is not a BiPredicateBean, which means that it won't be added to the predicates list of this filter, and the selected predicate index property won't be updated. This also means that any other method that relies on that index will fail.
    • toFilterBean

      public FilterBean<T,U> toFilterBean(String input)
      Converts this filter to a FilterBean from the given input.

      Checks for the selected BiPredicate, see checkIndex().
    • toFilterBean

      public FilterBean<T,U> toFilterBean(String input, ChainMode mode)
      Converts this filter to a FilterBean from the given input and ChainMode.

      Checks for the selected BiPredicate, see checkIndex().
    • toFilterBean

      public FilterBean<T,U> toFilterBean(String input, BiPredicateBean<U,U> bean, ChainMode mode)
      Converts this filter to a FilterBean from the given input, BiPredicateBean and ChainMode.
    • name

      public String name()
      Returns:
      the filter's name
    • getExtractor

      public Function<T,U> getExtractor()
      Returns:
      the function used to extract a field of type U from an object of type T
    • getPredicates

      public ObservableList<BiPredicateBean<U,U>> getPredicates()
      Returns:
      the list of usable BiPredicates, each wrapped in a BiPredicateBean
    • getSelectedPredicateIndex

      public int getSelectedPredicateIndex()
      Gets the value of the property selectedPredicateIndex.
      Property description:
      Used to specify the selected BiPredicateBean.
    • selectedPredicateIndexProperty

      public IntegerProperty selectedPredicateIndexProperty()
      Used to specify the selected BiPredicateBean.
      See Also:
    • setSelectedPredicateIndex

      public void setSelectedPredicateIndex(int selectedPredicateIndex)
      Sets the value of the property selectedPredicateIndex.
      Property description:
      Used to specify the selected BiPredicateBean.
    • getConverter

      public StringConverter<U> getConverter()
      Returns:
      the StringConverter used to convert the input String to an object of type U
    • toString

      public String toString()
      Overrides:
      toString in class Object