]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add methods to remove Bindings from Binder (#9932)
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Tue, 5 Sep 2017 13:29:14 +0000 (16:29 +0300)
committerHenri Sara <henri.sara@gmail.com>
Tue, 5 Sep 2017 13:29:14 +0000 (16:29 +0300)
server/src/main/java/com/vaadin/data/Binder.java
server/src/test/java/com/vaadin/data/BinderTest.java

index 64f389cc91c266133a0e81bbff54a32692a83ae1..f76cdcda2c5233b51fcb31d79e223072b477e4bd 100644 (file)
@@ -2387,4 +2387,50 @@ public class Binder<BEAN> implements Serializable {
     public Stream<HasValue<?>> getFields() {
         return bindings.stream().map(Binding::getField);
     }
+
+    /**
+     * Finds and removes all Bindings for the given field.
+     * 
+     * @param field
+     *            the field to remove from bindings
+     * 
+     * @since 8.2
+     */
+    public void removeBinding(HasValue<?> field) {
+        Objects.requireNonNull(field, "Field can not be null");
+        Set<BindingImpl<BEAN, ?, ?>> toRemove = bindings.stream()
+                .filter(binding -> field.equals(binding.getField()))
+                .collect(Collectors.toSet());
+        toRemove.forEach(this::removeBinding);
+    }
+
+    /**
+     * Removes the given Binding from this Binder.
+     * 
+     * @param binding
+     *            the binding to remove
+     * 
+     * @since 8.2
+     */
+    public void removeBinding(Binding<BEAN, ?> binding) {
+        Objects.requireNonNull(binding, "Binding can not be null");
+        if (bindings.remove(binding)) {
+            boundProperties.entrySet()
+                    .removeIf(entry -> entry.getValue().equals(binding));
+        }
+    }
+
+    /**
+     * Finds and removes the Binding for the given property name.
+     * 
+     * @param propertyName
+     *            the propertyName to remove from bindings
+     * 
+     * @since 8.2
+     */
+    public void removeBinding(String propertyName) {
+        Objects.requireNonNull(propertyName, "Property name can not be null");
+        Optional.ofNullable(boundProperties.get(propertyName))
+                .ifPresent(this::removeBinding);
+    }
 }
index 2a90eb6c4b41de0213e3e15e594bded9310c05fe..a3d4f6adffd568a08bf3241b647ac28e05760353 100644 (file)
@@ -16,6 +16,7 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.vaadin.data.Binder.Binding;
 import com.vaadin.data.Binder.BindingBuilder;
 import com.vaadin.data.converter.StringToDoubleConverter;
 import com.vaadin.data.converter.StringToIntegerConverter;
@@ -682,4 +683,74 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
         forField.withConverter(new StringToDoubleConverter("Failed"));
         forField.bind(Person::getFirstName, Person::setFirstName);
     }
+
+    @Test
+    public void remove_field_binding() {
+        binder.forField(ageField)
+                .withConverter(new StringToIntegerConverter("Can't convert"))
+                .bind(Person::getAge, Person::setAge);
+
+        // Test that the binding does work
+        Assert.assertTrue("Field not initially empty", ageField.isEmpty());
+        binder.setBean(item);
+        Assert.assertEquals("Binding did not work",
+                String.valueOf(item.getAge()), ageField.getValue());
+        binder.setBean(null);
+        Assert.assertTrue("Field not cleared", ageField.isEmpty());
+
+        // Remove the binding
+        binder.removeBinding(ageField);
+
+        // Test that it does not work anymore
+        binder.setBean(item);
+        Assert.assertNotEquals("Binding was not removed",
+                String.valueOf(item.getAge()), ageField.getValue());
+    }
+
+    @Test
+    public void remove_propertyname_binding() {
+        // Use a bean aware binder
+        Binder<Person> binder = new Binder<>(Person.class);
+
+        binder.bind(nameField, "firstName");
+
+        // Test that the binding does work
+        Assert.assertTrue("Field not initially empty", nameField.isEmpty());
+        binder.setBean(item);
+        Assert.assertEquals("Binding did not work", item.getFirstName(),
+                nameField.getValue());
+        binder.setBean(null);
+        Assert.assertTrue("Field not cleared", nameField.isEmpty());
+
+        // Remove the binding
+        binder.removeBinding("firstName");
+
+        // Test that it does not work anymore
+        binder.setBean(item);
+        Assert.assertNotEquals("Binding was not removed", item.getFirstName(),
+                nameField.getValue());
+    }
+
+    @Test
+    public void remove_binding() {
+        Binding<Person, Integer> binding = binder.forField(ageField)
+                .withConverter(new StringToIntegerConverter("Can't convert"))
+                .bind(Person::getAge, Person::setAge);
+
+        // Test that the binding does work
+        Assert.assertTrue("Field not initially empty", ageField.isEmpty());
+        binder.setBean(item);
+        Assert.assertEquals("Binding did not work",
+                String.valueOf(item.getAge()), ageField.getValue());
+        binder.setBean(null);
+        Assert.assertTrue("Field not cleared", ageField.isEmpty());
+
+        // Remove the binding
+        binder.removeBinding(binding);
+
+        // Test that it does not work anymore
+        binder.setBean(item);
+        Assert.assertNotEquals("Binding was not removed",
+                String.valueOf(item.getAge()), ageField.getValue());
+    }
 }