/*
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package javafx.beans.property;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.WeakInvalidationListener;
import javafx.beans.binding.DoubleExpression;
/**
* Super class for all readonly properties wrapping a {@code double}.
*
* @see javafx.beans.value.ObservableDoubleValue
* @see javafx.beans.binding.DoubleExpression
* @see ReadOnlyProperty
*
* @since JavaFX 2.0
*/
public abstract class ReadOnlyDoubleProperty extends DoubleExpression implements
ReadOnlyProperty<Number> {
/**
* The constructor of {@code ReadOnlyDoubleProperty}.
*/
public ReadOnlyDoubleProperty() {
}
/**
* Returns a string representation of this {@code ReadOnlyDoubleProperty} object.
* @return a string representation of this {@code ReadOnlyDoubleProperty} object.
*/
@Override
public StringtoString() {
final Objectbean = getBean();
final Stringname = getName();
final StringBuilderresult = new StringBuilder(
"ReadOnlyDoubleProperty [");
if (bean != null) {
result.append("bean: ").append(bean).append(", ");
}
if ((name != null) && !name.equals("")) {
result.append("name: ").append(name).append(", ");
}
result.append("value: ").append(get()).append("]");
return result.toString();
}
/**
* Returns a {@code ReadOnlyDoubleProperty} that wraps a
* {@link javafx.beans.property.ReadOnlyProperty}. If the
* {@code ReadOnlyProperty} is already a {@code ReadOnlyDoubleProperty}, it
* will be returned. Otherwise a new
* {@code ReadOnlyDoubleProperty} is created that is bound to
* the {@code ReadOnlyProperty}.
*
* Note: null values will be interpreted as 0.0
*
* @param property
* The source {@code ReadOnlyProperty}
* @return A {@code ReadOnlyDoubleProperty} that wraps the
* {@code ReadOnlyProperty} if necessary
* @throws NullPointerException
* if {@code property} is {@code null}
* @since JavaFX 8.0
*/
public static <T extends Number> ReadOnlyDoublePropertyreadOnlyDoubleProperty(final ReadOnlyProperty<T> property) {
if (property == null) {
throw new NullPointerException("Property cannot be null");
}
return property instanceof ReadOnlyDoubleProperty ? (ReadOnlyDoubleProperty) property:
new ReadOnlyDoublePropertyBase() {
private boolean valid = true;
private final InvalidationListenerlistener = observable -> {
if (valid) {
valid = false;
fireValueChangedEvent();
}
};
{
property.addListener(new WeakInvalidationListener(listener));
}
@Override
public double get() {
valid = true;
final T value = property.getValue();
return value == null ? 0.0 : value.doubleValue();
}
@Override
public ObjectgetBean() {
return null; // Virtual property, no bean
}
@Override
public StringgetName() {
return property.getName();
}
};
}
/**
* Creates a {@link javafx.beans.property.ReadOnlyObjectProperty} that holds the value
* of this {@code ReadOnlyDoubleProperty}. If the
* value of this {@code ReadOnlyDoubleProperty} changes, the value of the
* {@code ReadOnlyObjectProperty} will be updated automatically.
*
* @return the new {@code ReadOnlyObjectProperty}
* @since JavaFX 8.0
*/
@Override
public ReadOnlyObjectProperty<Double> asObject() {
return new ReadOnlyObjectPropertyBase<Double>() {
private boolean valid = true;
private final InvalidationListenerlistener = observable -> {
if (valid) {
valid = false;
fireValueChangedEvent();
}
};
{
ReadOnlyDoubleProperty.this.addListener(new WeakInvalidationListener(listener));
}
@Override
public ObjectgetBean() {
return null; // Virtual property, does not exist on a bean
}
@Override
public StringgetName() {
return ReadOnlyDoubleProperty.this.getName();
}
@Override
public Doubleget() {
valid = true;
return ReadOnlyDoubleProperty.this.getValue();
}
};
};
}