/* * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javafx.beans.binding; import javafx.beans.value.ObservableFloatValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.beans.value.ObservableValue; /** * {@code FloatExpression} is an * {@link javafx.beans.value.ObservableFloatValue} plus additional convenience * methods to generate bindings in a fluent style. *
* A concrete sub-class of {@code FloatExpression} has to implement the method
* {@link javafx.beans.value.ObservableFloatValue#get()}, which provides the
* actual value of this expression.
* @since JavaFX 2.0
*/
public abstract class FloatExpression extends NumberExpressionBase implements
ObservableFloatValue {
@Override
public int intValue() {
return (int) get();
}
@Override
public long longValue() {
return (long) get();
}
@Override
public float floatValue() {
return get();
}
@Override
public double doubleValue() {
return (double) get();
}
@Override
public Float getValue() {
return get();
}
/**
* Returns a {@code FloatExpression} that wraps a
* {@link javafx.beans.value.ObservableFloatValue}. If the
* {@code ObservableFloatValue} is already a {@code FloatExpression}, it
* will be returned. Otherwise a new
* {@link javafx.beans.binding.FloatBinding} is created that is bound to the
* {@code ObservableFloatValue}.
*
* @param value
* The source {@code ObservableFloatValue}
* @return A {@code FloatExpression} that wraps the
* {@code ObservableFloatValue} if necessary
* @throws NullPointerException
* if {@code value} is {@code null}
*/
public static FloatExpression floatExpression(
final ObservableFloatValue value) {
if (value == null) {
throw new NullPointerException("Value must be specified.");
}
return (value instanceof FloatExpression) ? (FloatExpression) value
: new FloatBinding() {
{
super.bind(value);
}
@Override
public void dispose() {
super.unbind(value);
}
@Override
protected float computeValue() {
return value.get();
}
@Override
public ObservableList
* Note: this method can be used to convert an {@link ObjectExpression} or
* {@link javafx.beans.property.ObjectProperty} of specific number type to FloatExpression, which
* is essentially an {@code ObservableValue
*
* Note: null values will be interpreted as 0f
*
* @param
* FloatProperty floatProperty = new SimpleFloatProperty(1.0f);
* ObjectProperty<Float> objectProperty = new SimpleObjectProperty<>(2.0f);
* BooleanBinding binding = floatProperty.greaterThan(FloatExpression.floatExpression(objectProperty));
*