Customizing Java 8 code in modemmapper package
This commit is contained in:
parent
a5264182cd
commit
8d7d98a144
|
@ -2,11 +2,11 @@ package com.baeldung.modelmapper;
|
|||
|
||||
import org.modelmapper.ModelMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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.
|
||||
*
|
||||
* @author Sasa Milenkovic
|
||||
|
@ -21,14 +21,12 @@ public class MapperUtil {
|
|||
|
||||
}
|
||||
|
||||
public static <S, T> List<T> mapList(List<S> sourceList, Class<T> target) {
|
||||
List<T> targetList = new ArrayList<T>();
|
||||
public static <S, T> List<T> mapList(List<S> source, Class<T> targetClass) {
|
||||
|
||||
for (S source : sourceList) {
|
||||
targetList.add(modelMapper.map(source, target));
|
||||
}
|
||||
|
||||
return targetList;
|
||||
return source
|
||||
.stream()
|
||||
.map(element -> modelMapper.map(element, targetClass))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,11 +4,11 @@ import org.modelmapper.AbstractConverter;
|
|||
import org.modelmapper.Converter;
|
||||
import org.modelmapper.PropertyMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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.
|
||||
*
|
||||
* @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>>() {
|
||||
|
||||
protected List<String> usernames;
|
||||
|
||||
@Override
|
||||
protected List<String> convert(List<User> users) {
|
||||
|
||||
usernames = new ArrayList<String>();
|
||||
users.forEach(user -> usernames.add(user.getUsername()));
|
||||
return usernames;
|
||||
return users
|
||||
.stream()
|
||||
.map(User::getUsername)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
|
||||
using(converter).map(source.getUsers(), destination.getUsernames());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public class UsersListMappingUnitTest {
|
|||
@Test
|
||||
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);
|
||||
|
||||
|
@ -72,12 +72,12 @@ public class UsersListMappingUnitTest {
|
|||
|
||||
UserList userList = new UserList();
|
||||
userList.setUsers(users);
|
||||
UserListDTO dto = new UserListDTO();
|
||||
modelMapper.map(userList, dto);
|
||||
UserListDTO dtos = new UserListDTO();
|
||||
modelMapper.map(userList, dtos);
|
||||
|
||||
assertNotNull(dto);
|
||||
assertThat(dto, Matchers.hasProperty("usernames"));
|
||||
assertThat(dto.getUsernames(), hasSize(3));
|
||||
assertNotNull(dtos);
|
||||
assertThat(dtos, Matchers.hasProperty("usernames"));
|
||||
assertThat(dtos.getUsernames(), hasSize(3));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue