DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / commons / src / main / java / org / onap / sdc / dcae / catalog / commons / ProxyBuilder.java
1 package org.onap.sdc.dcae.catalog.commons;
2
3 import java.util.Map;
4
5 import java.util.function.Function;
6 import java.util.function.BiFunction;
7
8 import org.apache.commons.beanutils.ConvertUtils;
9 import org.apache.commons.beanutils.Converter;
10
11 import org.json.JSONObject;
12
13 import org.onap.sdc.dcae.catalog.commons.Proxy;
14 import org.onap.sdc.dcae.catalog.commons.ProxyBuilder;
15
16
17 public class ProxyBuilder {
18
19         private Map<String, ?> context;
20         private Map<String, BiFunction<Proxy, Object[], Object>> extensions; 
21
22         public ProxyBuilder() {
23         }
24 /*
25         public <T> T build(Map theData, Class<T> theType) {
26                 return build(theData, this.context, theType);
27         }
28         
29         public <T> T build(Map theData, Map theContextData, Class<T> theType) {
30                 return (T)java.lang.reflect.Proxy.newProxyInstance(
31                                                         ProxyBuilder.class.getClassLoader(),
32                                                                 new Class[] { theType },
33                                                                         new Proxy(theData, this));
34         }
35 */
36         public <T> T build(Map theData, Class<T> theType) {
37                 return build(new JSONObject(theData), theType);
38         }
39         
40         public <T> T build(Map theData, Map theContextData, Class<T> theType) {
41                 return build(new JSONObject(theData), theContextData, theType);
42         }
43
44         public <T> T build(JSONObject theData, Class<T> theType) {
45                 return build(theData, this.context, theType);
46         }
47         
48         public <T> T build(JSONObject theData, Map theContextData, Class<T> theType) {
49                 return (T)java.lang.reflect.Proxy.newProxyInstance(
50                                                         ProxyBuilder.class.getClassLoader(),
51                                                                 new Class[] { theType },
52                                                                         new Proxy(theData, this));
53         }
54
55
56
57
58         public ProxyBuilder withConverter(final Function<Object, ?> theConverter, Class theType) {
59                 ConvertUtils.register(new Converter() {
60                                                                                                                 public Object convert(Class theToType, Object theValue) {
61                                                                                                                         return theConverter.apply(theValue);
62                                                                                                                 }
63                                                                                                         },
64                                                                                                         theType);
65                 return this;
66         }
67         
68         /*
69   plug in an extension to the proxy default behaviour.
70         */
71         public ProxyBuilder withExtensions(Map<String, BiFunction<Proxy, Object[], Object>> theExtensions) {
72                 this.extensions = theExtensions;
73                 return this;
74         }
75
76         public ProxyBuilder withContext(Map<String, ?> theContext) {
77                 this.context = theContext;
78                 return this;
79         }
80
81         protected Object context(String theName) {
82                 return this.context == null ? null : this.context.get(theName);
83         }
84
85         protected BiFunction<Proxy, Object[], Object> extension(String theName) {
86                 return this.extensions == null ? null : this.extensions.get(theName);
87         }
88
89         protected boolean hasExtension(String theName) {
90                 return this.extensions == null ? false : this.extensions.containsKey(theName);
91         }
92 }