Author: bleny Date: 2011-09-01 15:30:35 +0200 (Thu, 01 Sep 2011) New Revision: 2201 Url: http://nuiton.org/repositories/revision/nuiton-utils/2201 Log: #1729 add flag argument in Binder#obtainProperties to get all values even if null Modified: 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/Binder.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java 2011-09-01 12:23:57 UTC (rev 2200) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java 2011-09-01 13:30:35 UTC (rev 2201) @@ -137,19 +137,19 @@ } /** - * Obtain from the given object all properties registred in the binder + * Obtain from the given object all properties registered in the binder * model. - * <p/> - * <b>Note:</b> If a property's value is null, it will not be injected in - * the result. * * @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 * @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, - String... propertyNames) { + boolean includeNullValues, String... propertyNames) { if (source == null) { // special limit case return Collections.emptyMap(); @@ -174,22 +174,24 @@ // for primitive type case, force nullity read = null; } - if (read == null) { - continue; + if (read != null) { + if (model.containsBinderProperty(sourceProperty)) { + if (model.containsCollectionProperty(sourceProperty)) { + read = bindCollection(sourceProperty, read); + } else { + read = bindProperty(sourceProperty, read); + } + } else if (model.containsCollectionProperty(sourceProperty)) { + + // specific collection strategy is set, must use it + read = getCollectionValue(sourceProperty, read); + } } - if (model.containsBinderProperty(sourceProperty)) { - if (model.containsCollectionProperty(sourceProperty)) { - read = bindCollection(sourceProperty, read); - } else { - read = bindProperty(sourceProperty, read); - } - } else if (model.containsCollectionProperty(sourceProperty)) { - // specific collection strategy is set, must use it - read = getCollectionValue(sourceProperty, read); + boolean include = read != null || read == null && includeNullValues; + if (include) { + result.put(sourceProperty, read); } - - result.put(sourceProperty, read); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { @@ -202,6 +204,23 @@ } /** + * Obtain from the given object all properties registered in the binder + * model. + * <p/> + * <b>Note:</b> If a property's value is null, it will not be injected in + * the result. + * + * @param source the bean to read + * @param propertyNames subset of properties to load + * @return the map of properties obtained indexed by their property name, + * or an empty map is the given {@code from} is {@code null}. + */ + public Map<String, Object> obtainProperties(I source, + String... propertyNames) { + return obtainProperties(source, false, propertyNames); + } + + /** * Copy properties from a source bean to a destination one according to * the model of the binder. If {@code propertyNames} is defined, only * those properties will be copied. @@ -265,7 +284,6 @@ getAllPropertiesExclude(propertyNames) : getProperties(propertyNames); - for (String sourceProperty : propertyNames) { String targetProperty = model.getTargetProperty(sourceProperty); 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-09-01 12:23:57 UTC (rev 2200) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BinderTest.java 2011-09-01 13:30:35 UTC (rev 2201) @@ -107,6 +107,9 @@ map = binderA.obtainProperties(a); Assert.assertEquals(0, map.size()); + map = binderA.obtainProperties(a, true); + Assert.assertEquals(1, map.size()); + map = binderB.obtainProperties(a); Assert.assertEquals(0, map.size());