BindingsManager { public <T> void bind(@NotNull SettableValue<T> dest, @NotNull ObservableValue<T> src) { bind(dest, src, ObservableBool.TRUE); } BindingsManager();  BindingsManager(@NotNull BatchInvoker.Strategy invokeStrategy); void bind(@NotNull SettableValue<T> dest, @NotNull ObservableValue<T> src); void bind(@NotNull SettableValue<T> dest, @NotNull ObservableValue<T> src, @NotNull ObservableValue<Boolean> enabled); void bindTwoWay(@NotNull SettableValue<T> first, @NotNull SettableValue<T> second); void release(@NotNull SettableValue<?> dest); void releaseTwoWay(@NotNull SettableValue<T> first, @NotNull SettableValue<T> second); void releaseTwoWay(@NotNull SettableValue<T> value); void releaseAll();  }
@Test public void oneWayBindingAffectedByTarget() throws Exception { BindingsManager bindings = new BindingsManager(BatchInvoker.INVOKE_IMMEDIATELY_STRATEGY); IntValueProperty property1 = new IntValueProperty(10); IntValueProperty property2 = new IntValueProperty(20); bindings.bind(property1, property2); assertThat(property1.get()).isEqualTo(20); property1.set(30); assertThat(property1.get()).isEqualTo(30); assertThat(property2.get()).isEqualTo(20); property2.set(40); assertThat(property1.get()).isEqualTo(40); }
@Test public void mapBindingsUpdateDestinationList() throws Exception { BindingsManager bindings = new BindingsManager(BatchInvoker.INVOKE_IMMEDIATELY_STRATEGY); ObservableList<Integer> numericList = new ObservableList<>(); for (int i = 1; i <= 5; i++) { numericList.add(i); } ObservableList<String> stringList = new ObservableList<>(); CountListener listener = new CountListener(); stringList.addListener(listener); bindings.bind(stringList, new MapExpression<Integer, String>(numericList) { @NotNull @Override protected String transform(@NotNull Integer srcElement) { return srcElement.toString(); } }); assertThat(stringList).containsExactly("1", "2", "3", "4", "5"); assertThat(listener.getCount()).isEqualTo(1); numericList.addAll(Arrays.asList(6, 7)); assertThat(stringList).containsExactly("1", "2", "3", "4", "5", "6", "7"); assertThat(listener.getCount()).isEqualTo(2); numericList.clear(); assertThat(stringList).isEmpty(); assertThat(listener.getCount()).isEqualTo(3); }
@Test public void oneWayBindingsCanBeEnabledConditionally() { BindingsManager bindings = new BindingsManager(BatchInvoker.INVOKE_IMMEDIATELY_STRATEGY); IntValueProperty srcProperty = new IntValueProperty(10); IntValueProperty destProperty = new IntValueProperty(-5); BoolValueProperty bindingEnabled = new BoolValueProperty(true); bindings.bind(destProperty, srcProperty, bindingEnabled); assertThat(destProperty.get()).isEqualTo(10); srcProperty.set(20); assertThat(destProperty.get()).isEqualTo(20); bindingEnabled.set(false); assertThat(destProperty.get()).isEqualTo(20); srcProperty.set(30); assertThat(destProperty.get()).isEqualTo(20); srcProperty.set(40); assertThat(destProperty.get()).isEqualTo(20); bindingEnabled.set(true); assertThat(destProperty.get()).isEqualTo(40); srcProperty.set(50); assertThat(destProperty.get()).isEqualTo(50); }