Calling set with the same value does not notify the KO of a property change
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 02 Sep 2013 18:06:56 +0200
changeset 28083f39c708849
parent 279 539d268a7fec
child 281 c57ad238468c
Calling set with the same value does not notify the KO of a property change
json/src/main/java/org/apidesign/html/json/impl/JSON.java
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
json/src/test/java/net/java/html/json/MapModelTest.java
json/src/test/java/net/java/html/json/ModelTest.java
     1.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Sun Sep 01 07:13:38 2013 +0000
     1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Mon Sep 02 18:06:56 2013 +0200
     1.3 @@ -109,6 +109,24 @@
     1.4          Object o = t.toModel(aClass, data);
     1.5          return aClass.cast(o);
     1.6      }
     1.7 +    
     1.8 +    public static boolean isSame(int a, int b) {
     1.9 +        return a == b;
    1.10 +    }
    1.11 +    
    1.12 +    public static boolean isSame(double a, double b) {
    1.13 +        return a == b;
    1.14 +    }
    1.15 +    
    1.16 +    public static boolean isSame(Object a, Object b) {
    1.17 +        if (a == b) {
    1.18 +            return true;
    1.19 +        }
    1.20 +        if (a == null || b == null) {
    1.21 +            return false;
    1.22 +        }
    1.23 +        return a.equals(b);
    1.24 +    }
    1.25  
    1.26      public static <T> T extractValue(Class<T> type, Object val) {
    1.27          if (Number.class.isAssignableFrom(type)) {
     2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Sun Sep 01 07:13:38 2013 +0000
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Mon Sep 02 18:06:56 2013 +0200
     2.3 @@ -479,8 +479,9 @@
     2.4                  w.write("  }\n");
     2.5                  w.write("  public void " + gs[1] + "(" + tn + " v) {\n");
     2.6                  w.write("    if (locked) throw new IllegalStateException();\n");
     2.7 +                w.write("    if (org.apidesign.html.json.impl.JSON.isSame(prop_" + p.name() + ", v)) return;\n");
     2.8                  w.write("    prop_" + p.name() + " = v;\n");
     2.9 -                w.write("    org.apidesign.html.json.impl.Bindings b = intKnckt();\n");
    2.10 +                w.write("    org.apidesign.html.json.impl.Bindings b = ko[0];\n");
    2.11                  w.write("    if (b != null) {\n");
    2.12                  w.write("      b.valueHasMutated(\"" + p.name() + "\");\n");
    2.13                  Collection<String> dependants = deps.get(p.name());
     3.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Sun Sep 01 07:13:38 2013 +0000
     3.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Mon Sep 02 18:06:56 2013 +0200
     3.3 @@ -52,7 +52,7 @@
     3.4      }
     3.5      
     3.6      @Test public void isThereABinding() throws Exception {
     3.7 -        Person p = Models.bind(new Person(), c);
     3.8 +        Person p = Models.bind(new Person(), c).applyBindings();
     3.9          p.setFirstName("Jarda");
    3.10          
    3.11          Map m = (Map)WrapperObject.find(p);
    3.12 @@ -71,6 +71,32 @@
    3.13          assertEquals(o.changes, 2, "Snd change");
    3.14      }
    3.15      
    3.16 +    
    3.17 +    @Test public void dontNotifySameProperty() throws Exception {
    3.18 +        Person p = Models.bind(new Person(), c);
    3.19 +        p.setFirstName("Jirka");
    3.20 +        
    3.21 +        Map m = (Map)WrapperObject.find(p);
    3.22 +        Object v = m.get("firstName");
    3.23 +        assertNotNull(v, "Value should be in the map");
    3.24 +        assertEquals(v.getClass(), One.class, "It is instance of One");
    3.25 +        One o = (One)v;
    3.26 +        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
    3.27 +        
    3.28 +        p.setFirstName(new String("Jirka"));
    3.29 +        assertEquals(o.changes, 0, "No change so far, the value is the same");
    3.30 +        
    3.31 +        p.setFirstName("Jarda");
    3.32 +        assertFalse(o.pb.isReadOnly(), "Mutable property");
    3.33 +        
    3.34 +        assertEquals(o.get(), "Jarda", "Value should be in the map");
    3.35 +        
    3.36 +        o.set("Karle");
    3.37 +        
    3.38 +        assertEquals(p.getFirstName(), "Karle", "Model value updated");
    3.39 +        assertEquals(o.changes, 2, "Snd change");
    3.40 +    }
    3.41 +    
    3.42      @Test public void derivedProperty() throws Exception {
    3.43          Person p = Models.bind(new Person(), c);
    3.44          
     4.1 --- a/json/src/test/java/net/java/html/json/ModelTest.java	Sun Sep 01 07:13:38 2013 +0000
     4.2 +++ b/json/src/test/java/net/java/html/json/ModelTest.java	Mon Sep 02 18:06:56 2013 +0200
     4.3 @@ -115,6 +115,7 @@
     4.4      }
     4.5  
     4.6      @Test public void derivedArrayProp() {
     4.7 +        model.applyBindings();
     4.8          model.setCount(10);
     4.9          
    4.10          List<String> arr = model.getRepeat();
    4.11 @@ -133,6 +134,8 @@
    4.12      }
    4.13      
    4.14      @Test public void derivedPropertiesAreNotified() {
    4.15 +        model.applyBindings();
    4.16 +        
    4.17          model.setValue(33);
    4.18          
    4.19          // not interested in change of this property