Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DozerConverter |
|
| 3.375;3.375 |
1 | /** | |
2 | * Copyright 2005-2013 Dozer Project | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.dozer; | |
17 | ||
18 | import org.apache.commons.lang3.ClassUtils; | |
19 | ||
20 | /** | |
21 | * This class should be extended in order to implement new Custom Converters for value transformation. | |
22 | */ | |
23 | public abstract class DozerConverter<A, B> implements ConfigurableCustomConverter { | |
24 | ||
25 | private String parameter; | |
26 | private Class<A> prototypeA; | |
27 | private Class<B> prototypeB; | |
28 | ||
29 | /** | |
30 | * Defines two types, which will take part transformation. | |
31 | * As Dozer supports bi-directional mapping it is not known which of the classes is source and | |
32 | * which is destination. It will be decided in runtime. | |
33 | * | |
34 | * @param prototypeA type one | |
35 | * @param prototypeB type two | |
36 | */ | |
37 | 41 | public DozerConverter(Class<A> prototypeA, Class<B> prototypeB) { |
38 | 41 | this.prototypeA = prototypeA; |
39 | 41 | this.prototypeB = prototypeB; |
40 | 41 | } |
41 | ||
42 | // Method first checks exact type matches and only then checks for assignement | |
43 | public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) { | |
44 | 39 | Class<?> wrappedDestinationClass = ClassUtils.primitiveToWrapper(destinationClass); |
45 | 39 | Class<?> wrappedSourceClass = ClassUtils.primitiveToWrapper(sourceClass); |
46 | ||
47 | 39 | if (prototypeA.equals(wrappedDestinationClass)) { |
48 | 15 | return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue); |
49 | 24 | } else if (prototypeB.equals(wrappedDestinationClass)) { |
50 | 13 | return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue); |
51 | 11 | } else if (prototypeA.equals(wrappedSourceClass)) { |
52 | 1 | return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue); |
53 | 10 | } else if (prototypeB.equals(wrappedSourceClass)) { |
54 | 1 | return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue); |
55 | 9 | } else if (prototypeA.isAssignableFrom(wrappedDestinationClass)) { |
56 | 1 | return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue); |
57 | 8 | } else if (prototypeB.isAssignableFrom(wrappedDestinationClass)) { |
58 | 5 | return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue); |
59 | 3 | } else if (prototypeA.isAssignableFrom(wrappedSourceClass)) { |
60 | 1 | return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue); |
61 | 2 | } else if (prototypeB.isAssignableFrom(wrappedSourceClass)) { |
62 | 1 | return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue); |
63 | } else { | |
64 | 1 | throw new MappingException("Destination Type (" + wrappedDestinationClass.getName() |
65 | + ") is not accepted by this Custom Converter (" | |
66 | + this.getClass().getName() + ")!"); | |
67 | } | |
68 | ||
69 | } | |
70 | ||
71 | ||
72 | /** | |
73 | * Converts the source field to the destination field and return the resulting destination | |
74 | * value. | |
75 | * | |
76 | * @param source the value of the source field | |
77 | * @param destination the current value of the desitinatino field (or null) | |
78 | * @return the resulting value for the destinatino field | |
79 | */ | |
80 | abstract public B convertTo(A source, B destination); | |
81 | ||
82 | /** | |
83 | * Converts the source field to the destination field and return the resulting destination | |
84 | * value. | |
85 | * | |
86 | * @param source the value of the source field | |
87 | * @return the resulting value for the destinatino field | |
88 | */ | |
89 | public B convertTo(A source) { | |
90 | 1 | return convertTo(source, null); |
91 | } | |
92 | ||
93 | /** | |
94 | * Converts the source field to the destination field and return the resulting destination | |
95 | * value | |
96 | * | |
97 | * @param source the value of the source field | |
98 | * @param destination the current value of the desitinatino field (or null) | |
99 | * @return the resulting value for the destinatino field | |
100 | */ | |
101 | abstract public A convertFrom(B source, A destination); | |
102 | ||
103 | /** | |
104 | * Converts the source field to the destination field and return the resulting destination | |
105 | * value | |
106 | * | |
107 | * @param source the value of the source field | |
108 | * @return the resulting value for the destinatino field | |
109 | */ | |
110 | public A convertFrom(B source) { | |
111 | 1 | return convertFrom(source, null); |
112 | } | |
113 | ||
114 | /** | |
115 | * Sets the configured parameter value for this converter instance. | |
116 | * Should be called by Dozer internaly before actual mapping. | |
117 | * | |
118 | * @param parameter configured parameter value | |
119 | */ | |
120 | public void setParameter(String parameter) { | |
121 | 15 | this.parameter = parameter; |
122 | 15 | } |
123 | ||
124 | /** | |
125 | * Retrieves the static parameter configured for this particular converter instance. | |
126 | * It is not advisable to call this method from converter constructor as the parameter is not yet there. | |
127 | * | |
128 | * @return parameter value | |
129 | * @throws IllegalStateException if parameter has not been set yet. | |
130 | */ | |
131 | public String getParameter() { | |
132 | 4 | if (parameter == null) { |
133 | 1 | throw new IllegalStateException("Custom Converter Parameter has not yet been set!"); |
134 | } | |
135 | 3 | return parameter; |
136 | } | |
137 | ||
138 | } |