Customizing Java 8 code in modemmapper package

This commit is contained in:
Sasa M 2020-04-26 18:37:31 +02:00
parent a5264182cd
commit 8d7d98a144
3 changed files with 20 additions and 21 deletions

View File

@ -2,11 +2,11 @@ package com.baeldung.modelmapper;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* This is a helper class that contains methods for generic mapping of the users list. * This is a helper class that contains method for generic mapping of the users list.
* Initially, an instance of ModelMapper was created. * Initially, an instance of ModelMapper was created.
* *
* @author Sasa Milenkovic * @author Sasa Milenkovic
@ -21,14 +21,12 @@ public class MapperUtil {
} }
public static <S, T> List<T> mapList(List<S> sourceList, Class<T> target) { public static <S, T> List<T> mapList(List<S> source, Class<T> targetClass) {
List<T> targetList = new ArrayList<T>();
for (S source : sourceList) { return source
targetList.add(modelMapper.map(source, target)); .stream()
} .map(element -> modelMapper.map(element, targetClass))
.collect(Collectors.toList());
return targetList;
} }
} }

View File

@ -4,11 +4,11 @@ import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter; import org.modelmapper.Converter;
import org.modelmapper.PropertyMap; import org.modelmapper.PropertyMap;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. * 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. * In the configuration method, we call a converter to do the mapping.
* *
* @author Sasa Milenkovic * @author Sasa Milenkovic
@ -18,19 +18,20 @@ public class UserPropertyMap extends PropertyMap<UserList, UserListDTO> {
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() { Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
protected List<String> usernames;
@Override @Override
protected List<String> convert(List<User> users) { protected List<String> convert(List<User> users) {
usernames = new ArrayList<String>(); return users
users.forEach(user -> usernames.add(user.getUsername())); .stream()
return usernames; .map(User::getUsername)
.collect(Collectors.toList());
} }
}; };
@Override @Override
protected void configure() { protected void configure() {
using(converter).map(source.getUsers(), destination.getUsernames()); using(converter).map(source.getUsers(), destination.getUsernames());
} }
} }

View File

@ -59,7 +59,7 @@ public class UsersListMappingUnitTest {
@Test @Test
public void givenUsersList_whenUseGenericType_thenMapToDto() { public void givenUsersList_whenUseGenericType_thenMapToDto() {
// Mapping lists using generic type methods // Mapping lists using custom type methods
List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class); List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class);
@ -72,12 +72,12 @@ public class UsersListMappingUnitTest {
UserList userList = new UserList(); UserList userList = new UserList();
userList.setUsers(users); userList.setUsers(users);
UserListDTO dto = new UserListDTO(); UserListDTO dtos = new UserListDTO();
modelMapper.map(userList, dto); modelMapper.map(userList, dtos);
assertNotNull(dto); assertNotNull(dtos);
assertThat(dto, Matchers.hasProperty("usernames")); assertThat(dtos, Matchers.hasProperty("usernames"));
assertThat(dto.getUsernames(), hasSize(3)); assertThat(dtos.getUsernames(), hasSize(3));
} }