Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orika - list to list conversion

Tags:

java

orika

This is probably an easy one, but I cant find it in the docs. I have a person class

class BasicPerson {
   private String name;
   private int age;
   private Date birthDate;
   // getters/setters omitted
}

and a list of it

ArrayList<Person>

I want to change them to change them to

ArrayList<PersonDTO>

but with out an explicit loop. Is there a way to use MapperFacade.map for a list to list one line conversion ?

like image 528
Bick Avatar asked Feb 13 '14 11:02

Bick


2 Answers

It has this functionality built-in. Did you tried using the method

List<D> ma.glasnost.orika.impl.ConfigurableMapper.mapAsList(Iterable<S> source, Class<D> destinationClass)?

I tried to find a updated version of the Javadoc, but here is one of the 1.3.5. The current version is 1.4.5. MapperFacade Class

like image 141
André Avatar answered Sep 27 '22 16:09

André


If you use the MapperFacade interface, Orika can perform the mapping multiple times on the collection:

final MapperFacade mapperFacade = mapperFactory.getMapperFacade();
final List<Person> people = // Get the person instances
final List<PersonDto> personDtos = mapperFacade.mapAsList(people, PersonDto.class);

On the other hand, if you use the BoundMapperFacade interface, it doesn't contain such a convenience method.

And lastly, if you choose to use the ConfigurableMapper approach, it also includes a mapAsList method which in fact delegates to the MapperFacade.mapAsList method.

like image 31
isaolmez Avatar answered Sep 27 '22 15:09

isaolmez