Skip to content

Commit 5158f0d

Browse files
author
Gerald Unterrainer
committed
pair-programming refactoring
1 parent d342a6e commit 5158f0d

25 files changed

+291
-262
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>serialization</artifactId>
13-
<version>0.2.1</version>
13+
<version>0.2.2</version>
1414
<name>Serialization</name>
1515
<packaging>jar</packaging>
1616

Lines changed: 62 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package info.unterrainer.commons.serialization.objectmapper;
22

3-
import java.lang.reflect.Array;
4-
import java.lang.reflect.InvocationTargetException;
5-
import java.util.ArrayList;
3+
import static com.googlecode.jmapper.api.JMapperAPI.global;
4+
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
5+
66
import java.util.HashMap;
77
import java.util.Map;
88
import java.util.function.BiConsumer;
@@ -12,90 +12,67 @@
1212
import com.googlecode.jmapper.exceptions.JMapperException;
1313

1414
import info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperMappingException;
15-
import info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperProcessingException;
1615
import info.unterrainer.commons.serialization.objectmapper.key.MappingKey;
17-
18-
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
19-
import static com.googlecode.jmapper.api.JMapperAPI.global;
16+
import lombok.NonNull;
2017

2118
public class ObjectMapper {
2219

23-
private Map<MappingKey<?, ?>, BiConsumer<?, ?>> mappings;
24-
private Map<MappingKey<?, ?>, JMapper<?, ?>> mappers;
25-
26-
public ObjectMapper() {
27-
mappings = new HashMap<>();
28-
mappers = new HashMap<>();
29-
}
30-
31-
public <S, T> T[] map(Class<S> sourceType, Class<T> targetType, S[] source) {
32-
return map(sourceType, targetType, source, false);
33-
}
34-
35-
public <S, T> T[] map(Class<S> sourceType, Class<T> targetType, S[] source, boolean usejMapper) {
36-
37-
@SuppressWarnings("unchecked")
38-
T[] targetList = (T[]) Array.newInstance(targetType, source.length);
39-
for (int i = 0; i < source.length; i++) {
40-
targetList[i] = map(sourceType, targetType, source[i], usejMapper);
41-
}
42-
43-
return targetList;
44-
}
45-
46-
public <S, T> T map(Class<S> sourceType, Class<T> targetType, S source) {
47-
return this.map(sourceType, targetType, source, false);
48-
}
49-
50-
public <S, T> T map(Class<S> sourceType, Class<T> targetType, S source, boolean usejMapper) {
51-
T result = null;
52-
53-
@SuppressWarnings("unchecked")
54-
BiConsumer<S, T> biConsumer = (BiConsumer<S, T>) mappings.get(new MappingKey<S, T>(sourceType, targetType));
55-
// Map fields by name...
56-
if (biConsumer != null && usejMapper == false) {
57-
try {
58-
result = targetType.getConstructor().newInstance();
59-
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
60-
| InvocationTargetException | NoSuchMethodException | SecurityException
61-
| ObjectMapperProcessingException e) {
62-
throw new info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperProcessingException(
63-
e.getMessage(), e);
64-
}
65-
try {
66-
biConsumer.accept(source, result);
67-
} catch (ObjectMapperMappingException e) {
68-
throw new info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperMappingException(
69-
e.getMessage(), e);
70-
}
71-
} else {
72-
@SuppressWarnings("unchecked")
73-
JMapper<T, S> jMapper = (JMapper<T, S>) mappers.get(new MappingKey<S, T>(sourceType, targetType));
74-
75-
if (jMapper == null) {
76-
JMapperAPI jmapperApi = new JMapperAPI().add(mappedClass(targetType).add(global()));
77-
try {
78-
jMapper = new JMapper<>(targetType, sourceType, jmapperApi);
79-
T tmp = jMapper.getDestination(source);
80-
result = tmp;
81-
} catch (JMapperException | ObjectMapperMappingException e) {
82-
throw new info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperMappingException(
83-
e.getMessage(), e);
84-
}
85-
}
86-
if (result != null) {
87-
mappers.put(new MappingKey<S, T>(sourceType, targetType), jMapper);
88-
}
89-
}
90-
91-
return result;
92-
}
93-
94-
// mapping function
95-
// Object to Object mapper
96-
public <A, B> void registerMapping(Class<A> sourceType, Class<B> targetType, BiConsumer<A, B> forth,
97-
BiConsumer<B, A> back) {
98-
mappings.put(new MappingKey<A, B>(sourceType, targetType), forth);
99-
mappings.put(new MappingKey<B, A>(targetType, sourceType), back);
100-
}
20+
private Map<MappingKey<?, ?>, JMapper<?, ?>> mappers;
21+
private Map<MappingKey<?, ?>, BiConsumer<?, ?>> mappings;
22+
23+
public ObjectMapper() {
24+
mappings = new HashMap<>();
25+
mappers = new HashMap<>();
26+
}
27+
28+
public <A, B> void registerMapping(@NonNull final Class<A> sourceType, @NonNull final Class<B> targetType,
29+
final BiConsumer<A, B> forth, final BiConsumer<B, A> back) {
30+
if (forth != null)
31+
mappings.put(new MappingKey<>(sourceType, targetType), forth);
32+
if (back != null)
33+
mappings.put(new MappingKey<>(targetType, sourceType), back);
34+
}
35+
36+
public <S, T> T map(@NonNull final Class<S> sourceType, @NonNull final Class<T> targetType,
37+
@NonNull final S source) {
38+
return map(sourceType, targetType, source, null);
39+
}
40+
41+
public <S, T> T map(@NonNull final Class<S> sourceType, @NonNull final Class<T> targetType, @NonNull final S source,
42+
final T target) {
43+
T result = mapWithJMapper(sourceType, targetType, source, target);
44+
return mapWithBiConsumer(sourceType, targetType, source, result);
45+
}
46+
47+
private <T, S> T mapWithBiConsumer(@NonNull final Class<S> sourceType, @NonNull final Class<T> targetType,
48+
@NonNull final S source, @NonNull final T target) {
49+
@SuppressWarnings("unchecked")
50+
BiConsumer<S, T> biConsumer = (BiConsumer<S, T>) mappings.get(new MappingKey<>(sourceType, targetType));
51+
52+
if (biConsumer != null)
53+
biConsumer.accept(source, target);
54+
return target;
55+
}
56+
57+
private <T, S> T mapWithJMapper(@NonNull final Class<S> sourceType, @NonNull final Class<T> targetType,
58+
@NonNull final S source, final T target) {
59+
@SuppressWarnings("unchecked")
60+
JMapper<T, S> jMapper = (JMapper<T, S>) mappers.get(new MappingKey<>(sourceType, targetType));
61+
if (jMapper == null) {
62+
JMapperAPI jmapperApi = new JMapperAPI().add(mappedClass(targetType).add(global()));
63+
try {
64+
jMapper = new JMapper<>(targetType, sourceType, jmapperApi);
65+
} catch (JMapperException e) {
66+
throw new ObjectMapperMappingException("Error creating mapper.", e);
67+
}
68+
mappers.put(new MappingKey<>(sourceType, targetType), jMapper);
69+
}
70+
try {
71+
if (target == null)
72+
return jMapper.getDestination(source);
73+
return jMapper.getDestination(target, source);
74+
} catch (JMapperException e) {
75+
throw new ObjectMapperMappingException("Error mapping objects.", e);
76+
}
77+
}
10178
}

src/main/resources/log4j.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
1111

1212
# Print only info for imported libraries.
1313
log4j.logger.io.netty=WARN
14-
log4j.logger.org.eclipse.milo=WARN
14+
log4j.logger.org.eclipse.milo=WARN
15+
log4j.logger.com.googlecode.jmapper.JMapper=FATAL
16+
log4j.logger.org.reflections.Reflections=WARN

src/test/java/info/unterrainer/commons/serialization/ObjectMapperTests.java

Lines changed: 0 additions & 112 deletions
This file was deleted.

src/test/java/info/unterrainer/commons/serialization/DateTimeTests.java renamed to src/test/java/info/unterrainer/commons/serialization/jsonmapper/DateTimeTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization;
1+
package info.unterrainer.commons.serialization.jsonmapper;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

@@ -9,8 +9,7 @@
99

1010
import org.junit.jupiter.api.Test;
1111

12-
import info.unterrainer.commons.serialization.jsonmapper.JsonMapper;
13-
import info.unterrainer.commons.serialization.jsons.DateTimeJson;
12+
import info.unterrainer.commons.serialization.jsonmapper.jsons.DateTimeJson;
1413

1514
public class DateTimeTests {
1615

src/test/java/info/unterrainer/commons/serialization/JsonMapperTests.java renamed to src/test/java/info/unterrainer/commons/serialization/jsonmapper/JsonMapperTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization;
1+
package info.unterrainer.commons.serialization.jsonmapper;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

@@ -9,10 +9,9 @@
99
import com.fasterxml.jackson.core.JsonProcessingException;
1010
import com.fasterxml.jackson.databind.JsonMappingException;
1111

12-
import info.unterrainer.commons.serialization.jsonmapper.JsonMapper;
13-
import info.unterrainer.commons.serialization.jsons.ChildJson;
14-
import info.unterrainer.commons.serialization.jsons.MediolaDatagramJson;
15-
import info.unterrainer.commons.serialization.jsons.SimpleJson;
12+
import info.unterrainer.commons.serialization.jsonmapper.jsons.ChildJson;
13+
import info.unterrainer.commons.serialization.jsonmapper.jsons.MediolaDatagramJson;
14+
import info.unterrainer.commons.serialization.jsonmapper.jsons.SimpleJson;
1615

1716
public class JsonMapperTests {
1817

src/test/java/info/unterrainer/commons/serialization/jsons/ChildJson.java renamed to src/test/java/info/unterrainer/commons/serialization/jsonmapper/jsons/ChildJson.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package info.unterrainer.commons.serialization.jsons;
1+
package info.unterrainer.commons.serialization.jsonmapper.jsons;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44

5+
import info.unterrainer.commons.serialization.jsons.BasicJson;
56
import lombok.Data;
67
import lombok.EqualsAndHashCode;
78
import lombok.NoArgsConstructor;

src/test/java/info/unterrainer/commons/serialization/jsons/DateTimeJson.java renamed to src/test/java/info/unterrainer/commons/serialization/jsonmapper/jsons/DateTimeJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization.jsons;
1+
package info.unterrainer.commons.serialization.jsonmapper.jsons;
22

33
import java.time.LocalDate;
44
import java.time.LocalDateTime;

src/test/java/info/unterrainer/commons/serialization/jsons/MediolaDatagramJson.java renamed to src/test/java/info/unterrainer/commons/serialization/jsonmapper/jsons/MediolaDatagramJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization.jsons;
1+
package info.unterrainer.commons.serialization.jsonmapper.jsons;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44

src/test/java/info/unterrainer/commons/serialization/jsons/MediolaDatagramStateJson.java renamed to src/test/java/info/unterrainer/commons/serialization/jsonmapper/jsons/MediolaDatagramStateJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization.jsons;
1+
package info.unterrainer.commons.serialization.jsonmapper.jsons;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44

0 commit comments

Comments
 (0)