Module MaterialFX

Class BiBindingHelper<T>

java.lang.Object
io.github.palexdev.materialfx.bindings.base.AbstractBindingHelper<T>
io.github.palexdev.materialfx.bindings.BiBindingHelper<T>
Type Parameters:
T - the properties' value type

public class BiBindingHelper<T> extends AbstractBindingHelper<T>
Binding helper for bidirectional bindings.

Bidirectional bindings are syntactical sugar, because basically it's a listener attached to one or more observable values which acts as 'sources', when one of them change the target is updated and all the other sources are updated.

There is one issue though: when a source changes and the value is updated, there's a 'bounce' effect because all the other sources are updated causing the updateTarget to trigger every time. The same unwanted effect occurs when the target changes, as the sources update will then trigger again the updateTarget. To avoid this, and improve performance, two boolean flags are used to stop the listeners from proceeding with useless updates.

There's also another issue. In JavaFX when you bind the target multiple times (so you have multiple sources) the properties will have the value of the last used source. This helper though, stores the sources in a Map, which as you know it's not ordered. I could have used a LinkedHashMap, but I wanted to use a WeakHashMap to avoid memory leaks (I hope). Writing a WeakLinkedHashMap would have been too much work, and also very complicated, so in the end I created the BindingsMap, which allows to retrieve the sources in order of insertion.

The Map associates the source observable with the BiConsumer responsible for updating it.