34
34
import java .util .Set ;
35
35
import java .util .concurrent .ConcurrentHashMap ;
36
36
import java .util .function .Function ;
37
- import java .util .function .Supplier ;
37
+ import java .util .function .Predicate ;
38
38
import java .util .stream .Collectors ;
39
39
40
40
import org .springframework .core .GenericTypeResolver ;
@@ -99,35 +99,24 @@ public class CustomConversions {
99
99
private final Function <ConvertiblePair , Class <?>> getReadTarget = convertiblePair -> getCustomTarget (
100
100
convertiblePair .getSourceType (), convertiblePair .getTargetType (), readingPairs );
101
101
102
- private Function <ConvertiblePair , Class <?>> getWriteTarget = convertiblePair -> getCustomTarget (
102
+ private final Function <ConvertiblePair , Class <?>> getWriteTarget = convertiblePair -> getCustomTarget (
103
103
convertiblePair .getSourceType (), convertiblePair .getTargetType (), writingPairs );
104
104
105
- private Function <ConvertiblePair , Class <?>> getRawWriteTarget = convertiblePair -> getCustomTarget (
105
+ private final Function <ConvertiblePair , Class <?>> getRawWriteTarget = convertiblePair -> getCustomTarget (
106
106
convertiblePair .getSourceType (), null , writingPairs );
107
107
108
- /**
109
- * Deferred initialization allowing store implementations use a callback/consumer driven configuration of the actual
110
- * {@link CustomConversions}.
111
- *
112
- * @param converterConfiguration the {@link Supplier} providing a {@link ConverterConfiguration}.
113
- * @since 2.3
114
- */
115
- protected CustomConversions (Supplier <ConverterConfiguration > converterConfiguration ) {
116
- this (converterConfiguration .get ());
117
- }
118
-
119
108
/**
120
109
* @param converterConfiguration the {@link ConverterConfiguration} to apply.
121
110
* @since 2.3
122
111
*/
123
- protected CustomConversions (ConverterConfiguration converterConfiguration ) {
112
+ public CustomConversions (ConverterConfiguration converterConfiguration ) {
124
113
125
114
this .converterConfiguration = converterConfiguration ;
126
115
127
116
List <Object > registeredConverters = collectPotentialConverterRegistrations (
128
117
converterConfiguration .getStoreConversions (), converterConfiguration .getUserConverters ()).stream () //
129
118
.filter (this ::isSupportedConverter ) //
130
- .filter (it -> ! skip ( it ) ) //
119
+ .filter (this :: shouldRegister ) //
131
120
.map (ConverterRegistrationIntent ::getConverterRegistration ) //
132
121
.map (this ::register ) //
133
122
.distinct () //
@@ -191,7 +180,7 @@ public void registerConvertersIn(ConverterRegistry conversionService) {
191
180
192
181
/**
193
182
* Get all converters and add origin information
194
- *
183
+ *
195
184
* @param storeConversions
196
185
* @param converters
197
186
* @return
@@ -202,20 +191,23 @@ private List<ConverterRegistrationIntent> collectPotentialConverterRegistrations
202
191
203
192
List <ConverterRegistrationIntent > converterRegistrations = new ArrayList <>();
204
193
205
- converterRegistrations .addAll (converters .stream () //
206
- .flatMap (it -> storeConversions .getRegistrationsFor (it ).stream ()) //
194
+ converters .stream () //
195
+ .map (storeConversions ::getRegistrationsFor ) //
196
+ .flatMap (Streamable ::stream ) //
207
197
.map (ConverterRegistrationIntent ::userConverters ) //
208
- .collect ( Collectors . toList ()) );
198
+ .forEach ( converterRegistrations :: add );
209
199
210
- converterRegistrations .addAll (storeConversions .getStoreConverters ().stream () //
211
- .flatMap (it -> storeConversions .getRegistrationsFor (it ).stream ()) //
200
+ storeConversions .getStoreConverters ().stream () //
201
+ .map (storeConversions ::getRegistrationsFor ) //
202
+ .flatMap (Streamable ::stream ) //
212
203
.map (ConverterRegistrationIntent ::storeConverters ) //
213
- .collect ( Collectors . toList ()) );
204
+ .forEach ( converterRegistrations :: add );
214
205
215
- converterRegistrations .addAll (DEFAULT_CONVERTERS .stream () //
216
- .flatMap (it -> storeConversions .getRegistrationsFor (it ).stream ()) //
206
+ DEFAULT_CONVERTERS .stream () //
207
+ .map (storeConversions ::getRegistrationsFor ) //
208
+ .flatMap (Streamable ::stream ) //
217
209
.map (ConverterRegistrationIntent ::defaultConverters ) //
218
- .collect ( Collectors . toList ()) );
210
+ .forEach ( converterRegistrations :: add );
219
211
220
212
return converterRegistrations ;
221
213
}
@@ -253,7 +245,7 @@ private void registerConverterIn(Object candidate, ConverterRegistry conversionS
253
245
254
246
/**
255
247
* Registers the given {@link ConvertiblePair} as reading or writing pair depending on the type sides being basic
256
- * Mongo types.
248
+ * types.
257
249
*
258
250
* @param converterRegistration
259
251
*/
@@ -318,18 +310,17 @@ private boolean isSupportedConverter(ConverterRegistrationIntent registrationInt
318
310
319
311
/**
320
312
* @param intent must not be {@literal null}.
321
- * @return {@literal true } if the given {@link ConverterRegistration} shall be skipped.
313
+ * @return {@literal false } if the given {@link ConverterRegistration} shall be skipped.
322
314
* @since 2.3
323
315
*/
324
- private boolean skip (ConverterRegistrationIntent intent ) {
325
-
326
- return intent .isDefaultConveter ()
327
- && converterConfiguration .getSkipFor ().contains (intent .getConverterRegistration ().getConvertiblePair ());
316
+ private boolean shouldRegister (ConverterRegistrationIntent intent ) {
317
+ return !intent .isDefaultConverter ()
318
+ || converterConfiguration .shouldRegister (intent .getConverterRegistration ().getConvertiblePair ());
328
319
}
329
320
330
321
/**
331
322
* Returns the target type to convert to in case we have a custom conversion registered to convert the given source
332
- * type into a Mongo native one.
323
+ * type into a store native one.
333
324
*
334
325
* @param sourceType must not be {@literal null}
335
326
* @return
@@ -344,9 +335,9 @@ public Optional<Class<?>> getCustomWriteTarget(Class<?> sourceType) {
344
335
}
345
336
346
337
/**
347
- * Returns the target type we can read an inject of the given source type to. The returned type might
348
- * be a subclass of the given expected type though. If {@code requestedTargetType} is {@literal null} we will simply
349
- * return the first target type matching or {@literal null} if no conversion can be found.
338
+ * Returns the target type we can read an inject of the given source type to. The returned type might be a subclass of
339
+ * the given expected type though. If {@code requestedTargetType} is {@literal null} we will simply return the first
340
+ * target type matching or {@literal null} if no conversion can be found.
350
341
*
351
342
* @param sourceType must not be {@literal null}
352
343
* @param requestedTargetType must not be {@literal null}.
@@ -363,8 +354,8 @@ public Optional<Class<?>> getCustomWriteTarget(Class<?> sourceType, Class<?> req
363
354
}
364
355
365
356
/**
366
- * Returns whether we have a custom conversion registered to read {@code sourceType} into a native type. The
367
- * returned type might be a subclass of the given expected type though.
357
+ * Returns whether we have a custom conversion registered to read {@code sourceType} into a native type. The returned
358
+ * type might be a subclass of the given expected type though.
368
359
*
369
360
* @param sourceType must not be {@literal null}
370
361
* @return
@@ -377,8 +368,8 @@ public boolean hasCustomWriteTarget(Class<?> sourceType) {
377
368
}
378
369
379
370
/**
380
- * Returns whether we have a custom conversion registered to read an object of the given source type
381
- * into an object of the given native target type.
371
+ * Returns whether we have a custom conversion registered to read an object of the given source type into an object of
372
+ * the given native target type.
382
373
*
383
374
* @param sourceType must not be {@literal null}.
384
375
* @param targetType must not be {@literal null}.
@@ -393,8 +384,7 @@ public boolean hasCustomWriteTarget(Class<?> sourceType, Class<?> targetType) {
393
384
}
394
385
395
386
/**
396
- * Returns whether we have a custom conversion registered to read the given source into the given target
397
- * type.
387
+ * Returns whether we have a custom conversion registered to read the given source into the given target type.
398
388
*
399
389
* @param sourceType must not be {@literal null}
400
390
* @param targetType must not be {@literal null}
@@ -409,8 +399,8 @@ public boolean hasCustomReadTarget(Class<?> sourceType, Class<?> targetType) {
409
399
}
410
400
411
401
/**
412
- * Returns the actual target type for the given {@code sourceType} and {@code targetType}. Note that the
413
- * returned {@link Class} could be an assignable type to the given {@code targetType}.
402
+ * Returns the actual target type for the given {@code sourceType} and {@code targetType}. Note that the returned
403
+ * {@link Class} could be an assignable type to the given {@code targetType}.
414
404
*
415
405
* @param sourceType must not be {@literal null}.
416
406
* @param targetType must not be {@literal null}.
@@ -422,8 +412,8 @@ private Class<?> getCustomReadTarget(Class<?> sourceType, Class<?> targetType) {
422
412
}
423
413
424
414
/**
425
- * Inspects the given {@link ConvertiblePair ConvertiblePairs} for ones that have a source compatible type as source. Additionally
426
- * checks assignability of the target type if one is given.
415
+ * Inspects the given {@link ConvertiblePair ConvertiblePairs} for ones that have a source compatible type as source.
416
+ * Additionally checks assignability of the target type if one is given.
427
417
*
428
418
* @param sourceType must not be {@literal null}.
429
419
* @param targetType can be {@literal null}.
@@ -461,7 +451,7 @@ private static boolean hasAssignableSourceType(ConvertiblePair pair, Class<?> so
461
451
}
462
452
463
453
private static boolean requestedTargetTypeIsAssignable (@ Nullable Class <?> requestedTargetType , Class <?> targetType ) {
464
- return requestedTargetType == null ? true : targetType .isAssignableFrom (requestedTargetType );
454
+ return requestedTargetType == null || targetType .isAssignableFrom (requestedTargetType );
465
455
}
466
456
467
457
/**
@@ -607,7 +597,7 @@ public boolean isUserConverter() {
607
597
return isConverterOfSource (ConverterOrigin .USER_DEFINED );
608
598
}
609
599
610
- public boolean isDefaultConveter () {
600
+ public boolean isDefaultConverter () {
611
601
return isConverterOfSource (ConverterOrigin .DEFAULT );
612
602
}
613
603
@@ -645,7 +635,7 @@ private static class ConverterRegistration {
645
635
* @return
646
636
*/
647
637
public boolean isWriting () {
648
- return writing == true || (!reading && isSimpleTargetType ());
638
+ return writing || (!reading && isSimpleTargetType ());
649
639
}
650
640
651
641
/**
@@ -654,7 +644,7 @@ public boolean isWriting() {
654
644
* @return
655
645
*/
656
646
public boolean isReading () {
657
- return reading == true || (!writing && isSimpleSourceType ());
647
+ return reading || (!writing && isSimpleSourceType ());
658
648
}
659
649
660
650
/**
@@ -667,7 +657,7 @@ public ConvertiblePair getConvertiblePair() {
667
657
}
668
658
669
659
/**
670
- * Returns whether the source type is a Mongo simple one.
660
+ * Returns whether the source type is a simple one.
671
661
*
672
662
* @return
673
663
*/
@@ -676,7 +666,7 @@ public boolean isSimpleSourceType() {
676
666
}
677
667
678
668
/**
679
- * Returns whether the target type is a Mongo simple one.
669
+ * Returns whether the target type is a simple one.
680
670
*
681
671
* @return
682
672
*/
@@ -811,15 +801,15 @@ private boolean isStoreSimpleType(Class<?> type) {
811
801
/**
812
802
* Value object holding the actual {@link StoreConversions} and custom {@link Converter converters} configured for
813
803
* registration.
814
- *
804
+ *
815
805
* @author Christoph Strobl
816
806
* @since 2.3
817
807
*/
818
808
protected static class ConverterConfiguration {
819
809
820
810
private final StoreConversions storeConversions ;
821
811
private final List <?> userConverters ;
822
- private final Set <ConvertiblePair > skipFor ;
812
+ private final Predicate <ConvertiblePair > converterRegistrationFilter ;
823
813
824
814
/**
825
815
* Create a new ConverterConfiguration holding the given {@link StoreConversions} and user defined converters.
@@ -828,7 +818,7 @@ protected static class ConverterConfiguration {
828
818
* @param userConverters must not be {@literal null} use {@link Collections#emptyList()} instead.
829
819
*/
830
820
public ConverterConfiguration (StoreConversions storeConversions , List <?> userConverters ) {
831
- this (storeConversions , userConverters , Collections . emptySet () );
821
+ this (storeConversions , userConverters , it -> true );
832
822
}
833
823
834
824
/**
@@ -840,14 +830,14 @@ public ConverterConfiguration(StoreConversions storeConversions, List<?> userCon
840
830
*
841
831
* @param storeConversions must not be {@literal null}.
842
832
* @param userConverters must not be {@literal null} use {@link Collections#emptyList()} instead.
843
- * @param skipFor must not be {@literal null} use {@link Collections#emptyList()} instead .
833
+ * @param converterRegistrationFilter must not be {@literal null}. .
844
834
*/
845
835
public ConverterConfiguration (StoreConversions storeConversions , List <?> userConverters ,
846
- Collection <ConvertiblePair > skipFor ) {
836
+ Predicate <ConvertiblePair > converterRegistrationFilter ) {
847
837
848
838
this .storeConversions = storeConversions ;
849
839
this .userConverters = new ArrayList <>(userConverters );
850
- this .skipFor = new HashSet <>( skipFor ) ;
840
+ this .converterRegistrationFilter = converterRegistrationFilter ;
851
841
}
852
842
853
843
/**
@@ -867,8 +857,8 @@ List<?> getUserConverters() {
867
857
/**
868
858
* @return never {@literal null}.
869
859
*/
870
- Set < ConvertiblePair > getSkipFor ( ) {
871
- return skipFor ;
860
+ boolean shouldRegister ( ConvertiblePair candidate ) {
861
+ return this . converterRegistrationFilter . test ( candidate ) ;
872
862
}
873
863
}
874
864
}
0 commit comments