2020-04-21 18:46:45 +02:00
|
|
|
package com.baeldung.modelmapper;
|
2020-04-19 21:02:17 +02:00
|
|
|
|
|
|
|
import org.modelmapper.ModelMapper;
|
|
|
|
|
|
|
|
import java.util.List;
|
2020-04-26 18:37:31 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-04-19 21:02:17 +02:00
|
|
|
|
|
|
|
/**
|
2020-04-29 21:55:30 +02:00
|
|
|
* This is a helper class that contains method for custom mapping of the users list.
|
2020-04-25 21:57:43 +02:00
|
|
|
* Initially, an instance of ModelMapper was created.
|
2020-04-21 21:29:38 +02:00
|
|
|
*
|
2020-04-21 22:26:22 +02:00
|
|
|
* @author Sasa Milenkovic
|
2020-04-19 21:02:17 +02:00
|
|
|
*/
|
|
|
|
public class MapperUtil {
|
|
|
|
|
|
|
|
private static ModelMapper modelMapper = new ModelMapper();
|
|
|
|
|
|
|
|
|
|
|
|
private MapperUtil() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:37:31 +02:00
|
|
|
public static <S, T> List<T> mapList(List<S> source, Class<T> targetClass) {
|
2020-04-19 21:02:17 +02:00
|
|
|
|
2020-04-26 18:37:31 +02:00
|
|
|
return source
|
|
|
|
.stream()
|
|
|
|
.map(element -> modelMapper.map(element, targetClass))
|
|
|
|
.collect(Collectors.toList());
|
2020-04-19 21:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|