Creating TypeMap to use property mapping and converter class
This commit is contained in:
parent
c2e64bea72
commit
9e6dd6f41b
|
@ -1,37 +0,0 @@
|
||||||
package com.baeldung.modelmapper;
|
|
||||||
|
|
||||||
import org.modelmapper.AbstractConverter;
|
|
||||||
import org.modelmapper.Converter;
|
|
||||||
import org.modelmapper.PropertyMap;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* UserPropertyMap class instantiates the converter to map the data from the UserList to the UsersLisDTO.
|
|
||||||
* In the configuration method, we call a converter to do the mapping.
|
|
||||||
*
|
|
||||||
* @author Sasa Milenkovic
|
|
||||||
*/
|
|
||||||
public class UserPropertyMap extends PropertyMap<UserList, UserListDTO> {
|
|
||||||
|
|
||||||
|
|
||||||
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected List<String> convert(List<User> users) {
|
|
||||||
|
|
||||||
return users
|
|
||||||
.stream()
|
|
||||||
.map(User::getUsername)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void configure() {
|
|
||||||
|
|
||||||
using(converter).map(source.getUsers(), destination.getUsernames());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue