Author: tchemit Date: 2011-11-08 21:27:22 +0100 (Tue, 08 Nov 2011) New Revision: 2221 Url: http://nuiton.org/repositories/revision/nuiton-utils/2221 Log: Evolution #1798: [Bean] Improve the BeanMonitor api Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/PropertyDiff.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BinderTest.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java 2011-10-28 21:25:38 UTC (rev 2220) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java 2011-11-08 20:27:22 UTC (rev 2221) @@ -30,7 +30,8 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.Arrays; -import java.util.LinkedHashSet; +import java.util.Collection; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -68,10 +69,13 @@ protected final List<String> propertyNames; /** Names of monitored and modified properties for the attached bean. */ - protected final Set<String> modifiedProperties; + protected final Map<String, PropertyDiff> propertyDiffs; - /** To store the original values when watched properties are changing. */ - protected final Map<String, Object> originalValues; +// /** Names of monitored and modified properties for the attached bean. */ +// protected final Set<String> modifiedProperties; +// +// /** To store the original values when watched properties are changing. */ +// protected final Map<String, Object> originalValues; /** * The {@link PropertyChangeListener} which listen modification on bean @@ -89,8 +93,10 @@ */ public BeanMonitor(String... propertyNames) { this.propertyNames = Arrays.asList(propertyNames); - modifiedProperties = new LinkedHashSet<String>(); - originalValues = new TreeMap<String, Object>(); +// modifiedProperties = new LinkedHashSet<String>(); +// originalValues = new TreeMap<String, Object>(); + propertyDiffs = new LinkedHashMap<String, PropertyDiff>(); + listener = new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { @@ -104,31 +110,65 @@ Object newValue = evt.getNewValue(); Object oldValue = evt.getOldValue(); - if (modifiedProperties.contains(propertyName)) { + PropertyDiff propertyDiff; - // property already modified + if (propertyDiffs.containsKey(propertyName)) { + + // property was already modified + propertyDiff = propertyDiffs.get(propertyName); + + // change the target value + propertyDiff.setTargetValue(newValue); + // check if value did not come back to original - Object originalValue = originalValues.get(propertyName); - + Object originalValue = propertyDiff.getSourceValue(); if (originalValue == null && newValue == null || originalValue != null && originalValue.equals(newValue)) { // coming back to original value // the property is no more modified - modifiedProperties.remove(propertyName); - originalValues.remove(propertyName); - return; + propertyDiffs.remove(propertyName); +// return; } + } else { + propertyDiff = new PropertyDiff( + propertyName, + oldValue, + propertyName, + newValue, + null + ); - // can safely return since original is already known - // and property is already known to be modified - return; + // add it to modified properties + propertyDiffs.put(propertyName, propertyDiff); + } - // the property was not modified before - // just mark it and keep the original value - modifiedProperties.add(propertyName); - originalValues.put(propertyName, oldValue); +// if (modifiedProperties.contains(propertyName)) { +// +// // property already modified +// // check if value did not come back to original +// Object originalValue = originalValues.get(propertyName); +// +// if (originalValue == null && newValue == null || +// originalValue != null && originalValue.equals(newValue)) { +// +// // coming back to original value +// // the property is no more modified +// modifiedProperties.remove(propertyName); +// originalValues.remove(propertyName); +// return; +// } +// +// // can safely return since original is already known +// // and property is already known to be modified +// return; +// } +// +// // the property was not modified before +// // just mark it and keep the original value +// modifiedProperties.add(propertyName); +// originalValues.put(propertyName, oldValue); } }; } @@ -151,7 +191,7 @@ * the attached bean, {@code false} otherwise. */ public boolean wasModified() { - return !modifiedProperties.isEmpty(); + return !propertyDiffs.isEmpty(); } /** @@ -162,7 +202,8 @@ * @return the array of property names to monitor. */ public String[] getModifiedProperties() { - return modifiedProperties.toArray(new String[modifiedProperties.size()]); + Set<String> propertyNames = propertyDiffs.keySet(); + return propertyNames.toArray(new String[propertyNames.size()]); } /** @@ -173,11 +214,26 @@ public Map<String, Object> getOriginalValues() { // always expose a copy of map - Map<String, Object> map = new TreeMap<String, Object>(originalValues); + Map<String, Object> map = new TreeMap<String, Object>(); + for (PropertyDiff propertyDiff : propertyDiffs.values()) { + map.put(propertyDiff.getSourceProperty(), + propertyDiff.getSourceValue()); + } return map; } /** + * Obtains the property diffs since the bean is monitoried. + * + * @return the property diffs since the bean is monitoried. + * @since 2.4 + */ + public PropertyDiff[] getPropertyDiffs() { + Collection<PropertyDiff> values = propertyDiffs.values(); + return values.toArray(new PropertyDiff[values.size()]); + } + + /** * Sets the {@code bean} to monitor. * <p/> * As a side effect, it will attach the {@link #listener} to new bean @@ -219,7 +275,8 @@ /** To clear the modified states. */ public void clearModified() { - modifiedProperties.clear(); - originalValues.clear(); + propertyDiffs.clear(); +// modifiedProperties.clear(); +// originalValues.clear(); } } 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-10-28 21:25:38 UTC (rev 2220) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java 2011-11-08 20:27:22 UTC (rev 2221) @@ -366,74 +366,6 @@ } } - public class PropertyDiff { - - protected Class<?> propertyType; - - protected String sourceProperty; - - protected Object sourceValue; - - protected String targetProperty; - - protected Object targetValue; - - public PropertyDiff() { - } - - 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() { - return sourceProperty; - } - - public void setSourceProperty(String sourceProperty) { - this.sourceProperty = sourceProperty; - } - - public Object getSourceValue() { - return sourceValue; - } - - public void setSourceValue(Object sourceValue) { - this.sourceValue = sourceValue; - } - - public String getTargetProperty() { - return targetProperty; - } - - public void setTargetProperty(String targetProperty) { - this.targetProperty = targetProperty; - } - - public Object getTargetValue() { - return targetValue; - } - - 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, Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/PropertyDiff.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/PropertyDiff.java (rev 0) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/PropertyDiff.java 2011-11-08 20:27:22 UTC (rev 2221) @@ -0,0 +1,76 @@ +package org.nuiton.util.beans; + +/** +* TODO +* +* @author bleny <leny@codelutin.com> +* @since 2.4 +*/ +public class PropertyDiff { + + protected Class<?> propertyType; + + protected String sourceProperty; + + protected Object sourceValue; + + protected String targetProperty; + + protected Object targetValue; + + public PropertyDiff() { + } + + 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() { + return sourceProperty; + } + + public void setSourceProperty(String sourceProperty) { + this.sourceProperty = sourceProperty; + } + + public Object getSourceValue() { + return sourceValue; + } + + public void setSourceValue(Object sourceValue) { + this.sourceValue = sourceValue; + } + + public String getTargetProperty() { + return targetProperty; + } + + public void setTargetProperty(String targetProperty) { + this.targetProperty = targetProperty; + } + + public Object getTargetValue() { + return targetValue; + } + + public void setTargetValue(Object targetValue) { + this.targetValue = targetValue; + } + + public Class<?> getPropertyType() { + return propertyType; + } + + public void setPropertyType(Class<?> propertyType) { + this.propertyType = propertyType; + } + +} Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BinderTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BinderTest.java 2011-10-28 21:25:38 UTC (rev 2220) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BinderTest.java 2011-11-08 20:27:22 UTC (rev 2221) @@ -236,13 +236,13 @@ a.setB(VALUE_A); b.setB(VALUE_B); - List<Binder<BeanA, BeanB>.PropertyDiff> diff = binderB.diff(a, b); + List<PropertyDiff> diff = binderB.diff(a, b); // only one property has changed Assert.assertEquals(1, diff.size()); // names of the properties used for comparison - Binder.PropertyDiff propertyDiff = diff.get(0); + PropertyDiff propertyDiff = diff.get(0); Assert.assertEquals(BeanA.PROPERTY_B, propertyDiff.getSourceProperty()); Assert.assertEquals(BeanB.PROPERTY_B, propertyDiff.getTargetValue());