Creating Clearer Method Names & Concepts

Richard Bair richard.bair at oracle.com
Fri Nov 2 14:24:41 PDT 2012


> Binding in JavaFX is certainly another source of confusion. You have "binding" and you have "Binding":
> 
> A Property<T> has a method "bind", of which the doc says: "Create a unidirection binding for this Property."
> 
> While there is also the interface Binding<T>, of which the doc says: "A Binding calculates a value that depends on one or more sources."

I guess I didn't see these as different, as the "bind" method uses a Binding (or just listens to it if it is of the right type):

    public void bind(final ObservableValue<? extends Number> rawObservable) {
        if (rawObservable == null) {
            throw new NullPointerException("Cannot bind to null");
        }

        ObservableIntegerValue newObservable;
        if (rawObservable instanceof ObservableIntegerValue) {
            newObservable = (ObservableIntegerValue)rawObservable;
        } else if (rawObservable instanceof ObservableNumberValue) {
            final ObservableNumberValue numberValue = (ObservableNumberValue)rawObservable;
            newObservable = new IntegerBinding() {
                {
                    super.bind(rawObservable);
                }

                @Override
                protected int computeValue() {
                    return numberValue.intValue();
                }
            };
        } else {
            newObservable = new IntegerBinding() {
                {
                    super.bind(rawObservable);
                }

                @Override
                protected int computeValue() {
                    final Number value = rawObservable.getValue();
                    return (value == null)? 0 : value.intValue();
                }
            };
        }

        if (!newObservable.equals(observable)) {
            unbind();
            observable = newObservable;
            if (listener == null) {
                listener = new Listener();
            }
            observable.addListener(listener);
            markInvalid();
        }
    }



So the "bind" method is just shorthand, really, for using Bindings on that property.


More information about the openjfx-dev mailing list