Refactoring code in com.baeldung.modelmapper package
This commit is contained in:
+2
-14
@@ -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() {
|
||||
|
||||
+4
-3
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user