-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathMainActivity.java
More file actions
101 lines (89 loc) · 3.81 KB
/
MainActivity.java
File metadata and controls
101 lines (89 loc) · 3.81 KB
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
96
97
98
99
100
101
package com.example.mapstructtest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.example.mapstructtest.model.AutoValueCar;
import com.example.mapstructtest.model.AutoValueCarDto;
import com.example.mapstructtest.model.AutoValueFluentCar;
import com.example.mapstructtest.model.AutoValueFluentCarDto;
import com.example.mapstructtest.model.Car;
import com.example.mapstructtest.model.CarDto;
import com.example.mapstructtest.model.CarFluentSetter;
import com.example.mapstructtest.model.CarType;
import com.example.mapstructtest.model.ImmutableConstructableCar;
import com.example.mapstructtest.model.ImmutableConstructibleCarDto;
import com.example.mapstructtest.model.mapping.AutoValueFluentMapper;
import com.example.mapstructtest.model.mapping.AutoValueMapper;
import com.example.mapstructtest.model.mapping.CarMapper;
import com.example.mapstructtest.model.mapping.ImmutableConstructableCarMapper;
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv_text);
}
@Override
protected void onResume() {
super.onResume();
StringBuilder sb = new StringBuilder();
sb.append(manualMappingWithFluentSetter());
sb.append(withAutoValue());
sb.append(withAutoValueFluent());
sb.append(withAutoValueFluent());
// sb.append(withImmutable()); // Not yet for MapStruct 1.3
tv.setText(sb.toString());
}
String withImmutable() {
// MapStruct 1.3 does not support this immutable style contstructor.
ImmutableConstructableCar immutableConstructableCar = new ImmutableConstructableCar("Immutable Audi", 6, CarType.LARGE);
ImmutableConstructibleCarDto immutableCarDto = ImmutableConstructableCarMapper.INSTANCE.carToCarDto(immutableConstructableCar);
return getString("Immutable: ", immutableConstructableCar, immutableCarDto);
}
String manualMappingWithFluentSetter() {
Car car = new Car();
car.setConstructor("Audi");
car.setNumberOfSeats(5);
car.setType(CarType.LUXURY);
// Map the regular car
CarDto carDto = CarMapper.INSTANCE.carToCarDto(car);
// Then map it via fluent setter version
CarFluentSetter carFluentSetter = CarMapper.INSTANCE.toFluentSetterCar(carDto);
CarDto carDtoAgain = CarMapper.INSTANCE.toDto(carFluentSetter);
return getString("Car and car with fluent setter", car, carDto, carFluentSetter, carDtoAgain);
}
String withAutoValue() {
AutoValueCar.Builder builder = AutoValueCar.builder();
builder.setConstructor("Audi");
builder.setNumberOfSeats(5);
builder.setNumberOfAirbags(3);
builder.setType(CarType.LUXURY);
AutoValueCar car = builder.build();
AutoValueCarDto carDto = AutoValueMapper.INSTANCE.toDto(car);
AutoValueCar carAgain = AutoValueMapper.INSTANCE.toModel(carDto);
return getString("AutoValue mapping with MapStruct", car, carDto, carAgain);
}
String withAutoValueFluent() {
AutoValueFluentCar.Builder builder = AutoValueFluentCar.builder();
builder.constructor("Audi");
builder.numberOfSeats(5);
builder.type(CarType.LUXURY);
AutoValueFluentCar car = builder.build();
// MapStruct 1.3 does NOT work with fluent accessor style for getters, but does work with fluent setters
AutoValueFluentCarDto carDto = AutoValueFluentMapper.INSTANCE.toDto(car);
AutoValueFluentCar carAgain = AutoValueFluentMapper.INSTANCE.toModel(carDto);
return getString("AutoValue with fluent setter mapping", car, carDto, carAgain);
}
private String getString(String info, Object... objs) {
StringBuilder strb = new StringBuilder();
strb.append(info);
for (Object obj : objs) {
strb.append('\n');
strb.append(obj.toString());
}
strb.append('\n');
strb.append('\n');
return strb.toString();
}
}