Advanced-Bindings for JavaFX
javajavafxI'm a big fan of JavaFX's Properties and Data-Bindings. In my opinion, one of the most interesting part of the data binding capabilities of JavaFX is that you can create a binding that represents a calculation of values. For example you can create a binding that represents the addition of two integer properties:
import javafx.beans.binding.Bindings;
import static eu.lestard.assertj.javafx.api.Assertions.assertThat;
...
@Test
public void test(){
IntegerProperty a = new SimpleIntegerProperty(12);
IntegerProperty b = new SimpleIntegerProperty(30);
NumberBinding c = Bindings.add(a, b);
assertThat(c).hasValue(42);
a.set(2);
assertThat(c).hasValue(32);
}
There are binding method for basic operations like add, subtract, multiply, divide. But what if you need some more specific operations like exponentiation or sine?
To fill this gap I have created a library called "advanced-bindings" that will become a collection of custom bindings that are needed to create application logic with data bindings. The first step was to create binding implementations for all methods of java.lang.Math. This way you can do stuff like this:
@Test
public void testPow(){
DoubleProperty a = new SimpleDoubleProperty(3);
DoubleProperty b = new SimpleDoubleProperty(2);
final DoubleBinding pow = MathBindings.pow(a, b);
// 3^2 = 9
assertThat(pow).hasValue(9.0);
a.set(5);
b.set(3);
// 5^3 = 125
assertThat(pow).hasValue(125.0);
}
In the future I will add more useful helpers and custom bindings. If you have ideas feel free to add an issue at github.
I have uploaded the library to maven-central so you can use it with Gradle:
compile 'eu.lestard:advanced-bindings:0.1.0'
or with Maven:
<dependency>
<groupId>eu.lestard</groupId>
<artifactId>advanced-bindings</artifactId>
<version>0.1.0</version>
</dependency>