Last active
December 18, 2015 19:00
-
-
Save christophe-dooapp/5830193 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private DoubleBinding averageBinding = new FXDoubleBinding() { | |
/** | |
* This method is called every time the binding is becoming invalid. | |
*/ | |
@Override | |
protected void configure() { | |
addObservable(values()); | |
for (XYChart.Data<Number, Number> d : values()) { | |
addObservable(d.XValueProperty()); | |
} | |
} | |
@Override | |
protected double compute() { | |
double result = 0; | |
if (values().isEmpty()) { | |
return result; | |
} | |
for (XYChart.Data<Number, Number> d : values()) { | |
result += d.getXValue().doubleValue(); | |
} | |
return result / values().size(); | |
} | |
}; | |
//... | |
public static void main(String[] args) { | |
Foo foo = new Foo(); | |
System.out.println(foo.averageBinding().get()); //Should print 0.2 | |
foo.values().add(new XYChart.Data<Number, Number>(-3d, -3d)); | |
System.out.println(foo.averageBinding().get()); //Should print -0.6 | |
foo.values().get(0).setXValue(30d); | |
System.out.println(foo.averageBinding().get()); //Should print 6.825 | |
foo.values.get(3).setXValue(10d); | |
System.out.println(foo.averageBinding().get()); //Should print 10.075 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment