12
12
import io .beanmapper .core .converter .collections .CollectionListConverter ;
13
13
import io .beanmapper .core .converter .collections .CollectionMapConverter ;
14
14
import io .beanmapper .core .converter .collections .CollectionSetConverter ;
15
- import io .beanmapper .core .converter .impl .*;
15
+ import io .beanmapper .core .converter .impl .NumberToNumberConverter ;
16
+ import io .beanmapper .core .converter .impl .ObjectToStringConverter ;
17
+ import io .beanmapper .core .converter .impl .PrimitiveConverter ;
18
+ import io .beanmapper .core .converter .impl .StringToBigDecimalConverter ;
19
+ import io .beanmapper .core .converter .impl .StringToBooleanConverter ;
20
+ import io .beanmapper .core .converter .impl .StringToEnumConverter ;
21
+ import io .beanmapper .core .converter .impl .StringToIntegerConverter ;
22
+ import io .beanmapper .core .converter .impl .StringToLongConverter ;
23
+ import io .beanmapper .core .rule .BeanMapperRule ;
24
+ import io .beanmapper .core .rule .ConstantMapperRule ;
16
25
import io .beanmapper .core .unproxy .BeanUnproxy ;
17
26
import io .beanmapper .core .unproxy .DefaultBeanUnproxy ;
18
27
import io .beanmapper .core .unproxy .SkippingBeanUnproxy ;
@@ -91,11 +100,27 @@ public <S, T> T map(S source, Class<T> targetClass) {
91
100
return map (source , targetClass , beanInitializer , false );
92
101
}
93
102
103
+ /**
104
+ * Copies the values from the source object to a newly constructed target instance
105
+ * @param source source instance of the properties
106
+ * @param targetClass class of the target, needs to be constructed as the target instance
107
+ * @param converterChoosable when {@code true} a plain conversion is checked before mapping
108
+ * @param <S> The instance from which the properties get copied
109
+ * @param <T> the instance to which the properties get copied
110
+ * @return the target instance containing all applicable properties
111
+ * @throws BeanMappingException
112
+ */
113
+ @ SuppressWarnings ("unchecked" )
114
+ public <S , T > T map (S source , Class <T > targetClass , boolean converterChoosable ) {
115
+ return map (source , targetClass , beanInitializer , converterChoosable );
116
+ }
117
+
94
118
/**
95
119
* Copies the values from the source object to a newly constructed target instance
96
120
* @param source source instance of the properties
97
121
* @param targetClass class of the target, needs to be constructed as the target instance
98
122
* @param beanInitializer initializes the beans
123
+ * @param converterChoosable when {@code true} a plain conversion is checked before mapping
99
124
* @param <S> The instance from which the properties get copied
100
125
* @param <T> the instance to which the properties get copied
101
126
* @return the target instance containing all applicable properties
@@ -115,22 +140,33 @@ public <S, T> T map(S source, Class<T> targetClass, BeanInitializer beanInitiali
115
140
return map (source , target );
116
141
}
117
142
118
- public <S , T > T mapForListElement (S source , Class <T > targetClass ) {
119
- return map (source , targetClass , beanInitializer , true );
120
- }
121
-
122
143
/**
123
144
* Maps a list of source items to a list of target items with a specific class
124
145
* @param sourceItems the items to be mapped
125
146
* @param targetClass the class type of the items in the returned list
126
- * @param <S> source
127
- * @param <T> target
147
+ * @param <S> the instance from which the properties get copied.
148
+ * @param <T> the instance to which the properties get copied
128
149
* @return the list of mapped items with class T
129
150
* @throws BeanMappingException
130
151
*/
131
152
@ SuppressWarnings ("unchecked" )
132
153
public <S , T > Collection <T > map (Collection <S > sourceItems , Class <T > targetClass ) {
133
- Collection <T > targetItems = (Collection <T >) beanInitializer .instantiate (sourceItems .getClass ());
154
+ return map (sourceItems , targetClass , sourceItems .getClass ());
155
+ }
156
+
157
+ /**
158
+ * Maps a list of source items to a list of target items with a specific class
159
+ * @param sourceItems the items to be mapped
160
+ * @param targetClass the class type of the items in the returned list
161
+ * @param collectionClass the collection class
162
+ * @param <S> the instance from which the properties get copied.
163
+ * @param <T> the instance to which the properties get copied
164
+ * @return the list of mapped items with class T
165
+ * @throws BeanMappingException
166
+ */
167
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
168
+ public <S , T > Collection <T > map (Collection <S > sourceItems , Class <T > targetClass , Class <? extends Collection > collectionClass ) {
169
+ Collection <T > targetItems = (Collection <T >) beanInitializer .instantiate (collectionClass );
134
170
for (S source : sourceItems ) {
135
171
targetItems .add (map (source , targetClass ));
136
172
}
@@ -141,13 +177,27 @@ public <S, T> Collection<T> map(Collection<S> sourceItems, Class<T> targetClass)
141
177
* Copies the values from the source object to an existing target instance
142
178
* @param source source instance of the properties
143
179
* @param target target instance for the properties
144
- * @param <S> The instance from which the properties get copied.
180
+ * @param <S> the instance from which the properties get copied.
145
181
* @param <T> the instance to which the properties get copied
146
182
* @return the original target instance containing all applicable properties
147
183
* @throws BeanMappingException
148
184
*/
149
185
public <S , T > T map (S source , T target ) {
150
- return matchSourceToTarget (source , target );
186
+ return map (source , target , new ConstantMapperRule (true ));
187
+ }
188
+
189
+ /**
190
+ * Copies the values from the source object to an existing target instance
191
+ * @param source source instance of the properties
192
+ * @param target target instance for the properties
193
+ * @param rule the matching rule
194
+ * @param <S> the instance from which the properties get copied.
195
+ * @param <T> the instance to which the properties get copied
196
+ * @return the original target instance containing all applicable properties
197
+ * @throws BeanMappingException
198
+ */
199
+ public <S , T > T map (S source , T target , BeanMapperRule rule ) {
200
+ return matchSourceToTarget (source , target , rule );
151
201
}
152
202
153
203
/**
@@ -163,12 +213,14 @@ public <S, T> T map(S source, T target) {
163
213
* @return A filled target object.
164
214
* @throws BeanMappingException
165
215
*/
166
- private <S , T > T matchSourceToTarget (S source , T target ) {
216
+ private <S , T > T matchSourceToTarget (S source , T target , BeanMapperRule rule ) {
167
217
BeanMatch beanMatch = getBeanMatch (source , target );
168
218
for (String fieldName : beanMatch .getTargetNode ().keySet ()) {
169
219
BeanField sourceField = beanMatch .getSourceNode ().get (fieldName );
170
220
BeanField targetField = beanMatch .getTargetNode ().get (fieldName );
171
- processField (new BeanFieldMatch (source , target , sourceField , targetField , fieldName ));
221
+ if (rule .isAllowed (sourceField , targetField )) {
222
+ processField (new BeanFieldMatch (source , target , sourceField , targetField , fieldName ), rule );
223
+ }
172
224
}
173
225
return target ;
174
226
}
@@ -184,7 +236,7 @@ private <T, S> BeanMatch getBeanMatch(S source, T target) {
184
236
* @param beanFieldMatch contains the fields belonging to the source/target field match
185
237
* @throws BeanMappingException
186
238
*/
187
- private void processField (BeanFieldMatch beanFieldMatch ) {
239
+ private void processField (BeanFieldMatch beanFieldMatch , BeanMapperRule rule ) {
188
240
if (!beanFieldMatch .hasMatchingSource ()) {
189
241
dealWithNonMatchingNode (beanFieldMatch );
190
242
return ;
@@ -194,7 +246,7 @@ private void processField(BeanFieldMatch beanFieldMatch) {
194
246
isMappableClass (beanFieldMatch .getTargetClass ()) &&
195
247
beanFieldMatch .getSourceObject () != null ) {
196
248
197
- dealWithMappableNestedClass (beanFieldMatch );
249
+ dealWithMappableNestedClass (beanFieldMatch , rule );
198
250
return ;
199
251
}
200
252
if (beanFieldMatch .isMappable ()) {
@@ -239,13 +291,14 @@ private void copySourceToTarget(BeanFieldMatch beanFieldMatch) {
239
291
* If the field is a class which can itself be mapped to another class, it must be treated
240
292
* as such. The matching process is called recursively to deal with this pair.
241
293
* @param beanFieldMatch contains the fields belonging to the source/target field match
294
+ * @param rule the matching rule
242
295
* @throws BeanMappingException
243
296
*/
244
- private void dealWithMappableNestedClass (BeanFieldMatch beanFieldMatch ) {
297
+ private void dealWithMappableNestedClass (BeanFieldMatch beanFieldMatch , BeanMapperRule rule ) {
245
298
Object encapsulatedSource = beanFieldMatch .getSourceObject ();
246
299
if (encapsulatedSource != null ) {
247
300
Object encapsulatedTarget = beanFieldMatch .getOrCreateTargetObject ();
248
- matchSourceToTarget (encapsulatedSource , encapsulatedTarget );
301
+ matchSourceToTarget (encapsulatedSource , encapsulatedTarget , rule );
249
302
}
250
303
}
251
304
0 commit comments