Moving com.baeldung.modelmmaper package to java-collections-conversations module
This commit is contained in:
parent
0a42d945f8
commit
43852c4303
|
@ -19,6 +19,23 @@
|
|||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.modelmapper</groupId>
|
||||
<artifactId>modelmapper</artifactId>
|
||||
<version>${modelmapper.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.util;
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
|
@ -8,7 +8,9 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
* @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 {
|
||||
|
||||
|
@ -28,16 +30,13 @@ public class 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));
|
||||
}
|
||||
|
|
@ -1,10 +1,8 @@
|
|||
package com.baeldung.model;
|
||||
|
||||
import java.util.List;
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
* @description User model entity class
|
||||
*/
|
||||
public class User {
|
||||
|
|
@ -1,17 +1,14 @@
|
|||
package com.baeldung.model;
|
||||
|
||||
import java.util.List;
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
* @description UserDTO model class
|
||||
*/
|
||||
public class UserDTO {
|
||||
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String email;
|
||||
private List<String> usernames;
|
||||
|
||||
// getters and setters
|
||||
|
||||
|
@ -39,11 +36,5 @@ public class UserDTO {
|
|||
this.email = email;
|
||||
}
|
||||
|
||||
public List<String> getUsernames() {
|
||||
return usernames;
|
||||
}
|
||||
|
||||
public void setUsernames(List<String> usernames) {
|
||||
this.usernames = usernames;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
package com.baeldung.model;
|
||||
package com.baeldung.modelmapper;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author sasam0320
|
||||
* @date 4/18/2020
|
||||
* @description UserList class that contain collection of users
|
||||
*/
|
||||
|
||||
public class UserList {
|
||||
|
||||
private Collection<User> 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
|
@ -45,6 +45,9 @@
|
|||
<guava.version>23.0</guava.version>
|
||||
<commons.io.version>2.6</commons.io.version>
|
||||
<jmh.version>1.19</jmh.version>
|
||||
<modelmapper.version>2.3.6</modelmapper.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<hamcrest.version>1.3</hamcrest.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue