1 package org.onap.sdc.dcae.catalog.commons;
5 import java.util.function.Function;
6 import java.util.function.BiFunction;
8 import org.apache.commons.beanutils.ConvertUtils;
9 import org.apache.commons.beanutils.Converter;
11 import org.json.JSONObject;
13 import org.onap.sdc.dcae.catalog.commons.Proxy;
14 import org.onap.sdc.dcae.catalog.commons.ProxyBuilder;
17 public class ProxyBuilder {
19 private Map<String, ?> context;
20 private Map<String, BiFunction<Proxy, Object[], Object>> extensions;
22 public ProxyBuilder() {
25 public <T> T build(Map theData, Class<T> theType) {
26 return build(theData, this.context, theType);
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));
36 public <T> T build(Map theData, Class<T> theType) {
37 return build(new JSONObject(theData), theType);
40 public <T> T build(Map theData, Map theContextData, Class<T> theType) {
41 return build(new JSONObject(theData), theContextData, theType);
44 public <T> T build(JSONObject theData, Class<T> theType) {
45 return build(theData, this.context, theType);
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));
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);
69 plug in an extension to the proxy default behaviour.
71 public ProxyBuilder withExtensions(Map<String, BiFunction<Proxy, Object[], Object>> theExtensions) {
72 this.extensions = theExtensions;
76 public ProxyBuilder withContext(Map<String, ?> theContext) {
77 this.context = theContext;
81 protected Object context(String theName) {
82 return this.context == null ? null : this.context.get(theName);
85 protected BiFunction<Proxy, Object[], Object> extension(String theName) {
86 return this.extensions == null ? null : this.extensions.get(theName);
89 protected boolean hasExtension(String theName) {
90 return this.extensions == null ? false : this.extensions.containsKey(theName);