Clone needs to be deep
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 02 Sep 2013 19:58:24 +0200
changeset 282a81e79341064
parent 281 c57ad238468c
child 283 e90e281a06fc
Clone needs to be deep
json/src/main/java/org/apidesign/html/json/impl/JSONList.java
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
json/src/test/java/net/java/html/json/BoardTest.java
     1.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSONList.java	Mon Sep 02 18:35:53 2013 +0200
     1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSONList.java	Mon Sep 02 19:58:24 2013 +0200
     1.3 @@ -24,6 +24,7 @@
     1.4  import java.util.Arrays;
     1.5  import java.util.Collection;
     1.6  import java.util.Iterator;
     1.7 +import net.java.html.BrwsrCtx;
     1.8  
     1.9  /**
    1.10   *
    1.11 @@ -160,12 +161,24 @@
    1.12              }
    1.13          }
    1.14      }
    1.15 +    
    1.16 +    public void cloneAll(BrwsrCtx c, Collection<T> other) {
    1.17 +        Boolean isModel = null;
    1.18 +        for (T t : other) {
    1.19 +            if (isModel == null) {
    1.20 +                isModel = JSON.isModel(t.getClass());
    1.21 +            }
    1.22 +            if (isModel) {
    1.23 +                add(JSON.bindTo(t, c));
    1.24 +            } else {
    1.25 +                add(t);
    1.26 +            }
    1.27 +        }
    1.28 +    }
    1.29  
    1.30      @Override
    1.31      public JSONList clone() {
    1.32 -        JSONList ko = (JSONList) super.clone();
    1.33 -        ko.model = null;
    1.34 -        return ko;
    1.35 +        throw new UnsupportedOperationException();
    1.36      }
    1.37  
    1.38      static final Object koData(Collection<?> c, Bindings m) {
     2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Mon Sep 02 18:35:53 2013 +0200
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Mon Sep 02 19:58:24 2013 +0200
     2.3 @@ -1346,7 +1346,7 @@
     2.4                  }
     2.5                  w.write("    ret.prop_" + p.name() + " =  prop_" + p.name() + "  == null ? null : prop_" + p.name() + ".clone();\n");
     2.6              } else {
     2.7 -                w.write("    ret.prop_" + p.name() + ".addAll(prop_" + p.name() + ");\n");
     2.8 +                w.write("    ret.prop_" + p.name() + ".cloneAll(ctx, prop_" + p.name() + ");\n");
     2.9              }
    2.10          }
    2.11          
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/json/src/test/java/net/java/html/json/BoardTest.java	Mon Sep 02 19:58:24 2013 +0200
     3.3 @@ -0,0 +1,61 @@
     3.4 +/**
     3.5 + * HTML via Java(tm) Language Bindings
     3.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details. apidesign.org
    3.16 + * designates this particular file as subject to the
    3.17 + * "Classpath" exception as provided by apidesign.org
    3.18 + * in the License file that accompanied this code.
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License
    3.21 + * along with this program. Look for COPYING file in the top folder.
    3.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    3.23 + */
    3.24 +package net.java.html.json;
    3.25 +
    3.26 +import static org.testng.Assert.assertFalse;
    3.27 +import static org.testng.Assert.assertTrue;
    3.28 +import org.testng.annotations.Test;
    3.29 +
    3.30 +/**
    3.31 + *
    3.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.33 + */
    3.34 +@Model(className = "Board", properties = {
    3.35 +    @Property(name = "rows", type = Row.class, array = true)
    3.36 +})
    3.37 +public class BoardTest {
    3.38 +    
    3.39 +    @Model(className = "Row", properties = {
    3.40 +        @Property(name = "column", type = Column.class, array = true)
    3.41 +    })
    3.42 +    static class RowModel {
    3.43 +    }
    3.44 +    
    3.45 +    @Model(className = "Column", properties = {
    3.46 +        @Property(name = "black", type = boolean.class)
    3.47 +    })
    3.48 +    static class ColumnModel {
    3.49 +    }
    3.50 +
    3.51 +    @Test public void deepClone() {
    3.52 +        Board orig = new Board(new Row(new Column(true)));
    3.53 +        assertTrue(orig.getRows().get(0).getColumn().get(0).isBlack(), "Really true");
    3.54 +        
    3.55 +        Board clone = orig.clone();
    3.56 +        assertTrue(clone.getRows().get(0).getColumn().get(0).isBlack(), "Clone also true");
    3.57 +        
    3.58 +        clone.getRows().get(0).getColumn().get(0).setBlack(false);
    3.59 +        
    3.60 +        assertFalse(clone.getRows().get(0).getColumn().get(0).isBlack(), "Clone also is not false");
    3.61 +        assertTrue(orig.getRows().get(0).getColumn().get(0).isBlack(), "Orig still true");
    3.62 +    }
    3.63 +    
    3.64 +}