Purpose of specialized BidirectionalBinding classes for primitive property types
Michael Strauß
michaelstrau2 at gmail.com
Mon Sep 28 23:25:44 UTC 2020
The com.sun.javafx.binding.BidirectionalBinding class contains specialized
implementations for primitive types:
1. BidirectionalDoubleBinding
2. BidirectionalFloatBinding
3. BidirectionalIntegerBinding
4. BidirectionalLongBinding
5. BidirectionalBooleanBinding
These private implementation classes are selected by the static
BidirectionalBinding.bind(Property<T>, Property<T>) method iff the runtime
type of both provided property objects corresponds to the respective
primitive property implementation (DoubleProperty, FloatProperty, etc.).
In all other cases, the generic TypedGenericBidirectionalBinding
implementation is selected.
The only meaningful difference between the specialized classes and
TypedGenericBidirectionalBinding seems to be an attempt to avoid boxing
conversions by replacing the generic
property1.setValue(newValue)
with variations of the following code:
property1.set(newValue.doubleValue())
However, if I'm not mistaken, this doesn't prevent any boxing conversion,
thus rendering all of the specialized binding classes useless:
1. All of these classes implement the ChangeListener interface, which
provides the changed(ObservableValue<T>, T oldValue, T newValue) method.
2. Calling this method will box both 'oldValue' and 'newValue' parameters
if the values come from a primitive property type like DoubleProperty,
FloatProperty, etc.
3. Since both parameters are already boxed, the attempt to use
'set(newValue.doubleValue())' yields no advantage compared to just calling
'setValue(newValue)'.
If the purpose was indeed to prevent boxing conversions, these classes
would need to implement InvalidationListener, not ChangeListener.
More information about the openjfx-dev
mailing list