Refactoring code in com.baeldung.modelmapper package
This commit is contained in:
parent
32d1cc8a13
commit
a5264182cd
|
@ -1,39 +1,27 @@
|
|||
package com.baeldung.modelmapper;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This is a helper class that contains methods for generic mapping of the users list.
|
||||
* Initially, an instance of ModelMapper was created. In the static block we set the matching configuration to STRICT.
|
||||
* Initially, an instance of ModelMapper was created.
|
||||
*
|
||||
* @author Sasa Milenkovic
|
||||
*/
|
||||
public class MapperUtil {
|
||||
|
||||
|
||||
private static ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
|
||||
static {
|
||||
|
||||
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||
|
||||
}
|
||||
|
||||
private MapperUtil() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static <S, T> T mapTo(final S source, final Class<T> target) {
|
||||
return modelMapper.map(source, target);
|
||||
}
|
||||
|
||||
public static <S, T> List<T> mapList(final List<S> sourceList, final Class<T> target) {
|
||||
public static <S, T> List<T> mapList(List<S> sourceList, Class<T> target) {
|
||||
List<T> targetList = new ArrayList<T>();
|
||||
|
||||
for (S source : sourceList) {
|
||||
|
|
|
@ -8,7 +8,7 @@ package com.baeldung.modelmapper;
|
|||
public class User {
|
||||
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String username;
|
||||
private String email;
|
||||
private String contactNumber;
|
||||
private String userType;
|
||||
|
@ -18,9 +18,9 @@ public class User {
|
|||
public User() {
|
||||
}
|
||||
|
||||
public User(String userId, String userName, String email, String contactNumber, String userType) {
|
||||
public User(String userId, String username, String email, String contactNumber, String userType) {
|
||||
this.userId = userId;
|
||||
this.userName = userName;
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
this.contactNumber = contactNumber;
|
||||
this.userType = userType;
|
||||
|
@ -34,12 +34,12 @@ public class User {
|
|||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
public void setUsername(String userName) {
|
||||
this.username = userName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
|
|
|
@ -8,7 +8,7 @@ package com.baeldung.modelmapper;
|
|||
public class UserDTO {
|
||||
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String username;
|
||||
private String email;
|
||||
|
||||
// getters and setters
|
||||
|
@ -21,12 +21,12 @@ public class UserDTO {
|
|||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
|
|
|
@ -18,14 +18,15 @@ public class UserPropertyMap extends PropertyMap<UserList, UserListDTO> {
|
|||
|
||||
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
|
||||
|
||||
List<String> usernames = new ArrayList<>();
|
||||
protected List<String> usernames;
|
||||
|
||||
@Override
|
||||
protected List<String> convert(List<User> users) {
|
||||
|
||||
users.forEach(user -> usernames.add(user.getUserName()));
|
||||
usernames = new ArrayList<String>();
|
||||
users.forEach(user -> usernames.add(user.getUsername()));
|
||||
return usernames;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
|
|
|
@ -25,14 +25,14 @@ import static org.junit.Assert.assertThat;
|
|||
*/
|
||||
public class UsersListMappingUnitTest {
|
||||
|
||||
private ModelMapper mapper;
|
||||
private ModelMapper modelMapper;
|
||||
private List<User> users;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
|
||||
mapper = new ModelMapper();
|
||||
mapper.addMappings(new UserPropertyMap());
|
||||
modelMapper = new ModelMapper();
|
||||
modelMapper.addMappings(new UserPropertyMap());
|
||||
users = new ArrayList();
|
||||
users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER"));
|
||||
users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER"));
|
||||
|
@ -49,7 +49,7 @@ public class UsersListMappingUnitTest {
|
|||
integers.add(2);
|
||||
integers.add(3);
|
||||
|
||||
List<Character> characters = mapper.map(integers, new TypeToken<List<Character>>() {
|
||||
List<Character> characters = modelMapper.map(integers, new TypeToken<List<Character>>() {
|
||||
}.getType());
|
||||
|
||||
assertThat(characters, hasItems('1', '2', '3'));
|
||||
|
@ -66,14 +66,14 @@ public class UsersListMappingUnitTest {
|
|||
assertThat(userDtoList, Matchers.<UserDTO>hasItem(
|
||||
Matchers.both(hasProperty("userId", equalTo("b100")))
|
||||
.and(hasProperty("email", equalTo("user1@baeldung.com")))
|
||||
.and(hasProperty("userName", equalTo("user1")))));
|
||||
.and(hasProperty("username", equalTo("user1")))));
|
||||
|
||||
// Mapping lists using PropertyMap and Converter
|
||||
|
||||
UserList userList = new UserList();
|
||||
userList.setUsers(users);
|
||||
UserListDTO dto = new UserListDTO();
|
||||
mapper.map(userList, dto);
|
||||
modelMapper.map(userList, dto);
|
||||
|
||||
assertNotNull(dto);
|
||||
assertThat(dto, Matchers.hasProperty("usernames"));
|
||||
|
|
Loading…
Reference in New Issue