Every method should have only one meaning: making those that have to be implemented protected abstract. osgi
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Mon, 06 Jan 2014 08:37:00 +0100
branchosgi
changeset 4120e38b2918258
parent 411 1c1dbe362c59
child 413 9ce2f6c147a4
Every method should have only one meaning: making those that have to be implemented protected abstract.
json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java
json/src/main/java/org/apidesign/html/json/spi/Proto.java
json/src/main/java/org/netbeans/html/json/impl/JSON.java
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java
json/src/main/java/org/netbeans/html/json/impl/WrapperObject.java
     1.1 --- a/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Sun Jan 05 23:24:07 2014 +0100
     1.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Mon Jan 06 08:37:00 2014 +0100
     1.3 @@ -82,6 +82,31 @@
     1.4              protected void notifyChange(Proto proto, int propIndex) {
     1.5                  proto.onChange(propIndex);
     1.6              }
     1.7 +
     1.8 +            @Override
     1.9 +            protected <Model> void setValue(Proto.Type<Model> type, Model model, int index, Object value) {
    1.10 +                type.setValue(model, index, value);
    1.11 +            }
    1.12 +
    1.13 +            @Override
    1.14 +            protected <Model> Object getValue(Proto.Type<Model> type, Model model, int index) {
    1.15 +                return type.getValue(model, index);
    1.16 +            }
    1.17 +
    1.18 +            @Override
    1.19 +            protected Proto findProto(Proto.Type<?> type, Object object) {
    1.20 +                return type.protoFor(object);
    1.21 +            }
    1.22 +
    1.23 +            @Override
    1.24 +            protected <Model> Model cloneTo(Proto.Type<Model> type, Model model, BrwsrCtx c) {
    1.25 +                return type.cloneTo(model, c);
    1.26 +            }
    1.27 +
    1.28 +            @Override
    1.29 +            protected Object read(Proto.Type<?> from, BrwsrCtx c, Object data) {
    1.30 +                return from.read(c, data);
    1.31 +            }
    1.32          };
    1.33      }
    1.34  
     2.1 --- a/json/src/main/java/org/apidesign/html/json/spi/Proto.java	Sun Jan 05 23:24:07 2014 +0100
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/Proto.java	Mon Jan 06 08:37:00 2014 +0100
     2.3 @@ -431,14 +431,69 @@
     2.4              return new Proto(obj, this, context);
     2.5          }
     2.6          
     2.7 -        // XXX: should be protected
     2.8 -        public abstract void setValue(Model model, int index, Object value);
     2.9 -        public abstract Object getValue(Model model, int index);
    2.10 -        public abstract void call(Model model, int index, Object data, Object event);
    2.11 -        public abstract Model cloneTo(Object model, BrwsrCtx ctx);
    2.12 -        public abstract Model read(BrwsrCtx c, Object json);
    2.13 -        public abstract void onChange(Model model, int index);
    2.14 -        public abstract Proto protoFor(Object object);
    2.15 +        //
    2.16 +        // Implemented by subclasses
    2.17 +        //
    2.18 +        
    2.19 +        /** Sets value of a {@link #registerProperty(java.lang.String, int, boolean) registered property}
    2.20 +         * to new value.
    2.21 +         * 
    2.22 +         * @param model the instance of {@link Model model class}
    2.23 +         * @param index index of the property used during registration
    2.24 +         * @param value the value to set the property to
    2.25 +         */
    2.26 +        protected abstract void setValue(Model model, int index, Object value);
    2.27 +        
    2.28 +        /** Obtains and returns value of a 
    2.29 +         * {@link #registerProperty(java.lang.String, int, boolean) registered property}.
    2.30 +         * 
    2.31 +         * @param model the instance of {@link Model model class}
    2.32 +         * @param index index of the property used during registration
    2.33 +         * @return current value of the property
    2.34 +         */
    2.35 +        protected abstract Object getValue(Model model, int index);
    2.36 +        
    2.37 +        /** Invokes a {@link #registerFunction(java.lang.String, int) registered function
    2.38 +         * on given object.
    2.39 +         * 
    2.40 +         * @param model the instance of {@link Model model class}
    2.41 +         * @param index index of the property used during registration
    2.42 +         * @param data the currently selected object the function is about to operate on
    2.43 +         * @param event the event that triggered the event
    2.44 +         */
    2.45 +        protected abstract void call(Model model, int index, Object data, Object event);
    2.46 +        
    2.47 +        /** Re-binds the model object to new browser context.
    2.48 +         * 
    2.49 +         * @param model the instance of {@link Model model class}
    2.50 +         * @param ctx browser context to clone the object to
    2.51 +         * @return new instance of the model suitable for new context
    2.52 +         */
    2.53 +        protected abstract Model cloneTo(Model model, BrwsrCtx ctx);
    2.54 +        
    2.55 +        /** Reads raw JSON data and converts them to our model class.
    2.56 +         * 
    2.57 +         * @param c the browser context to work in
    2.58 +         * @param json raw JSON data to get values from
    2.59 +         * @return new instance of model class filled by the data
    2.60 +         */
    2.61 +        protected abstract Model read(BrwsrCtx c, Object json);
    2.62 +        
    2.63 +        /** Called when a {@link #registerProperty(java.lang.String, int, boolean) registered property}
    2.64 +         * changes its value.
    2.65 +         * 
    2.66 +         * @param model the object that has the property
    2.67 +         * @param index the index of the property during registration
    2.68 +         */
    2.69 +        protected abstract void onChange(Model model, int index);
    2.70 +        
    2.71 +        /** Finds out if there is an associated proto-object for given
    2.72 +         * object.
    2.73 +         * 
    2.74 +         * @param object an object, presumably (but not necessarily) instance of Model class
    2.75 +         * @return associated proto-object or <code>null</code>
    2.76 +         */
    2.77 +        protected abstract Proto protoFor(Object object);
    2.78  
    2.79          /**
    2.80           * 
     3.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Sun Jan 05 23:24:07 2014 +0100
     3.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Mon Jan 06 08:37:00 2014 +0100
     3.3 @@ -352,11 +352,11 @@
     3.4      }
     3.5      
     3.6      public static <Model> Model bindTo(Model model, BrwsrCtx c) {
     3.7 -        Proto.Type<?> from = findType(model.getClass());
     3.8 +        Proto.Type<Model> from = (Proto.Type<Model>) findType(model.getClass());
     3.9          if (from == null) {
    3.10              throw new IllegalArgumentException();
    3.11          }
    3.12 -        return (Model) from.cloneTo(model, c);
    3.13 +        return PropertyBindingAccessor.clone(from, model, c);
    3.14      }
    3.15      
    3.16      public static <T> T readStream(BrwsrCtx c, Class<T> modelClazz, InputStream data) 
    3.17 @@ -376,7 +376,7 @@
    3.18              if (from == null) {
    3.19                  initClass(modelClazz);
    3.20              } else {
    3.21 -                return modelClazz.cast(from.read(c, data));
    3.22 +                return modelClazz.cast(PropertyBindingAccessor.readFrom(from, c, data));
    3.23              }
    3.24          }
    3.25          throw new NullPointerException();
     4.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Sun Jan 05 23:24:07 2014 +0100
     4.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Mon Jan 06 08:37:00 2014 +0100
     4.3 @@ -375,7 +375,7 @@
     4.4                  w.append("    }\n");
     4.5                  w.append(onReceiveType);
     4.6                  w.append("    @Override public " + className + " read(net.java.html.BrwsrCtx c, Object json) { return new " + className + "(c, json); }\n");
     4.7 -                w.append("    @Override public " + className + " cloneTo(Object o, net.java.html.BrwsrCtx c) { return ((" + className + ")o).clone(c); }\n");
     4.8 +                w.append("    @Override public " + className + " cloneTo(" + className + " o, net.java.html.BrwsrCtx c) { return o.clone(c); }\n");
     4.9                  w.append("  }\n");
    4.10                  w.append("  private ").append(className).append("(net.java.html.BrwsrCtx c, Object json) {\n");
    4.11                  w.append("    this(c);\n");
     5.1 --- a/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java	Sun Jan 05 23:24:07 2014 +0100
     5.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java	Mon Jan 06 08:37:00 2014 +0100
     5.3 @@ -72,6 +72,11 @@
     5.4      
     5.5      protected abstract Bindings bindings(Proto proto, boolean initialize);
     5.6      protected abstract void notifyChange(Proto proto, int propIndex);
     5.7 +    protected abstract <Model> void setValue(Proto.Type<Model> type, Model model, int index, Object value);
     5.8 +    protected abstract <Model> Object getValue(Proto.Type<Model> type, Model model, int index);
     5.9 +    protected abstract Proto findProto(Proto.Type<?> type, Object object);
    5.10 +    protected abstract <Model> Model cloneTo(Proto.Type<Model> type, Model model, BrwsrCtx c);
    5.11 +    protected abstract Object read(Proto.Type<?> from, BrwsrCtx c, Object data);
    5.12      
    5.13      static Bindings getBindings(Proto proto, boolean initialize) {
    5.14          return DEFAULT.bindings(proto, initialize);
    5.15 @@ -90,6 +95,15 @@
    5.16      ) {
    5.17          return DEFAULT.newCall(ctx, callback, urlBefore, urlAfter, method, data);
    5.18      }
    5.19 +    static Proto protoFor(Proto.Type<?> type, Object object) {
    5.20 +        return DEFAULT.findProto(type, object);
    5.21 +    }
    5.22 +    static <Model> Model clone(Proto.Type<Model> type, Model model, BrwsrCtx c) {
    5.23 +        return DEFAULT.cloneTo(type, model, c);
    5.24 +    }
    5.25 +    static Object readFrom(Proto.Type<?> from, BrwsrCtx c, Object data) {
    5.26 +        return DEFAULT.read(from, c, data);
    5.27 +    }
    5.28  
    5.29      public static final class PBData<M> {
    5.30          public final String name;
    5.31 @@ -109,11 +123,11 @@
    5.32          }
    5.33  
    5.34          public void setValue(Object v) {
    5.35 -            access.setValue(model, index, v);
    5.36 +            DEFAULT.setValue(access, model, index, v);
    5.37          }
    5.38  
    5.39          public Object getValue() {
    5.40 -            return access.getValue(model, index);
    5.41 +            return DEFAULT.getValue(access, model, index);
    5.42          }
    5.43  
    5.44          public boolean isReadOnly() {
     6.1 --- a/json/src/main/java/org/netbeans/html/json/impl/WrapperObject.java	Sun Jan 05 23:24:07 2014 +0100
     6.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/WrapperObject.java	Mon Jan 06 08:37:00 2014 +0100
     6.3 @@ -73,7 +73,7 @@
     6.4          if (type == null) {
     6.5              return null;
     6.6          }
     6.7 -        final Proto proto = type.protoFor(object);
     6.8 +        final Proto proto = PropertyBindingAccessor.protoFor(type, object);
     6.9          if (proto == null) {
    6.10              return null;
    6.11          }