diff --git a/java-collections-conversions-2/pom.xml b/java-collections-conversions-2/pom.xml index b04d764719..0f7cdadeb2 100644 --- a/java-collections-conversions-2/pom.xml +++ b/java-collections-conversions-2/pom.xml @@ -20,6 +20,23 @@ commons-lang3 ${commons-lang3.version} + + org.modelmapper + modelmapper + ${modelmapper.version} + + + junit + junit + ${junit.version} + test + + + org.hamcrest + hamcrest + ${hamcrest.version} + test + diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java new file mode 100644 index 0000000000..23a549e652 --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -0,0 +1,32 @@ +package com.baeldung.modelmapper; + +import org.modelmapper.ModelMapper; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * This is a helper class that contains method for custom mapping of the users list. + * Initially, an instance of ModelMapper was created. + * + * @author Sasa Milenkovic + */ +public class MapperUtil { + + private static ModelMapper modelMapper = new ModelMapper(); + + + private MapperUtil() { + + + } + + public static List mapList(List source, Class targetClass) { + + return source + .stream() + .map(element -> modelMapper.map(element, targetClass)) + .collect(Collectors.toList()); + } + +} diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java new file mode 100644 index 0000000000..8ed674d86a --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java @@ -0,0 +1,70 @@ +package com.baeldung.modelmapper; + +/** + * User model entity class + * + * @author Sasa Milenkovic + */ +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; + } + + +} diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java new file mode 100644 index 0000000000..b67bb58ef4 --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java @@ -0,0 +1,41 @@ +package com.baeldung.modelmapper; + +/** + * UserDTO model class + * + * @author Sasa Milenkovic + */ +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; + } + + +} diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java new file mode 100644 index 0000000000..7b6bed807b --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java @@ -0,0 +1,21 @@ +package com.baeldung.modelmapper; + +import java.util.Collection; + +/** + * UserList class that contain collection of users + * + * @author Sasa Milenkovic + */ +public class UserList { + + private Collection users; + + public Collection getUsers() { + return users; + } + + public void setUsers(Collection users) { + this.users = users; + } +} diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java new file mode 100644 index 0000000000..c001cbbc3c --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java @@ -0,0 +1,21 @@ +package com.baeldung.modelmapper; + +import java.util.List; + +/** + * UserListDTO class that contain list of username properties + * + * @author Sasa Milenkovic + */ +public class UserListDTO { + + private List usernames; + + public List getUsernames() { + return usernames; + } + + public void setUsernames(List usernames) { + this.usernames = usernames; + } +} diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java new file mode 100644 index 0000000000..19423713e2 --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java @@ -0,0 +1,23 @@ +package com.baeldung.modelmapper; + +import org.modelmapper.AbstractConverter; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * UsersListConverter class map the property data from the list of users into the list of user names. + * + * @author Sasa Milenkovic + */ +public class UsersListConverter extends AbstractConverter, List> { + + @Override + protected List convert(List users) { + + return users + .stream() + .map(User::getUsername) + .collect(Collectors.toList()); + } +} diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java new file mode 100644 index 0000000000..a8a72b12f7 --- /dev/null +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.modelmapper; + +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.modelmapper.ModelMapper; +import org.modelmapper.TypeMap; +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.junit.Assert.assertThat; + + +/** + * This class has test methods of mapping Integer to Character list, + * mapping users list to DTO list using MapperUtil custom type method and property mapping using converter class + * + * @author Sasa Milenkovic + */ +public class UsersListMappingUnitTest { + + private ModelMapper modelMapper; + private List users; + + @Before + public void init() { + + modelMapper = new ModelMapper(); + + TypeMap typeMap = modelMapper.createTypeMap(UserList.class, UserListDTO.class); + + typeMap.addMappings(mapper -> mapper.using(new UsersListConverter()) + .map(UserList::getUsers, UserListDTO::setUsernames)); + + 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 whenInteger_thenMapToCharacter() { + + List integers = new ArrayList(); + + integers.add(1); + integers.add(2); + integers.add(3); + + List characters = modelMapper.map(integers, new TypeToken>() { + }.getType()); + + assertThat(characters, hasItems('1', '2', '3')); + + } + + @Test + public void givenUsersList_whenUseGenericType_thenMapToUserDTO() { + + // Mapping lists using custom (generic) type mapping + + List userDtoList = MapperUtil.mapList(users, UserDTO.class); + + assertThat(userDtoList, Matchers.hasItem( + Matchers.both(hasProperty("userId", equalTo("b100"))) + .and(hasProperty("email", equalTo("user1@baeldung.com"))) + .and(hasProperty("username", equalTo("user1"))))); + + + } + + @Test + public void givenUsersList_whenUseConverter_thenMapToUsernames() { + + // Mapping lists using property mapping and converter + + UserList userList = new UserList(); + userList.setUsers(users); + UserListDTO dtos = new UserListDTO(); + modelMapper.map(userList, dtos); + + assertThat(dtos.getUsernames(), hasItems("user1", "user2", "user3")); + + } + +} \ No newline at end of file diff --git a/parent-java/pom.xml b/parent-java/pom.xml index 4828bc2abb..baad9fecf4 100644 --- a/parent-java/pom.xml +++ b/parent-java/pom.xml @@ -1,8 +1,8 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 parent-java 0.0.1-SNAPSHOT @@ -45,6 +45,9 @@ 23.0 2.6 1.19 + 2.3.7 + 4.12 + 2.2