Class BeanToPropertyValueTransformer
- All Implemented Interfaces:
org.apache.commons.collections.Transformer
Transformer
that outputs a property value.
An implementation of org.apache.commons.collections.Transformer
that transforms
the object provided by returning the value of a specified property of the object. The
constructor for BeanToPropertyValueTransformer
requires 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 by org.apache.commons.beanutils.PropertyUtils
. If any
object in the property path specified by propertyName
is null
then the
outcome is based on the value of the ignoreNull
attribute.
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 Collection
of person objects and return a Collection
of objects which represents the cities in which each person lived. Assuming...
-
The top level object in the
peeopleCollection
is 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.
- Version:
- $Id$
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Should null objects on the property path throw anIllegalArgumentException
?private final org.apache.commons.logging.Log
For logging.private String
The name of the property that will be used in the transformation of the object. -
Constructor Summary
ConstructorsConstructorDescriptionBeanToPropertyValueTransformer
(String propertyName) Constructs a Transformer which does not ignore nulls.BeanToPropertyValueTransformer
(String propertyName, boolean ignoreNull) Constructs a Transformer and sets ignoreNull. -
Method Summary
Modifier and TypeMethodDescriptionReturns the name of the property that will be used in the transformation of the bean.boolean
Returns the flag which determines whethernull
objects in the property path will genenerate anIllegalArgumentException
or not.Returns the value of the property named in the transformer's constructor for the object provided.
-
Field Details
-
log
private final org.apache.commons.logging.Log logFor logging. -
propertyName
The name of the property that will be used in the transformation of the object. -
ignoreNull
private boolean ignoreNullShould null objects on the property path throw an
IllegalArgumentException
?Determines whether
null
objects in the property path will genenerate anIllegalArgumentException
or not. If set totrue
then if any objects in the property path evaluate tonull
then theIllegalArgumentException
throw byPropertyUtils
will be logged but not rethrown andnull
will be returned. If set tofalse
then if any objects in the property path evaluate tonull
then theIllegalArgumentException
throw byPropertyUtils
will be logged and rethrown.
-
-
Constructor Details
-
BeanToPropertyValueTransformer
Constructs a Transformer which does not ignore nulls. Constructor which takes the name of the property that will be used in the transformation and assumesignoreNull
to befalse
.- Parameters:
propertyName
- The name of the property that will be used in the transformation.- Throws:
IllegalArgumentException
- If thepropertyName
isnull
or empty.
-
BeanToPropertyValueTransformer
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 whethernull
objects in the property path will genenerate anIllegalArgumentException
or not.- Parameters:
propertyName
- The name of the property that will be used in the transformation.ignoreNull
- Determines whethernull
objects in the property path will genenerate anIllegalArgumentException
or not.- Throws:
IllegalArgumentException
- If thepropertyName
isnull
or empty.
-
-
Method Details
-
transform
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 isnull
then the outcome will be based on the value of theignoreNull
attribute. By default,ignoreNull
isfalse
and would result in anIllegalArgumentException
if an object in the property path leading up to the target property isnull
.- Specified by:
transform
in 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:
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 isnull
andignoreNull
is set tofalse
.
-
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 whethernull
objects in the property path will genenerate anIllegalArgumentException
or not. If set totrue
then if any objects in the property path evaluate tonull
then theIllegalArgumentException
throw byPropertyUtils
will be logged but not rethrown andnull
will be returned. If set tofalse
then if any objects in the property path evaluate tonull
then theIllegalArgumentException
throw byPropertyUtils
will be logged and rethrown.- Returns:
- The flag which determines whether
null
objects in the property path will genenerate anIllegalArgumentException
or not.
-