Author: tchemit Date: 2011-09-09 07:09:07 +0200 (Fri, 09 Sep 2011) New Revision: 2203 Url: http://nuiton.org/repositories/revision/nuiton-utils/2203 Log: Evolution #1728: add diff method in Binder to compare the values of two bean : add type in PropertyDiff + reformat code Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java 2011-09-09 05:08:34 UTC (rev 2202) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java 2011-09-09 05:09:07 UTC (rev 2203) @@ -140,16 +140,16 @@ * Obtain from the given object all properties registered in the binder * model. * - * @param source the bean to read - * @param propertyNames subset of properties to load + * @param source the bean to read + * @param propertyNames subset of properties to load * @param includeNullValues get <strong>all</strong> the properties and - * values for the given bean. If false, you'll get only the values + * values for the given bean. If false, you'll get only the values * @return the map of properties obtained indexed by their property name, * or an empty map is the given {@code from} is {@code null}. * @since 2.3 */ public Map<String, Object> obtainProperties(I source, - boolean includeNullValues, String... propertyNames) { + boolean includeNullValues, String... propertyNames) { if (source == null) { // special limit case return Collections.emptyMap(); @@ -188,7 +188,7 @@ } } - boolean include = read != null || read == null && includeNullValues; + boolean include = read != null || includeNullValues; if (include) { result.put(sourceProperty, read); } @@ -329,7 +329,7 @@ } protected Object readProperty(String sourceProperty, Object source, - Method readMethod) { + Method readMethod) { try { Object read = null; if (source != null) { @@ -368,6 +368,8 @@ public class PropertyDiff { + protected Class<?> propertyType; + protected String sourceProperty; protected Object sourceValue; @@ -376,13 +378,19 @@ protected Object targetValue; - public PropertyDiff() {} + public PropertyDiff() { + } - public PropertyDiff(String sourceProperty, Object sourceValue, String targetProperty, Object targetValue) { + public PropertyDiff(String sourceProperty, + Object sourceValue, + String targetProperty, + Object targetValue, + Class<?> propertyType) { this.sourceProperty = sourceProperty; this.sourceValue = sourceValue; this.targetProperty = targetProperty; this.targetValue = targetValue; + this.propertyType= propertyType; } public String getSourceProperty() { @@ -416,10 +424,20 @@ public void setTargetValue(Object targetValue) { this.targetValue = targetValue; } + + public Class<?> getPropertyType() { + return propertyType; + } + + public void setPropertyType(Class<?> propertyType) { + this.propertyType = propertyType; + } } - protected List<PropertyDiff> diff(I source, O target, boolean excludeProperties, - String... propertyNames) { + protected List<PropertyDiff> diff(I source, + O target, + boolean excludeProperties, + String... propertyNames) { if (source == null) { throw new NullPointerException("parameter 'source' can no be null"); } @@ -437,16 +455,24 @@ Method sourceReadMethod = model.getSourceReadMethod(sourceProperty); - Object sourceRead = readProperty(sourceProperty, source, sourceReadMethod); + Object sourceRead = readProperty(sourceProperty, source, + sourceReadMethod); String targetProperty = model.getTargetProperty(sourceProperty); Method targetReadMethod = model.getTargetReadMethod(targetProperty); - Object targetRead = readProperty(targetProperty, target, targetReadMethod); + Object targetRead = readProperty(targetProperty, target, + targetReadMethod); if (ObjectUtils.notEqual(sourceRead, targetRead)) { - PropertyDiff propertyDiff = new PropertyDiff(sourceProperty, sourceRead, targetProperty, targetRead); + PropertyDiff propertyDiff = new PropertyDiff( + sourceProperty, + sourceRead, + targetProperty, + targetRead, + sourceReadMethod.getReturnType() + ); result.add(propertyDiff); } } @@ -454,34 +480,39 @@ return result; } - /** Compare two beans property by property according to the model. - * + /** + * Compare two beans property by property according to the model. + * <p/> * List contains one element per property with different values (according * to the result of an equals() call) * * @param source a bean of type I * @param target a bean of type O * @return a list with all the properties which values differ in source - * and target. Properties with equal values are not included. + * and target. Properties with equal values are not included. * @since 2.3 */ public List<PropertyDiff> diff(I source, O target) { return diff(source, target, false); } - /** Compare two beans property by property according to the model. - * + /** + * Compare two beans property by property according to the model. + * <p/> * List contains one element per property with different values (according * to the result of an equals() call) * * @param source a bean of type I * @param target a bean of type O + * @param propertyNames property names to exclude from the diff * @return a list with all the properties which values differ in source - * and target. Properties with equal values and excluded properties will - * not be contained in the result + * and target. Properties with equal values and excluded properties + * will not be contained in the result * @since 2.3 */ - public List<PropertyDiff> diffExcluding(I source, O target, String... propertyNames) { + public List<PropertyDiff> diffExcluding(I source, + O target, + String... propertyNames) { return diff(source, target, true, propertyNames); }
participants (1)
-
tchemit@users.nuiton.org