/
ComplexConversion.java
95 lines (76 loc) · 2.71 KB
/
ComplexConversion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.modelmapper;
import static org.testng.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.modelmapper.config.Configuration.AccessLevel;
import org.modelmapper.spi.MappingContext;
public class ComplexConversion {
public static class PaymentInfo {
int id;
String type;
PaymentInfo(int id, String type) {
this.id = id;
this.type = type;
}
}
public static class Customer {
List<PaymentInfo> info;
List<PaymentInfo> getInfo() {
return info;
}
}
public static class BillingInfoDTO {
int id;
}
public static class ShippingInfoDTO {
int id;
}
public static class CustomerDTO {
List<BillingInfoDTO> billingInfo;
List<ShippingInfoDTO> shippingInfo;
void setBillingInfo(List<BillingInfoDTO> billingInfo) {
this.billingInfo = billingInfo;
}
void setShippingInfo(List<ShippingInfoDTO> shippingInfo) {
this.shippingInfo = shippingInfo;
}
}
static class PaymentInfoConverter implements Converter<List<PaymentInfo>, List<Object>> {
private boolean billing;
PaymentInfoConverter(boolean billing) {
this.billing = billing;
}
public List<Object> convert(MappingContext<List<PaymentInfo>, List<Object>> context) {
List<PaymentInfo> payments = new ArrayList<PaymentInfo>();
for (PaymentInfo p : context.getSource())
if (!billing ^ "Billing".equals(p.type))
payments.add(p);
return context.getMappingEngine().map(context.create(payments, context.getDestinationType()));
}
}
public static void main(String... args) throws Exception {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(AccessLevel.PACKAGE_PRIVATE);
modelMapper.addMappings(new PropertyMap<Customer, CustomerDTO>() {
@Override
protected void configure() {
using(new PaymentInfoConverter(true)).map(source.getInfo()).setBillingInfo(null);
using(new PaymentInfoConverter(false)).map(source.getInfo()).setShippingInfo(null);
}
});
PaymentInfo info1 = new PaymentInfo(1, "Billing");
PaymentInfo info2 = new PaymentInfo(2, "Shipping");
PaymentInfo info3 = new PaymentInfo(3, "Billing");
PaymentInfo info4 = new PaymentInfo(4, "Shipping");
Customer customer = new Customer();
customer.info = Arrays.asList(info1, info2, info3, info4);
CustomerDTO dto = modelMapper.map(customer, CustomerDTO.class);
assertEquals(dto.billingInfo.get(0).id, 1);
assertEquals(dto.billingInfo.get(1).id, 3);
assertEquals(dto.shippingInfo.get(0).id, 2);
assertEquals(dto.shippingInfo.get(1).id, 4);
}
}