From a5264182cd0faed8a9cae5368ea84d1372ad3be2 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sat, 25 Apr 2020 21:57:43 +0200 Subject: [PATCH] Refactoring code in com.baeldung.modelmapper package --- .../com/baeldung/modelmapper/MapperUtil.java | 16 ++-------------- .../main/java/com/baeldung/modelmapper/User.java | 14 +++++++------- .../java/com/baeldung/modelmapper/UserDTO.java | 10 +++++----- .../baeldung/modelmapper/UserPropertyMap.java | 7 ++++--- .../modelmapper/UsersListMappingUnitTest.java | 12 ++++++------ 5 files changed, 24 insertions(+), 35 deletions(-) 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 index 546e415755..60af44cc76 100644 --- 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 @@ -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 T mapTo(final S source, final Class target) { - return modelMapper.map(source, target); - } - - public static List mapList(final List sourceList, final Class target) { + public static List mapList(List sourceList, Class target) { List targetList = new ArrayList(); for (S source : sourceList) { 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 index 23a424dedd..8ed674d86a 100644 --- 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 @@ -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() { 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 index e31414b629..b67bb58ef4 100644 --- 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 @@ -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() { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index d2c32a307d..0d2ebf7b4c 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -18,14 +18,15 @@ public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { - List usernames = new ArrayList<>(); + protected List usernames; + @Override protected List convert(List users) { - users.forEach(user -> usernames.add(user.getUserName())); + usernames = new ArrayList(); + users.forEach(user -> usernames.add(user.getUsername())); return usernames; } - }; @Override 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 index 05792fda38..aaeb8711a8 100644 --- 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 @@ -25,14 +25,14 @@ import static org.junit.Assert.assertThat; */ public class UsersListMappingUnitTest { - private ModelMapper mapper; + private ModelMapper modelMapper; private List users; @Before public void init() { - mapper = new ModelMapper(); - mapper.addMappings(new UserPropertyMap()); + modelMapper = new ModelMapper(); + modelMapper.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")); @@ -49,7 +49,7 @@ public class UsersListMappingUnitTest { integers.add(2); integers.add(3); - List characters = mapper.map(integers, new TypeToken>() { + List characters = modelMapper.map(integers, new TypeToken>() { }.getType()); assertThat(characters, hasItems('1', '2', '3')); @@ -66,14 +66,14 @@ public class UsersListMappingUnitTest { assertThat(userDtoList, Matchers.hasItem( Matchers.both(hasProperty("userId", equalTo("b100"))) .and(hasProperty("email", equalTo("user1@baeldung.com"))) - .and(hasProperty("userName", equalTo("user1"))))); + .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); + modelMapper.map(userList, dto); assertNotNull(dto); assertThat(dto, Matchers.hasProperty("usernames"));