Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.dooapp.FXBinding.model;
import javafx.beans.binding.DoubleBinding;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
/**
* A simple demo class
* Created at 20/06/13 14:44.<br>
*
* @author Christophe DUFOUR
*/
public class Foo {
/**
* A list of double values
*/
private ObservableList<Double> values = FXCollections.observableArrayList(0.3d, 0.1d, 0.2d);
/**
* A list of double values
*/
public ObservableList<Double> values() {
return values;
}
/**
* the average value of all double in values
*
* @see #values()
*/
private DoubleBinding averageBinding = new DoubleBinding() {
{
//call #computeValue every time #values() changes
super.bind(values());
}
@Override
protected double computeValue() {
double result = 0;
if (values().isEmpty()) {
return result;
}
for (Double d : values()) {
result += d;
}
return result / values().size();
}
};
/**
* the average value of all double in values
*
* @see #values()
*/
public DoubleBinding averageBinding() {
return averageBinding;
}
/**
* Just a main to show a demo
*
* @param args args
*/
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo.averageBinding().get()); //Should print 0.2
foo.values().add(-3d);
System.out.println(foo.averageBinding().get()); //Should print -0.6
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment