Package dev.zemco.codegame.util
Class BindingUtils
java.lang.Object
dev.zemco.codegame.util.BindingUtils
Collection of utility functions for simplifying manipulation of JavaFX bindings.
Currently, these functions only consist of helper mapping functions that allow us to transform
observable properties using mappers. This was done because recommended solution that JavaFX provides
is walking properties using Bindings.selectString(Object, String...).
This, in our opinion, is error-prone to changes, and alternatives tend to be harder to read.
To combat this issue we wrap creation of new bindings with simple to use and read mapping interface.
- Author:
- Erik Zemčík
-
Method Summary
Modifier and TypeMethodDescriptionstatic <F> javafx.beans.binding.StringBindingCreates binding that maps property toStringusing amapper.static <F> javafx.beans.binding.StringBindingmapOrDefault(javafx.beans.value.ObservableValue<F> property, Function<F, String> mapper, String defaultValue) Creates binding that maps property toStringusing amapper, falling back todefaultValueif property value isnull.static <F> javafx.beans.binding.StringBindingCreates binding that maps property toStringusing amapper, falling back tonullif property value isnull.
-
Method Details
-
map
public static <F> javafx.beans.binding.StringBinding map(javafx.beans.value.ObservableValue<F> property, Function<F, String> mapper) Creates binding that maps property toStringusing amapper.- Type Parameters:
F- describes type ofpropertywhich is passed tomapper- Parameters:
property- source of values that are used for mappingmapper-Functionthat transforms value frompropertytoString- Returns:
- binding that computes
Stringvalue usingmapper - Throws:
IllegalArgumentException- ifpropertyormapperisnull- See Also:
-
mapOrDefault
public static <F> javafx.beans.binding.StringBinding mapOrDefault(javafx.beans.value.ObservableValue<F> property, Function<F, String> mapper, String defaultValue) Creates binding that maps property toStringusing amapper, falling back todefaultValueif property value isnull.- Type Parameters:
F- describes type ofpropertywhich is passed tomapper- Parameters:
property- source of values that are used for mappingmapper-Functionthat transforms non-null value frompropertytoStringdefaultValue- fallback value if property value isnull- Returns:
- binding that computes
Stringvalue usingmapper - Throws:
IllegalArgumentException- ifpropertyormapperisnull- See Also:
-
mapOrNull
public static <F> javafx.beans.binding.StringBinding mapOrNull(javafx.beans.value.ObservableValue<F> property, Function<F, String> mapper) Creates binding that maps property toStringusing amapper, falling back tonullif property value isnull.- Type Parameters:
F- describes type ofpropertywhich is passed tomapper- Parameters:
property- source of values that are used for mappingmapper-Functionthat transforms non-null value frompropertytoString- Returns:
- binding that computes
Stringvalue usingmapper - Throws:
IllegalArgumentException- ifpropertyormapperisnull- See Also:
-