From 4bb9f166b6df24f11549f9f2627aaf26fbb9e7b6 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sun, 19 Apr 2020 21:02:17 +0200 Subject: [PATCH] Source project for the draft article --- model-mapper/src/Main.java | 58 +++++++++++++++ model-mapper/src/com/baeldung/model/User.java | 70 +++++++++++++++++++ .../src/com/baeldung/model/UserDTO.java | 49 +++++++++++++ .../src/com/baeldung/model/UserList.java | 21 ++++++ .../src/com/baeldung/util/MapperUtil.java | 47 +++++++++++++ .../com/baeldung/util/UserPropertyMap.java | 36 ++++++++++ 6 files changed, 281 insertions(+) create mode 100644 model-mapper/src/Main.java create mode 100644 model-mapper/src/com/baeldung/model/User.java create mode 100644 model-mapper/src/com/baeldung/model/UserDTO.java create mode 100644 model-mapper/src/com/baeldung/model/UserList.java create mode 100644 model-mapper/src/com/baeldung/util/MapperUtil.java create mode 100644 model-mapper/src/com/baeldung/util/UserPropertyMap.java diff --git a/model-mapper/src/Main.java b/model-mapper/src/Main.java new file mode 100644 index 0000000000..a6deb49168 --- /dev/null +++ b/model-mapper/src/Main.java @@ -0,0 +1,58 @@ +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 integers = new ArrayList(); + + integers.add(1); + integers.add(2); + integers.add(3); + + List characters = mapper.map(integers, new TypeToken>() {}.getType()); + + System.out.println("Character list: " + characters); + + // Mapping lists using generic type methods + + List 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 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); + + } +} diff --git a/model-mapper/src/com/baeldung/model/User.java b/model-mapper/src/com/baeldung/model/User.java new file mode 100644 index 0000000000..73e4baafb5 --- /dev/null +++ b/model-mapper/src/com/baeldung/model/User.java @@ -0,0 +1,70 @@ +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; + } + + +} diff --git a/model-mapper/src/com/baeldung/model/UserDTO.java b/model-mapper/src/com/baeldung/model/UserDTO.java new file mode 100644 index 0000000000..ed056ace8c --- /dev/null +++ b/model-mapper/src/com/baeldung/model/UserDTO.java @@ -0,0 +1,49 @@ +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 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 getUsernames() { + return usernames; + } + + public void setUsernames(List usernames) { + this.usernames = usernames; + } +} diff --git a/model-mapper/src/com/baeldung/model/UserList.java b/model-mapper/src/com/baeldung/model/UserList.java new file mode 100644 index 0000000000..b30d5507d5 --- /dev/null +++ b/model-mapper/src/com/baeldung/model/UserList.java @@ -0,0 +1,21 @@ +package com.baeldung.model; + +import java.util.Collection; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class UserList { + + private Collection users; + + public Collection getUsers() { + return users; + } + + public void setUsers(Collection users) { + this.users = users; + } +} diff --git a/model-mapper/src/com/baeldung/util/MapperUtil.java b/model-mapper/src/com/baeldung/util/MapperUtil.java new file mode 100644 index 0000000000..fe10b7777d --- /dev/null +++ b/model-mapper/src/com/baeldung/util/MapperUtil.java @@ -0,0 +1,47 @@ +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 T mapTo(final S source, final Class target) { + + return modelMapper.map(source, target); + } + + public static List mapList(final List sourceList, final Class target) { + + List targetList = new ArrayList(); + + for (S source : sourceList) { + + targetList.add(modelMapper.map(source, target)); + } + + return targetList; + } + +} diff --git a/model-mapper/src/com/baeldung/util/UserPropertyMap.java b/model-mapper/src/com/baeldung/util/UserPropertyMap.java new file mode 100644 index 0000000000..4346174440 --- /dev/null +++ b/model-mapper/src/com/baeldung/util/UserPropertyMap.java @@ -0,0 +1,36 @@ +package com.baeldung.util; + +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; +import java.util.ArrayList; +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class UserPropertyMap extends PropertyMap { + + + Converter, List> converter = new AbstractConverter, List>() { + + List usernames = new ArrayList<>(); + + protected List convert(List users) { + + users.forEach(user -> usernames.add(user.getUserName())); + return usernames; + } + + }; + + @Override + protected void configure() { + using(converter).map(source.getUsers(), destination.getUsernames()); + } +}