Skip to content

Instantly share code, notes, and snippets.

@christophe-dooapp
Last active December 18, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophe-dooapp/5822407 to your computer and use it in GitHub Desktop.
Save christophe-dooapp/5822407 to your computer and use it in GitHub Desktop.
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