12

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 ?

Bick
  • 17,833
  • 52
  • 146
  • 251
  • You could you Guava and Lists.transform. [link](http://stackoverflow.com/questions/7383624/how-to-transform-listx-to-another-listy) – komfitura Feb 13 '14 at 11:51
  • Thanks. But orika is byte code handled which seems faster. – Bick Feb 13 '14 at 12:39
  • Plus I find it hard to use a framework or library with this at the top of its site https://code.google.com/p/guava-libraries/wiki/FunctionalExplained - "Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava, and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps" – Bick Feb 13 '14 at 12:40

3 Answers3

16

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

André
  • 2,184
  • 1
  • 22
  • 30
  • 5
    Yup the above answer is correct. You can just use `return mapper.mapAsList(basicPerson, PersonDTO.class);` Should be accepted. – Akhil K Nambiar May 11 '14 at 05:13
  • It converts the date to `longValue` instead of giving it a proper date format. https://stackoverflow.com/questions/55141817/date-format-is-changing-in-dto-but-not-in-listdto-on-returning-through-respon – Farrukh Chishti Mar 13 '19 at 13:18
  • 1
    Given the question nature, you linked, this has nothing to do with Orika, but with Jackson. As I far as I can tell, successfully mapping the source to the destination class. – André Mar 14 '19 at 02:11
2

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.

isaolmez
  • 1,015
  • 11
  • 14
-1
package com.miya.takeaway.common.util.orika;

import com.miya.takeaway.common.util.orika.converter.ConverterHelper;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;

public class OrikaUtils {

    private static MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();


    public static <T> List<T> map(List<?> objects, Class<T> target) {
        if (CollectionUtils.isEmpty(objects)) {
            return Collections.EMPTY_LIST;
        }
        return mapperFactory.getMapperFacade().mapAsList(objects.toArray(), target);
    }
}