/* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package javafx.beans.binding; import javafx.beans.value.ObservableDoubleValue; import javafx.beans.value.ObservableNumberValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import com.sun.javafx.collections.annotations.ReturnsUnmodifiableCollection; import javafx.beans.value.ObservableValue; /** * A {@code DoubleExpression} is a * {@link javafx.beans.value.ObservableDoubleValue} plus additional convenience * methods to generate bindings in a fluent style. *
* A concrete sub-class of {@code DoubleExpression} has to implement the method
* {@link javafx.beans.value.ObservableDoubleValue#get()}, which provides the
* actual value of this expression.
* @since JavaFX 2.0
*/
public abstract class DoubleExpression extends NumberExpressionBase implements
ObservableDoubleValue {
@Override
public int intValue() {
return (int) get();
}
@Override
public long longValue() {
return (long) get();
}
@Override
public float floatValue() {
return (float) get();
}
@Override
public double doubleValue() {
return get();
}
@Override
public Double getValue() {
return get();
}
/**
* Returns a {@code DoubleExpression} that wraps a
* {@link javafx.beans.value.ObservableDoubleValue}. If the
* {@code ObservableDoubleValue} is already a {@code DoubleExpression}, it
* will be returned. Otherwise a new
* {@link javafx.beans.binding.DoubleBinding} is created that is bound to
* the {@code ObservableDoubleValue}.
*
* @param value
* The source {@code ObservableDoubleValue}
* @return A {@code DoubleExpression} that wraps the
* {@code ObservableDoubleValue} if necessary
* @throws NullPointerException
* if {@code value} is {@code null}
*/
public static DoubleExpression doubleExpression(
final ObservableDoubleValue value) {
if (value == null) {
throw new NullPointerException("Value must be specified.");
}
return (value instanceof DoubleExpression) ? (DoubleExpression) value
: new DoubleBinding() {
{
super.bind(value);
}
@Override
public void dispose() {
super.unbind(value);
}
@Override
protected double computeValue() {
return value.get();
}
@Override
@ReturnsUnmodifiableCollection
public ObservableList
* Note: this method can be used to convert an {@link ObjectExpression} or
* {@link javafx.beans.property.ObjectProperty} of specific number type to DoubleExpression, which
* is essentially an {@code ObservableValue
*
* Note: null values will be interpreted as 0.0
*
* @param value
* The source {@code ObservableValue}
* @return A {@code DoubleExpression} that wraps the
* {@code ObservableValue} if necessary
* @throws NullPointerException
* if {@code value} is {@code null}
* @since JavaFX 8.0
*/
public static
* DoubleProperty doubleProperty = new SimpleDoubleProperty(1.0);
* ObjectProperty<Double> objectProperty = new SimpleObjectProperty<>(2.0);
* BooleanBinding binding = doubleProperty.greaterThan(DoubleExpression.doubleExpression(objectProperty));
*