Apparently they removed BeanToPropertyValueTransformer
from the release of apache-commons-collection4
.
I managed to achieve the same behavior by defininig a custom Transformer
. The introduction of generics eliminates the necessity of casting the output collection:
Collection<MyInputType> myCollection = ...
Collection<MyType> myTypes = CollectionUtils.collect(myCollection, new Transformer<MyInputType, MyType>() {
@Override
public MyType transform(MyInputType input) {
return input.getMyProperty();
}
}
You could also write your own Transformer
that uses reflection
class ReflectionTransformer<O>
implements
Transformer<Object, O> {
private String reflectionString;
public ReflectionTransformer(String reflectionString) {
this.reflectionString = reflectionString;
}
@SuppressWarnings("unchecked")
@Override
public O transform(
Object input) {
try {
return (O) BeanUtils.getProperty(input, reflectionString);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
and use it same as you used to do with BeanToPropertyValueTransformer
Collection<MyType> myTypes = CollectionUtils.collect(myCollection, new ReflectionTransformer<MyType>("myProperty"));