Simplify implementation of FunctionBinding by usage of enhanced singletonizer holder Impl
1.1 --- a/json/src/main/java/org/apidesign/html/json/spi/FunctionBinding.java Mon Dec 23 15:06:51 2013 +0100
1.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/FunctionBinding.java Mon Dec 23 15:29:02 2013 +0100
1.3 @@ -44,35 +44,59 @@
1.4
1.5 import net.java.html.json.Function;
1.6 import net.java.html.json.Model;
1.7 -import org.netbeans.html.json.impl.PropertyBindingAccessor.FBData;
1.8
1.9 /** Describes a function provided by the {@link Model} and
1.10 * annotated by {@link Function} annotation.
1.11 *
1.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.13 */
1.14 -public final class FunctionBinding {
1.15 - private final FBData<?> fb;
1.16 -
1.17 - FunctionBinding(FBData<?> fb) {
1.18 - this.fb = fb;
1.19 - }
1.20 -
1.21 - public String getFunctionName() {
1.22 - return fb.name;
1.23 +public abstract class FunctionBinding {
1.24 + FunctionBinding() {
1.25 }
1.26
1.27 - /** Calls the function provided data associated with current element,
1.28 - * as well as information about the event that triggered the event.
1.29 - *
1.30 + /** Returns name of the function.
1.31 + * @return function name
1.32 + */
1.33 + public abstract String getFunctionName();
1.34 +
1.35 + /**
1.36 + * Calls the function provided data associated with current element, as well
1.37 + * as information about the event that triggered the event.
1.38 + *
1.39 * @param data data associated with selected element
1.40 * @param ev event (with additional properties) that triggered the event
1.41 */
1.42 - public void call(Object data, Object ev) {
1.43 - try {
1.44 - fb.call(data, ev);
1.45 - } catch (Throwable ex) {
1.46 - ex.printStackTrace();
1.47 + public abstract void call(Object data, Object ev);
1.48 +
1.49 + static <M> FunctionBinding registerFunction(String name, int index, M model, Proto.Type<M> access) {
1.50 + return new Impl<M>(name, index, model, access);
1.51 + }
1.52 +
1.53 + private static final class Impl<M> extends FunctionBinding {
1.54 + final String name;
1.55 + private final M model;
1.56 + private final Proto.Type<M> access;
1.57 + private final int index;
1.58 +
1.59 + public Impl(String name, int index, M model, Proto.Type<M> access) {
1.60 + this.name = name;
1.61 + this.index = index;
1.62 + this.model = model;
1.63 + this.access = access;
1.64 + }
1.65 +
1.66 + @Override
1.67 + public String getFunctionName() {
1.68 + return name;
1.69 + }
1.70 +
1.71 + @Override
1.72 + public void call(Object data, Object ev) {
1.73 + try {
1.74 + access.call(model, index, data, ev);
1.75 + } catch (Throwable ex) {
1.76 + ex.printStackTrace();
1.77 + }
1.78 }
1.79 }
1.80 }
2.1 --- a/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java Mon Dec 23 15:06:51 2013 +0100
2.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java Mon Dec 23 15:29:02 2013 +0100
2.3 @@ -68,11 +68,6 @@
2.4 }
2.5
2.6 @Override
2.7 - protected <M> FunctionBinding newFunction(FBData<M> d) {
2.8 - return new FunctionBinding(d);
2.9 - }
2.10 -
2.11 - @Override
2.12 protected JSONCall newCall(BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter, String method, Object data) {
2.13 return new JSONCall(ctx, callback, urlBefore, urlAfter, method, data);
2.14 }
3.1 --- a/json/src/main/java/org/apidesign/html/json/spi/Proto.java Mon Dec 23 15:06:51 2013 +0100
3.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/Proto.java Mon Dec 23 15:29:02 2013 +0100
3.3 @@ -142,7 +142,7 @@
3.4 }
3.5 FunctionBinding[] fb = new FunctionBinding[type.functions.length];
3.6 for (int i = 0; i < fb.length; i++) {
3.7 - fb[i] = b.registerFunction(
3.8 + fb[i] = FunctionBinding.registerFunction(
3.9 type.functions[i], i, obj, type
3.10 );
3.11 }
4.1 --- a/json/src/main/java/org/netbeans/html/json/impl/Bindings.java Mon Dec 23 15:06:51 2013 +0100
4.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/Bindings.java Mon Dec 23 15:29:02 2013 +0100
4.3 @@ -47,7 +47,6 @@
4.4 import org.apidesign.html.json.spi.PropertyBinding;
4.5 import org.apidesign.html.json.spi.Proto;
4.6 import org.apidesign.html.json.spi.Technology;
4.7 -import org.netbeans.html.json.impl.PropertyBindingAccessor.FBData;
4.8 import org.netbeans.html.json.impl.PropertyBindingAccessor.PBData;
4.9
4.10 /**
4.11 @@ -66,10 +65,6 @@
4.12 return PropertyBindingAccessor.create(new PBData<M>(this, propName, index, model, access, readOnly));
4.13 }
4.14
4.15 - public <M> FunctionBinding registerFunction(String name, int index, M model, Proto.Type<M> access) {
4.16 - return PropertyBindingAccessor.createFunction(new FBData<M>(name, index, model, access));
4.17 - }
4.18 -
4.19 public static Bindings<?> apply(BrwsrCtx c, Object model) {
4.20 Technology<?> bp = JSON.findTechnology(c);
4.21 return apply(bp);
5.1 --- a/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java Mon Dec 23 15:06:51 2013 +0100
5.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java Mon Dec 23 15:29:02 2013 +0100
5.3 @@ -65,7 +65,6 @@
5.4 }
5.5
5.6 protected abstract <M> PropertyBinding newBinding(PBData<M> d);
5.7 - protected abstract <M> FunctionBinding newFunction(FBData<M> d);
5.8 protected abstract JSONCall newCall(
5.9 BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter,
5.10 String method, Object data
5.11 @@ -75,9 +74,6 @@
5.12 static <M> PropertyBinding create(PBData<M> d) {
5.13 return DEFAULT.newBinding(d);
5.14 }
5.15 - static <M> FunctionBinding createFunction(FBData<M> d) {
5.16 - return DEFAULT.newFunction(d);
5.17 - }
5.18 static JSONCall createCall(
5.19 BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter,
5.20 String method, Object data
5.21 @@ -118,23 +114,4 @@
5.22 return bindings;
5.23 }
5.24 } // end of PBData
5.25 -
5.26 - public static final class FBData<M> {
5.27 - public final String name;
5.28 - private final M model;
5.29 - private final Proto.Type<M> access;
5.30 - private final int index;
5.31 -
5.32 - public FBData(String name, int index, M model, Proto.Type<M> access) {
5.33 - this.name = name;
5.34 - this.index = index;
5.35 - this.model = model;
5.36 - this.access = access;
5.37 - }
5.38 -
5.39 -
5.40 - public void call(Object data, Object ev) {
5.41 - access.call(model, index, data, ev);
5.42 - }
5.43 - } // end of FBData
5.44 }