Reputation: 14458
How to call this pattern, and is there any existing apache-commons utility for this:
class Person {
String getName();
}
List<Person> persons = ...;
// create a dynamic bean on the fly, which can be used as:
Object personXxxx = transformListOfBeans(Person.class, persons);
// so each of the bean properties now returns the list of the original property:
List<String> personNames = personXxxx.name;
// i.e. the transformation creates a new "type":
class PersonXxxx {
List<String> getName();
}
How to call this kind of transformation? A proxy should keep the method signatures. So it is not a proxy, neither decorator.
Well, I can simply rename the generated property names to plural form, like:
personXxxx.names
This is no problem. I want to know if such pattern was already known so I don't have to choose the appropriate words myself.
Upvotes: 1
Views: 98
Reputation: 7496
I don't think there is a pattern as such, but guava provides a simple way of transforming a collection of one to another. For the example:
List<Person> persons = ...;
Iterable<String> names = Iterables.transform(persons, new Function<Person, String>() {
@Override
public String apply(Person person) {
return person.getName();
}
});
Upvotes: 1
Reputation: 15729
A possibility is to declare an interface for all the methods, e.g. (leading I for clarity) INamable, IAgeable, ISexable etc. Cast the List<Person> to List<ISomething>.
Obviously this has issues.
Upvotes: 0
Reputation: 3525
I don't think a tool exists for this. You have to code it :
List<String> names = new ArrayList<String>(persons.size());
for (Person person : persons) {
names.add(person.getName());
}
Maybe you can use introspection to be more generic, but code is really simple.
Upvotes: 1