Moving com.baeldung.modelmmaper package to java-collections-conversations module
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @description
|
||||
* 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.
|
||||
*/
|
||||
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) {
|
||||
List<T> targetList = new ArrayList<T>();
|
||||
|
||||
for (S source : sourceList) {
|
||||
targetList.add(modelMapper.map(source, target));
|
||||
}
|
||||
|
||||
return targetList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @description User model entity class
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String email;
|
||||
private String contactNumber;
|
||||
private String userType;
|
||||
|
||||
// Standard constructors, getters and setters
|
||||
|
||||
public User(){}
|
||||
|
||||
public User(String userId, String userName, String email, String contactNumber, String userType) {
|
||||
this.userId = userId;
|
||||
this.userName = userName;
|
||||
this.email = email;
|
||||
this.contactNumber = contactNumber;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getContactNumber() {
|
||||
return contactNumber;
|
||||
}
|
||||
|
||||
public void setContactNumber(String contactNumber) {
|
||||
this.contactNumber = contactNumber;
|
||||
}
|
||||
|
||||
public String getUserType() {
|
||||
return userType;
|
||||
}
|
||||
|
||||
public void setUserType(String userType) {
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @description UserDTO model class
|
||||
*/
|
||||
public class UserDTO {
|
||||
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String email;
|
||||
|
||||
// getters and setters
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @description UserList class that contain collection of users
|
||||
*/
|
||||
public class UserList {
|
||||
|
||||
private Collection<User> users;
|
||||
|
||||
public Collection<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(Collection<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @description UserListDTO class that contain list of username properties
|
||||
*/
|
||||
public class UserListDTO {
|
||||
|
||||
private List<String> usernames;
|
||||
|
||||
public List<String> getUsernames() {
|
||||
return usernames;
|
||||
}
|
||||
|
||||
public void setUsernames(List<String> usernames) {
|
||||
this.usernames = usernames;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import org.modelmapper.AbstractConverter;
|
||||
import org.modelmapper.Converter;
|
||||
import org.modelmapper.PropertyMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @description
|
||||
* UserPropertyMap class instantiates the converter to map the data from the user list to the user name list.
|
||||
* In the configuration method, we call a converter to do the mapping.
|
||||
*/
|
||||
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> convert(List<User> users) {
|
||||
|
||||
users.forEach(user -> usernames.add(user.getUserName()));
|
||||
return usernames;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
using(converter).map(source.getUsers(), destination.getUsernames());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user