Class BeanToPropertyValueTransformer
- java.lang.Object
-
- org.apache.commons.beanutils.BeanToPropertyValueTransformer
-
- All Implemented Interfaces:
org.apache.commons.collections.Transformer
public class BeanToPropertyValueTransformer extends java.lang.Object implements org.apache.commons.collections.TransformerTransformerthat outputs a property value.An implementation of
org.apache.commons.collections.Transformerthat transforms the object provided by returning the value of a specified property of the object. The constructor forBeanToPropertyValueTransformerrequires the name of the property that will be used in the transformation. The property can be a simple, nested, indexed, or mapped property as defined byorg.apache.commons.beanutils.PropertyUtils. If any object in the property path specified bypropertyNameisnullthen the outcome is based on the value of theignoreNullattribute.A typical usage might look like:
// create the transformer BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "person.address.city" ); // transform the Collection Collection peoplesCities = CollectionUtils.collect( peopleCollection, transformer );
This would take a
Collectionof person objects and return aCollectionof objects which represents the cities in which each person lived. Assuming...-
The top level object in the
peeopleCollectionis an object which represents a person. -
The person object has a
getAddress()method which returns an object which represents a person's address. -
The address object has a
getCity()method which returns an object which represents the city in which a person lives.
- See Also:
PropertyUtils,Transformer
-
-
Field Summary
Fields Modifier and Type Field Description private booleanignoreNullShould null objects on the property path throw anIllegalArgumentException?private org.apache.commons.logging.LoglogFor logging.private java.lang.StringpropertyNameThe name of the property that will be used in the transformation of the object.
-
Constructor Summary
Constructors Constructor Description BeanToPropertyValueTransformer(java.lang.String propertyName)Constructs a Transformer which does not ignore nulls.BeanToPropertyValueTransformer(java.lang.String propertyName, boolean ignoreNull)Constructs a Transformer and sets ignoreNull.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetPropertyName()Returns the name of the property that will be used in the transformation of the bean.booleanisIgnoreNull()Returns the flag which determines whethernullobjects in the property path will genenerate anIllegalArgumentExceptionor not.java.lang.Objecttransform(java.lang.Object object)Returns the value of the property named in the transformer's constructor for the object provided.
-
-
-
Field Detail
-
log
private final org.apache.commons.logging.Log log
For logging.
-
propertyName
private final java.lang.String propertyName
The name of the property that will be used in the transformation of the object.
-
ignoreNull
private final boolean ignoreNull
Should null objects on the property path throw an
IllegalArgumentException?Determines whether
nullobjects in the property path will genenerate anIllegalArgumentExceptionor not. If set totruethen if any objects in the property path evaluate tonullthen theIllegalArgumentExceptionthrow byPropertyUtilswill be logged but not rethrown andnullwill be returned. If set tofalsethen if any objects in the property path evaluate tonullthen theIllegalArgumentExceptionthrow byPropertyUtilswill be logged and rethrown.
-
-
Constructor Detail
-
BeanToPropertyValueTransformer
public BeanToPropertyValueTransformer(java.lang.String propertyName)
Constructs a Transformer which does not ignore nulls. Constructor which takes the name of the property that will be used in the transformation and assumesignoreNullto befalse.- Parameters:
propertyName- The name of the property that will be used in the transformation.- Throws:
java.lang.IllegalArgumentException- If thepropertyNameisnullor empty.
-
BeanToPropertyValueTransformer
public BeanToPropertyValueTransformer(java.lang.String propertyName, boolean ignoreNull)Constructs a Transformer and sets ignoreNull. Constructor which takes the name of the property that will be used in the transformation and a boolean which determines whethernullobjects in the property path will genenerate anIllegalArgumentExceptionor not.- Parameters:
propertyName- The name of the property that will be used in the transformation.ignoreNull- Determines whethernullobjects in the property path will genenerate anIllegalArgumentExceptionor not.- Throws:
java.lang.IllegalArgumentException- If thepropertyNameisnullor empty.
-
-
Method Detail
-
getPropertyName
public java.lang.String getPropertyName()
Returns the name of the property that will be used in the transformation of the bean.- Returns:
- The name of the property that will be used in the transformation of the bean.
-
isIgnoreNull
public boolean isIgnoreNull()
Returns the flag which determines whethernullobjects in the property path will genenerate anIllegalArgumentExceptionor not. If set totruethen if any objects in the property path evaluate tonullthen theIllegalArgumentExceptionthrow byPropertyUtilswill be logged but not rethrown andnullwill be returned. If set tofalsethen if any objects in the property path evaluate tonullthen theIllegalArgumentExceptionthrow byPropertyUtilswill be logged and rethrown.- Returns:
- The flag which determines whether
nullobjects in the property path will genenerate anIllegalArgumentExceptionor not.
-
transform
public java.lang.Object transform(java.lang.Object object)
Returns the value of the property named in the transformer's constructor for the object provided. If any object in the property path leading up to the target property isnullthen the outcome will be based on the value of theignoreNullattribute. By default,ignoreNullisfalseand would result in anIllegalArgumentExceptionif an object in the property path leading up to the target property isnull.- Specified by:
transformin interfaceorg.apache.commons.collections.Transformer- Parameters:
object- The object to be transformed.- Returns:
- The value of the property named in the transformer's constructor for the object provided.
- Throws:
java.lang.IllegalArgumentException- If an IllegalAccessException, InvocationTargetException, or NoSuchMethodException is thrown when trying to access the property specified on the object provided. Or if an object in the property path provided isnullandignoreNullis set tofalse.
-
-