branch develop updated (73c480d -> ecb5c52)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git from 73c480d Catch more exception while binding (fixes #3758) new ecb5c52 Be able to transform target values in a Binder (fixes #3759) The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit ecb5c52fb7ca442e1badbea2919d1788ffce7051 Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Aug 19 16:02:54 2015 +0200 Be able to transform target values in a Binder (fixes #3759) Summary of changes: src/main/java/org/nuiton/util/beans/Binder.java | 38 ++++++++++++++++++++++ .../org/nuiton/util/beans/BinderModelBuilder.java | 8 ++++- 2 files changed, 45 insertions(+), 1 deletion(-) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git commit ecb5c52fb7ca442e1badbea2919d1788ffce7051 Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Aug 19 16:02:54 2015 +0200 Be able to transform target values in a Binder (fixes #3759) --- src/main/java/org/nuiton/util/beans/Binder.java | 38 ++++++++++++++++++++++ .../org/nuiton/util/beans/BinderModelBuilder.java | 8 ++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nuiton/util/beans/Binder.java b/src/main/java/org/nuiton/util/beans/Binder.java index d85b150..c261551 100644 --- a/src/main/java/org/nuiton/util/beans/Binder.java +++ b/src/main/java/org/nuiton/util/beans/Binder.java @@ -22,6 +22,7 @@ package org.nuiton.util.beans; +import com.google.common.base.Function; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,6 +36,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -261,6 +263,8 @@ public class Binder<I, O> implements Serializable { */ public void injectProperties(Map<String, Object> properties, O target, boolean includeNullValues) { + boolean useFunctions = model.isUseFunctions(); + for (Map.Entry<String, Object> entry : properties.entrySet()) { String propertyName = entry.getKey(); if (!getModel().containsTargetProperty(propertyName)) { @@ -278,6 +282,9 @@ public class Binder<I, O> implements Serializable { if (log.isDebugEnabled()) { log.debug("Inject property: " + propertyName + " to " + target); } + if (useFunctions && propertyValue != null) { + propertyValue = transform(propertyName, propertyValue); + } Method writeMethod = getModel().getTargetWriteMethod(propertyName); try { writeMethod.invoke(target, propertyValue); @@ -292,6 +299,17 @@ public class Binder<I, O> implements Serializable { } + protected Object transform(String propertyName, Object propertyValue) { + Function function = model.getFunction(propertyValue.getClass()); + if (function != null) { + if (log.isDebugEnabled()) { + log.debug("Transform property: " + propertyName); + } + propertyValue = function.apply(propertyValue); + } + return propertyValue; + } + /** * Copy properties from a source bean to a destination one according to * the model of the binder. If {@code propertyNames} is defined, only @@ -384,6 +402,8 @@ public class Binder<I, O> implements Serializable { getAllPropertiesExclude(propertyNames) : getProperties(propertyNames); + boolean useFunctions = model.isUseFunctions(); + for (String sourceProperty : propertyNames) { String targetProperty = model.getTargetProperty(sourceProperty); @@ -416,6 +436,9 @@ public class Binder<I, O> implements Serializable { // specific collection strategy is set, must use it read = getCollectionValue(sourceProperty, read); } + if (useFunctions && read != null) { + read = transform(sourceProperty, read); + } model.getTargetWriteMethod(targetProperty).invoke(target, read); } catch (Exception e) { throw new RuntimeException( @@ -707,6 +730,11 @@ public class Binder<I, O> implements Serializable { /** factory of target Instance */ protected InstanceFactory<T> instanceFactory; + /** + * Dictonnary of function to apply by source class type. + */ + protected final Map<Class<?>, Function<?,?>> functions; + private static final long serialVersionUID = 2L; public BinderModel(Class<S> sourceType, Class<T> targetType) { @@ -717,6 +745,7 @@ public class Binder<I, O> implements Serializable { propertiesMapping = new TreeMap<String, String>(); collectionStrategies = new TreeMap<String, CollectionStrategy>(); binders = new TreeMap<String, Binder<?, ?>>(); + functions = new LinkedHashMap<>(); } /** @@ -936,5 +965,14 @@ public class Binder<I, O> implements Serializable { public InstanceFactory<T> getInstanceFactory() { return instanceFactory; } + + public Function getFunction(Class<?> aClass) { + return functions.get(aClass); + } + + public boolean isUseFunctions() { + return !functions.isEmpty(); + } + } } diff --git a/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java b/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java index 1964158..f905457 100644 --- a/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java +++ b/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java @@ -22,6 +22,8 @@ package org.nuiton.util.beans; +import com.google.common.base.Function; + import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; @@ -243,6 +245,11 @@ public class BinderModelBuilder<S, T> { return this; } + public <K,V> BinderModelBuilder<S, T> addFunction(Class<K> type, Function<K,V> function) { + model.functions.put(type, function); + return this; + } + /** * Convinient method to create directly a {@link Binder} using the * underlying {@link #model} the builder contains. @@ -599,7 +606,6 @@ public class BinderModelBuilder<S, T> { model.addBinding(sourceDescriptor, targetDescriptor); } - protected Binder.BinderModel<S, T> getModel() { return model; } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm