]> source.dussan.org Git - vaadin-framework.git/commitdiff
Allow AbstractField to override value equality (#8201)
authorLeif Åstrand <legioth@gmail.com>
Tue, 10 Jan 2017 10:22:22 +0000 (12:22 +0200)
committerDenis <denis@vaadin.com>
Tue, 10 Jan 2017 10:22:22 +0000 (12:22 +0200)
* Allow AbstractField to override value equality

Fixes #8089

server/src/main/java/com/vaadin/ui/AbstractField.java
server/src/test/java/com/vaadin/ui/AbstractFieldTest.java

index f19e1fd8fc8af79c56607e4558facab954f6cef9..9f1a275ad03d8d21c727e692545c50bcc6645f67 100644 (file)
@@ -133,7 +133,7 @@ public abstract class AbstractField<T> extends AbstractComponent
         if (userOriginated && isReadOnly()) {
             return false;
         }
-        if (Objects.equals(value, getValue())) {
+        if (!isDifferentValue(value)) {
             return false;
         }
         doSetValue(value);
@@ -145,6 +145,24 @@ public abstract class AbstractField<T> extends AbstractComponent
         return true;
     }
 
+    /**
+     * Called when a new value is set to determine whether the provided new
+     * value is considered to be a change compared to the current value. This is
+     * used to determine whether {@link #doSetValue(Object)} should be called
+     * and a value change event fired.
+     *
+     * @param newValue
+     *            the new value candidate to check, may be <code>null</code>
+     *
+     * @return <code>true</code> if the provided value is considered to be
+     *         different and a value change event should be fired;
+     *         <code>false</code> if the values are considered to be the same
+     *         and no value change should be fired
+     */
+    protected boolean isDifferentValue(T newValue) {
+        return !Objects.equals(newValue, getValue());
+    }
+
     /**
      * Sets the value of this field. May do sanitization or throw
      * {@code IllegalArgumentException} if the value is invalid. Typically saves
index 7f66ca5ed3198744330db8aadce5d0a8656ca470..8d8d9a6499eecda737d54b54746338820ef52a75 100644 (file)
@@ -15,6 +15,14 @@ import com.vaadin.server.ClientConnector;
 
 public class AbstractFieldTest extends EasyMockSupport {
 
+    private final class IdentityTextField extends TextField {
+        @Override
+        protected boolean isDifferentValue(String newValue) {
+            // Checks for identity instead of equality
+            return newValue != getValue();
+        }
+    }
+
     class TextField extends AbstractField<String> {
 
         String value = "";
@@ -119,4 +127,53 @@ public class AbstractFieldTest extends EasyMockSupport {
         assertSame("event source connector", source, e.getSource());
         assertEquals("event from user", userOriginated, e.isUserOriginated());
     }
+
+    @Test
+    public void identityField_realChange() {
+        TextField identityField = new IdentityTextField();
+
+        identityField.addValueChangeListener(l);
+
+        // Expect event to both listeners for actual change
+        l.valueChange(EasyMock.capture(capture));
+
+        replayAll();
+
+        identityField.setValue("value");
+
+        verifyAll();
+    }
+
+    @Test
+    public void identityField_onlyIdentityChange() {
+        TextField identityField = new IdentityTextField();
+        identityField.setValue("value");
+
+        identityField.addValueChangeListener(l);
+
+        // Expect event to both listeners for actual change
+        l.valueChange(EasyMock.capture(capture));
+
+        replayAll();
+
+        String sameValueDifferentIdentity = new String("value");
+        identityField.setValue(sameValueDifferentIdentity);
+
+        verifyAll();
+    }
+
+    @Test
+    public void identityField_noChange() {
+        TextField identityField = new IdentityTextField();
+        identityField.setValue("value");
+
+        identityField.addValueChangeListener(l);
+
+        // Expect no event for identical value
+        replayAll();
+
+        identityField.setValue(identityField.getValue());
+
+        verifyAll();
+    }
 }