One class less: No need for a separate WrapperObject class osgi
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Mon, 06 Jan 2014 08:55:10 +0100
branchosgi
changeset 414bc7cb0bbf0b3
parent 413 9ce2f6c147a4
child 415 ead37ae4d202
One class less: No need for a separate WrapperObject class
json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java
json/src/main/java/org/netbeans/html/json/impl/JSON.java
json/src/main/java/org/netbeans/html/json/impl/JSONList.java
json/src/main/java/org/netbeans/html/json/impl/WrapperObject.java
json/src/test/java/net/java/html/json/MapModelTest.java
json/src/test/java/net/java/html/json/TypesTest.java
json/src/test/java/org/netbeans/html/json/impl/JSONListTest.java
     1.1 --- a/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Mon Jan 06 08:49:02 2014 +0100
     1.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Mon Jan 06 08:55:10 2014 +0100
     1.3 @@ -44,10 +44,10 @@
     1.4  
     1.5  import net.java.html.BrwsrCtx;
     1.6  import org.netbeans.html.json.impl.Bindings;
     1.7 +import org.netbeans.html.json.impl.JSON;
     1.8  import org.netbeans.html.json.impl.PropertyBindingAccessor;
     1.9  import org.netbeans.html.json.impl.PropertyBindingAccessor.PBData;
    1.10  import org.netbeans.html.json.impl.RcvrJSON;
    1.11 -import org.netbeans.html.json.impl.WrapperObject;
    1.12  
    1.13  /** Describes a property when one is asked to 
    1.14   * bind it 
    1.15 @@ -120,7 +120,7 @@
    1.16      
    1.17      public Object getValue() {
    1.18          Object v = data.getValue();
    1.19 -        Object r = WrapperObject.find(v, data.getBindings());
    1.20 +        Object r = JSON.find(v, data.getBindings());
    1.21          return r == null ? v : r;
    1.22      }
    1.23      
     2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Mon Jan 06 08:49:02 2014 +0100
     2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Mon Jan 06 08:55:10 2014 +0100
     2.3 @@ -44,6 +44,7 @@
     2.4  
     2.5  import java.io.IOException;
     2.6  import java.io.InputStream;
     2.7 +import java.util.Collection;
     2.8  import java.util.HashMap;
     2.9  import java.util.Map;
    2.10  import net.java.html.BrwsrCtx;
    2.11 @@ -258,6 +259,32 @@
    2.12          return Boolean.TRUE.equals(val);
    2.13      }
    2.14      
    2.15 +    public static Object find(Object object, Bindings model) {
    2.16 +        if (object == null) {
    2.17 +            return null;
    2.18 +        }
    2.19 +        if (object instanceof JSONList) {
    2.20 +            return ((JSONList<?>) object).koData();
    2.21 +        }
    2.22 +        if (object instanceof Collection) {
    2.23 +            return JSONList.koData((Collection<?>) object, model);
    2.24 +        }
    2.25 +        Proto.Type<?> type = JSON.findType(object.getClass());
    2.26 +        if (type == null) {
    2.27 +            return null;
    2.28 +        }
    2.29 +        final Proto proto = PropertyBindingAccessor.protoFor(type, object);
    2.30 +        if (proto == null) {
    2.31 +            return null;
    2.32 +        }
    2.33 +        final Bindings b = PropertyBindingAccessor.getBindings(proto, true);
    2.34 +        return b == null ? null : b.koData();
    2.35 +    }
    2.36 +
    2.37 +    public static Object find(Object object) {
    2.38 +        return find(object, null);
    2.39 +    }
    2.40 +    
    2.41      public static void loadJSON(
    2.42          BrwsrCtx c, RcvrJSON callback,
    2.43          String urlBefore, String urlAfter, String method,
     3.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSONList.java	Mon Jan 06 08:49:02 2014 +0100
     3.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSONList.java	Mon Jan 06 08:55:10 2014 +0100
     3.3 @@ -194,7 +194,7 @@
     3.4      static final Object koData(Collection<?> c, Bindings m) {
     3.5          Object[] arr = c.toArray(new Object[c.size()]);
     3.6          for (int i = 0; i < arr.length; i++) {
     3.7 -            Object r = WrapperObject.find(arr[i], m);
     3.8 +            Object r = JSON.find(arr[i], m);
     3.9              if (r != null) {
    3.10                  arr[i] = r;
    3.11              }
     4.1 --- a/json/src/main/java/org/netbeans/html/json/impl/WrapperObject.java	Mon Jan 06 08:49:02 2014 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,83 +0,0 @@
     4.4 -/**
     4.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 - *
     4.7 - * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
     4.8 - *
     4.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    4.10 - * Other names may be trademarks of their respective owners.
    4.11 - *
    4.12 - * The contents of this file are subject to the terms of either the GNU
    4.13 - * General Public License Version 2 only ("GPL") or the Common
    4.14 - * Development and Distribution License("CDDL") (collectively, the
    4.15 - * "License"). You may not use this file except in compliance with the
    4.16 - * License. You can obtain a copy of the License at
    4.17 - * http://www.netbeans.org/cddl-gplv2.html
    4.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.19 - * specific language governing permissions and limitations under the
    4.20 - * License.  When distributing the software, include this License Header
    4.21 - * Notice in each file and include the License file at
    4.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    4.23 - * particular file as subject to the "Classpath" exception as provided
    4.24 - * by Oracle in the GPL Version 2 section of the License file that
    4.25 - * accompanied this code. If applicable, add the following below the
    4.26 - * License Header, with the fields enclosed by brackets [] replaced by
    4.27 - * your own identifying information:
    4.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    4.29 - *
    4.30 - * Contributor(s):
    4.31 - *
    4.32 - * The Original Software is NetBeans. The Initial Developer of the Original
    4.33 - * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    4.34 - *
    4.35 - * If you wish your version of this file to be governed by only the CDDL
    4.36 - * or only the GPL Version 2, indicate your decision by adding
    4.37 - * "[Contributor] elects to include this software in this distribution
    4.38 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    4.39 - * single choice of license, a recipient has the option to distribute
    4.40 - * your version of this file under either the CDDL, the GPL Version 2 or
    4.41 - * to extend the choice of license to its licensees as provided above.
    4.42 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    4.43 - * Version 2 license, then the option applies only if the new code is
    4.44 - * made subject to such option by the copyright holder.
    4.45 - */
    4.46 -package org.netbeans.html.json.impl;
    4.47 -
    4.48 -import java.util.Collection;
    4.49 -import org.apidesign.html.json.spi.Proto;
    4.50 -
    4.51 -/** A way to extract real object from a model classes.
    4.52 - *
    4.53 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.54 - */
    4.55 -public final class WrapperObject {
    4.56 -    private WrapperObject() {
    4.57 -    }
    4.58 -    
    4.59 -    public static Object find(Object object) {
    4.60 -        return find(object, null);
    4.61 -    }
    4.62 -    
    4.63 -    public static Object find(Object object, Bindings model) {
    4.64 -        if (object == null) {
    4.65 -            return null;
    4.66 -        }
    4.67 -        
    4.68 -        if (object instanceof JSONList) {
    4.69 -            return ((JSONList<?>)object).koData();
    4.70 -        }
    4.71 -        if (object instanceof Collection) {
    4.72 -            return JSONList.koData((Collection<?>)object, model);
    4.73 -        }
    4.74 -
    4.75 -        Proto.Type<?> type = JSON.findType(object.getClass());
    4.76 -        if (type == null) {
    4.77 -            return null;
    4.78 -        }
    4.79 -        final Proto proto = PropertyBindingAccessor.protoFor(type, object);
    4.80 -        if (proto == null) {
    4.81 -            return null;
    4.82 -        }
    4.83 -        final Bindings b = PropertyBindingAccessor.getBindings(proto, true);
    4.84 -        return b == null ? null : b.koData();
    4.85 -    }
    4.86 -}
     5.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Mon Jan 06 08:49:02 2014 +0100
     5.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Mon Jan 06 08:55:10 2014 +0100
     5.3 @@ -49,12 +49,12 @@
     5.4  import java.util.HashMap;
     5.5  import java.util.Map;
     5.6  import org.apidesign.html.context.spi.Contexts;
     5.7 -import org.netbeans.html.json.impl.WrapperObject;
     5.8  import org.apidesign.html.json.spi.FunctionBinding;
     5.9  import org.apidesign.html.json.spi.JSONCall;
    5.10  import org.apidesign.html.json.spi.PropertyBinding;
    5.11  import org.apidesign.html.json.spi.Technology;
    5.12  import org.apidesign.html.json.spi.Transfer;
    5.13 +import org.netbeans.html.json.impl.JSON;
    5.14  import org.testng.annotations.BeforeMethod;
    5.15  import org.testng.annotations.Test;
    5.16  import static org.testng.Assert.*;
    5.17 @@ -77,7 +77,7 @@
    5.18          Person p = Models.bind(new Person(), c).applyBindings();
    5.19          p.setFirstName("Jarda");
    5.20          
    5.21 -        Map m = (Map)WrapperObject.find(p);
    5.22 +        Map m = (Map)JSON.find(p);
    5.23          Object v = m.get("firstName");
    5.24          assertNotNull(v, "Value should be in the map");
    5.25          assertEquals(v.getClass(), One.class, "It is instance of One");
    5.26 @@ -98,7 +98,7 @@
    5.27          Person p = Models.bind(new Person(), c);
    5.28          p.setFirstName("Jirka");
    5.29          
    5.30 -        Map m = (Map)WrapperObject.find(p);
    5.31 +        Map m = (Map)JSON.find(p);
    5.32          Object v = m.get("firstName");
    5.33          assertNotNull(v, "Value should be in the map");
    5.34          assertEquals(v.getClass(), One.class, "It is instance of One");
    5.35 @@ -122,7 +122,7 @@
    5.36      @Test public void derivedProperty() throws Exception {
    5.37          Person p = Models.bind(new Person(), c);
    5.38          
    5.39 -        Map m = (Map)WrapperObject.find(p);
    5.40 +        Map m = (Map)JSON.find(p);
    5.41          Object v = m.get("fullName");
    5.42          assertNotNull(v, "Value should be in the map");
    5.43          assertEquals(v.getClass(), One.class, "It is instance of One");
    5.44 @@ -135,7 +135,7 @@
    5.45          p.setFirstName("Trans");
    5.46          p.setSex(Sex.MALE);
    5.47          
    5.48 -        Map m = (Map)WrapperObject.find(p);
    5.49 +        Map m = (Map)JSON.find(p);
    5.50          Object o = m.get("changeSex");
    5.51          assertNotNull(o, "Function registered in the model");
    5.52          assertEquals(o.getClass(), One.class);
    5.53 @@ -152,7 +152,7 @@
    5.54          Person p = Models.bind(new Person(), c);
    5.55          p.setFirstName("Trans");
    5.56          
    5.57 -        Map m = (Map)WrapperObject.find(p);
    5.58 +        Map m = (Map)JSON.find(p);
    5.59          Object o = m.get("changeSex");
    5.60          assertNotNull(o, "Function registered in the model");
    5.61          assertEquals(o.getClass(), One.class);
     6.1 --- a/json/src/test/java/net/java/html/json/TypesTest.java	Mon Jan 06 08:49:02 2014 +0100
     6.2 +++ b/json/src/test/java/net/java/html/json/TypesTest.java	Mon Jan 06 08:55:10 2014 +0100
     6.3 @@ -46,9 +46,9 @@
     6.4  import java.util.Map;
     6.5  import net.java.html.json.MapModelTest.One;
     6.6  import org.apidesign.html.context.spi.Contexts;
     6.7 -import org.netbeans.html.json.impl.WrapperObject;
     6.8  import org.apidesign.html.json.spi.Technology;
     6.9  import org.apidesign.html.json.spi.Transfer;
    6.10 +import org.netbeans.html.json.impl.JSON;
    6.11  import org.testng.annotations.BeforeMethod;
    6.12  import org.testng.annotations.Test;
    6.13  import static org.testng.Assert.*;
    6.14 @@ -111,10 +111,10 @@
    6.15          t.setFloatX(99f);
    6.16          */
    6.17          
    6.18 -        Object json = WrapperObject.find(t);
    6.19 +        Object json = JSON.find(t);
    6.20          
    6.21          Types copy = Models.bind(new Types(), c);
    6.22 -        Map copyMap = (Map) WrapperObject.find(copy);
    6.23 +        Map copyMap = (Map) JSON.find(copy);
    6.24          One o = (One) copyMap.get("readFromEvent");
    6.25          o.fb.call(null, json);
    6.26          
     7.1 --- a/json/src/test/java/org/netbeans/html/json/impl/JSONListTest.java	Mon Jan 06 08:49:02 2014 +0100
     7.2 +++ b/json/src/test/java/org/netbeans/html/json/impl/JSONListTest.java	Mon Jan 06 08:55:10 2014 +0100
     7.3 @@ -80,7 +80,7 @@
     7.4          p.setLastName("2");
     7.5          p.setSex(Sex.MALE);
     7.6  
     7.7 -        Object real = WrapperObject.find(p);
     7.8 +        Object real = JSON.find(p);
     7.9          assertEquals(this, real, "I am the right model");
    7.10      }
    7.11      
    7.12 @@ -135,7 +135,7 @@
    7.13          People people = Models.bind(new People(), c).applyBindings();
    7.14          people.getInfo().add(p);
    7.15          
    7.16 -        Object real = WrapperObject.find(people.getInfo());
    7.17 +        Object real = JSON.find(people.getInfo());
    7.18          assertEquals(real, this, "I am the model of the array");
    7.19      }
    7.20