Skip to content

Commit 7db61d3

Browse files
committed
changed mapping method to have one less parameter
1 parent af0c6af commit 7db61d3

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "Launch Current File",
10+
"request": "launch",
11+
"mainClass": "${file}",
12+
"vmArgs": "--add-opens=java.base/java.lang=ALL-UNNAMED"
13+
}
14+
]
15+
}

src/main/java/info/unterrainer/commons/serialization/objectmapper/ObjectMapper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ public <A, B> void registerMapping(@NonNull final Class<A> sourceType, @NonNull
3737
mappings.put(new MappingKey<>(targetType, sourceType), back);
3838
}
3939

40-
public <S, T> T map(@NonNull final Class<S> sourceType, @NonNull final Class<T> targetType,
40+
public <S, T> T map(@NonNull final Class<T> targetType,
4141
@NonNull final S source) {
42-
return map(sourceType, targetType, source, null);
42+
return map(targetType, source, null);
4343
}
4444

45-
public <S, T> T map(@NonNull final Class<S> sourceType, @NonNull final Class<T> targetType, @NonNull final S source,
45+
@SuppressWarnings("unchecked")
46+
public <S, T> T map(@NonNull final Class<T> targetType, @NonNull final S source,
4647
final T target) {
48+
final Class<S> sourceType = (Class<S>) source.getClass();
4749
T result = mapWithJMapper(sourceType, targetType, source, target);
4850
return mapWithBiConsumer(sourceType, targetType, source, result);
4951
}

src/test/java/info/unterrainer/commons/serialization/objectmapper/ObjectMapperMappingTests.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertSame;
65

76
import java.time.LocalDate;
87
import java.util.Arrays;
@@ -14,7 +13,6 @@
1413
import info.unterrainer.commons.serialization.objectmapper.models.ObjectWithArray;
1514
import info.unterrainer.commons.serialization.objectmapper.models.ObjectWithObject;
1615
import info.unterrainer.commons.serialization.objectmapper.models.OtherObjectWithArray;
17-
import info.unterrainer.commons.serialization.objectmapper.models.OtherObjectWithObject;
1816
import info.unterrainer.commons.serialization.objectmapper.models.OtherSimpleUser;
1917
import info.unterrainer.commons.serialization.objectmapper.models.SimpleUser;
2018

@@ -71,7 +69,7 @@ public void mappingComplexUserToSimpleUserWorks() {
7169
cu.setDateTime(LocalDate.now());
7270
cu.setValue(100.00d);
7371
SimpleUser su = SimpleUser.builder().build();
74-
su = oMapper.map(ComplexUser.class, SimpleUser.class, cu, su);
72+
su = oMapper.map(cu, su);
7573
assertEquals(su.getFirstName(), cu.getFirstName());
7674
assertEquals(su.getLastName(), cu.getLastName());
7775
}
@@ -84,7 +82,7 @@ public void mappingComplexUserToOtherSimpleUserWorks() {
8482
cu.setDateTime(LocalDate.now());
8583
cu.setValue(100.00d);
8684
OtherSimpleUser osu = OtherSimpleUser.builder().build();
87-
osu = oMapper.map(ComplexUser.class, OtherSimpleUser.class, cu, osu);
85+
osu = oMapper.map(cu, osu);
8886
assertEquals(osu.getFirstName(), cu.getFirstName());
8987
assertEquals(osu.getSurName(), cu.getLastName());
9088
}
@@ -95,7 +93,7 @@ public void mappingOtherSimpleUserToComplexUserWorks() {
9593
su.setFirstName("Danijel");
9694
su.setSurName("Balog");
9795
ComplexUser cu = ComplexUser.builder().build();
98-
cu = oMapper.map(OtherSimpleUser.class, ComplexUser.class, su, cu);
96+
cu = oMapper.map(su, cu);
9997
assertEquals(su.getFirstName(), cu.getFirstName());
10098
assertEquals(su.getSurName(), cu.getLastName());
10199
}
@@ -107,7 +105,7 @@ public void mappingObjectWithArrayWorks() {
107105
"Balog",
108106
"Hallo"
109107
};
110-
ObjectWithArray oArray = oMapper.map(String[].class, ObjectWithArray.class, array);
108+
ObjectWithArray oArray = oMapper.map(ObjectWithArray.class, array);
111109
assertArrayEquals(array, oArray.getObjects());
112110
}
113111

@@ -119,14 +117,14 @@ public void mappingObjectWithArrayOfObjectsWorks() {
119117
"Balog",
120118
"Hallo"
121119
});
122-
OtherObjectWithArray oArray = oMapper.map(ObjectWithArray.class, OtherObjectWithArray.class, array);
120+
OtherObjectWithArray oArray = oMapper.map(OtherObjectWithArray.class, array);
123121
assertArrayEquals(array.getObjects(), oArray.getObjects());
124122
}
125123

126124
@Test
127125
public void mappingObjectWithinObjectWorks() {
128126
Integer i = 1234;
129-
ObjectWithObject oWO = oMapper.map(Integer.class, ObjectWithObject.class, i);
127+
ObjectWithObject oWO = oMapper.map(ObjectWithObject.class, i);
130128
assertEquals(i, oWO.getObject());
131129
}
132130
}

src/test/java/info/unterrainer/commons/serialization/objectmapper/ObjectMapperSimpleTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,37 @@ public void beforeEach() {
2525
@Test
2626
public void mappingCarToCarDtoWorks() {
2727
Car car = new Car(2, "1929");
28-
CarDto carDto = oMapper.map(Car.class, CarDto.class, car);
28+
CarDto carDto = oMapper.map(CarDto.class, car);
2929
assertEquals(car.getDoorCount(), carDto.getDoorCount());
3030
assertEquals(car.getMakeOfYear(), carDto.getMakeOfYear());
3131
}
3232

3333
@Test
3434
public void mappingCarToSmallCarDtoWorks() {
3535
Car car = new Car(2, "1929");
36-
SmallCarDto smallCarDto = oMapper.map(Car.class, SmallCarDto.class, car);
36+
SmallCarDto smallCarDto = oMapper.map(SmallCarDto.class, car);
3737
assertEquals(car.getDoorCount(), smallCarDto.getDoorCount());
3838
}
3939

4040
@Test
4141
public void mappingSmallCarDtoToCarWorks() {
4242
SmallCarDto smallCarDto = new SmallCarDto(2);
43-
Car car = oMapper.map(SmallCarDto.class, Car.class, smallCarDto);
43+
Car car = oMapper.map(Car.class, smallCarDto);
4444
assertEquals(smallCarDto.getDoorCount(), car.getDoorCount());
4545
}
4646

4747
@Test
4848
public void mappingCarDtoToSimpleUserThrowsException() {
4949
assertThrows(ObjectMapperMappingException.class, () -> {
5050
CarDto car = new CarDto(4, "1975");
51-
oMapper.map(CarDto.class, SimpleUser.class, car);
51+
oMapper.map(SimpleUser.class, car);
5252
});
5353
}
5454

5555
@Test
5656
public void mappingObjectToObjectWorks() {
5757
ObjectWithObject oWO = new ObjectWithObject("HALLO");
58-
OtherObjectWithObject otherOWO = oMapper.map(ObjectWithObject.class, OtherObjectWithObject.class, oWO);
58+
OtherObjectWithObject otherOWO = oMapper.map(OtherObjectWithObject.class, oWO);
5959
assertEquals(oWO.getObject(), otherOWO.getObject());
6060
}
6161
}

0 commit comments

Comments
 (0)