Moving com.baeldung.modelmmaper package to java-collections-conversations module
This commit is contained in:
parent
4bb9f166b6
commit
b9cbfa49c4
|
@ -1,8 +1,5 @@
|
|||
package com.baeldung.util;
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
import com.baeldung.model.UserDTO;
|
||||
import com.baeldung.model.UserList;
|
||||
import org.modelmapper.AbstractConverter;
|
||||
import org.modelmapper.Converter;
|
||||
import org.modelmapper.PropertyMap;
|
||||
|
@ -11,10 +8,11 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
* @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, UserDTO> {
|
||||
public class UserPropertyMap extends PropertyMap<UserList, UserListDTO> {
|
||||
|
||||
|
||||
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
|
|
@ -0,0 +1,85 @@
|
|||
package com.baeldung.modelmapper;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.hamcrest.Matchers.hasProperty;
|
||||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
||||
/**
|
||||
* @sasam0320
|
||||
* @description
|
||||
* This class has test methods of mapping Integer to Character list,
|
||||
* mapping user list to DTO list using MapperUtil generic methods and Converter
|
||||
*/
|
||||
public class UserMappingTest {
|
||||
|
||||
private ModelMapper mapper;
|
||||
private List<User> users;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
|
||||
mapper = new ModelMapper();
|
||||
mapper.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"));
|
||||
users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapIntegerList() {
|
||||
|
||||
List<Integer> integers = new ArrayList<Integer>();
|
||||
|
||||
integers.add(1);
|
||||
integers.add(2);
|
||||
integers.add(3);
|
||||
|
||||
List<Character> characters = mapper.map(integers, new TypeToken<List<Character>>() {
|
||||
}.getType());
|
||||
|
||||
assertThat(characters, hasItems('1','2','3'));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapGenericTypeLists() {
|
||||
|
||||
// Mapping lists using generic type methods
|
||||
|
||||
List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class);
|
||||
|
||||
assertThat(userDtoList, Matchers.<UserDTO>hasItem(
|
||||
Matchers.both(hasProperty("userId", equalTo("b100")))
|
||||
.and(hasProperty("email", equalTo("user1@baeldung.com")))
|
||||
.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);
|
||||
|
||||
assertNotNull(dto);
|
||||
assertThat(dto, Matchers.hasProperty("usernames"));
|
||||
assertThat(dto.getUsernames(), hasSize(3));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
import com.baeldung.model.User;
|
||||
import com.baeldung.model.UserDTO;
|
||||
import com.baeldung.model.UserList;
|
||||
import com.baeldung.util.MapperUtil;
|
||||
import com.baeldung.util.UserPropertyMap;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
*/
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//Instantiate ModelMapper
|
||||
|
||||
ModelMapper mapper = new ModelMapper();
|
||||
mapper.addMappings(new UserPropertyMap());
|
||||
|
||||
// Mapping lists using TypeToken generic class
|
||||
|
||||
List<Integer> integers = new ArrayList<Integer>();
|
||||
|
||||
integers.add(1);
|
||||
integers.add(2);
|
||||
integers.add(3);
|
||||
|
||||
List<Character> characters = mapper.map(integers, new TypeToken<List<Character>>() {}.getType());
|
||||
|
||||
System.out.println("Character list: " + characters);
|
||||
|
||||
// Mapping lists using generic type methods
|
||||
|
||||
List<User> 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"));
|
||||
users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN"));
|
||||
|
||||
List<UserDTO> userDtoList = MapperUtil.mapList(users, UserDTO.class);
|
||||
userDtoList.stream().map(userDto -> userDto.getEmail()).forEachOrdered(System.out::println);
|
||||
|
||||
// Mapping lists using PropertyMap and Converter
|
||||
|
||||
UserList userList = new UserList();
|
||||
userList.setUsers(users);
|
||||
UserDTO dto = new UserDTO();
|
||||
|
||||
mapper.map(userList, dto);
|
||||
dto.getUsernames().forEach(System.out::println);
|
||||
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package com.baeldung.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package com.baeldung.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
*/
|
||||
public class UserDTO {
|
||||
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String email;
|
||||
private List<String> usernames;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
public List<String> getUsernames() {
|
||||
return usernames;
|
||||
}
|
||||
|
||||
public void setUsernames(List<String> usernames) {
|
||||
this.usernames = usernames;
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.model;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
*/
|
||||
|
||||
public class UserList {
|
||||
|
||||
private Collection<User> users;
|
||||
|
||||
public Collection<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(Collection<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package com.baeldung.util;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue