1
1
package info .unterrainer .commons .serialization .objectmapper ;
2
2
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
+
6
6
import java .util .HashMap ;
7
7
import java .util .Map ;
8
8
import java .util .function .BiConsumer ;
12
12
import com .googlecode .jmapper .exceptions .JMapperException ;
13
13
14
14
import info .unterrainer .commons .serialization .objectmapper .exceptions .ObjectMapperMappingException ;
15
- import info .unterrainer .commons .serialization .objectmapper .exceptions .ObjectMapperProcessingException ;
16
15
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 ;
20
17
21
18
public class ObjectMapper {
22
19
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
+ }
101
78
}
0 commit comments