24
24
import java .util .List ;
25
25
import java .util .Locale ;
26
26
import java .util .Map ;
27
+ import java .util .Optional ;
27
28
import java .util .Set ;
28
29
import java .util .concurrent .ConcurrentHashMap ;
29
30
43
44
import org .springframework .data .convert .WritingConverter ;
44
45
import org .springframework .data .mapping .model .SimpleTypeHolder ;
45
46
import org .springframework .data .mongodb .core .mapping .MongoSimpleTypes ;
46
- import org .springframework .data .util .CacheValue ;
47
47
import org .springframework .util .Assert ;
48
48
49
49
/**
@@ -71,9 +71,9 @@ public class CustomConversions {
71
71
72
72
private final List <Object > converters ;
73
73
74
- private final Map <ConvertiblePair , CacheValue <Class <?>>> customReadTargetTypes ;
75
- private final Map <ConvertiblePair , CacheValue <Class <?>>> customWriteTargetTypes ;
76
- private final Map <Class <?>, CacheValue <Class <?>>> rawWriteTargetTypes ;
74
+ private final Map <ConvertiblePair , Optional <Class <?>>> customReadTargetTypes ;
75
+ private final Map <ConvertiblePair , Optional <Class <?>>> customWriteTargetTypes ;
76
+ private final Map <Class <?>, Optional <Class <?>>> rawWriteTargetTypes ;
77
77
78
78
/**
79
79
* Creates an empty {@link CustomConversions} object.
@@ -91,12 +91,12 @@ public CustomConversions(List<?> converters) {
91
91
92
92
Assert .notNull (converters , "List of converters must not be null!" );
93
93
94
- this .readingPairs = new LinkedHashSet <ConvertiblePair >();
95
- this .writingPairs = new LinkedHashSet <ConvertiblePair >();
96
- this .customSimpleTypes = new HashSet <Class <?> >();
97
- this .customReadTargetTypes = new ConcurrentHashMap <ConvertiblePair , CacheValue < Class <?>> >();
98
- this .customWriteTargetTypes = new ConcurrentHashMap <ConvertiblePair , CacheValue < Class <?>> >();
99
- this .rawWriteTargetTypes = new ConcurrentHashMap <Class <?>, CacheValue < Class <?>> >();
94
+ this .readingPairs = new LinkedHashSet <>();
95
+ this .writingPairs = new LinkedHashSet <>();
96
+ this .customSimpleTypes = new HashSet <>();
97
+ this .customReadTargetTypes = new ConcurrentHashMap <>();
98
+ this .customWriteTargetTypes = new ConcurrentHashMap <>();
99
+ this .rawWriteTargetTypes = new ConcurrentHashMap <>();
100
100
101
101
List <Object > toRegister = new ArrayList <Object >();
102
102
@@ -241,13 +241,8 @@ private void register(ConverterRegistration converterRegistration) {
241
241
*/
242
242
public Class <?> getCustomWriteTarget (final Class <?> sourceType ) {
243
243
244
- return getOrCreateAndCache (sourceType , rawWriteTargetTypes , new Producer () {
245
-
246
- @ Override
247
- public Class <?> get () {
248
- return getCustomTarget (sourceType , null , writingPairs );
249
- }
250
- });
244
+ return rawWriteTargetTypes .computeIfAbsent (sourceType , it -> getCustomTarget (sourceType , null , writingPairs ))
245
+ .orElse (null );
251
246
}
252
247
253
248
/**
@@ -265,14 +260,8 @@ public Class<?> getCustomWriteTarget(final Class<?> sourceType, final Class<?> r
265
260
return getCustomWriteTarget (sourceType );
266
261
}
267
262
268
- return getOrCreateAndCache (new ConvertiblePair (sourceType , requestedTargetType ), customWriteTargetTypes ,
269
- new Producer () {
270
-
271
- @ Override
272
- public Class <?> get () {
273
- return getCustomTarget (sourceType , requestedTargetType , writingPairs );
274
- }
275
- });
263
+ return customWriteTargetTypes .computeIfAbsent (new ConvertiblePair (sourceType , requestedTargetType ),
264
+ it -> getCustomTarget (sourceType , requestedTargetType , writingPairs )).orElse (null );
276
265
}
277
266
278
267
/**
@@ -324,14 +313,8 @@ private Class<?> getCustomReadTarget(final Class<?> sourceType, final Class<?> r
324
313
return null ;
325
314
}
326
315
327
- return getOrCreateAndCache (new ConvertiblePair (sourceType , requestedTargetType ), customReadTargetTypes ,
328
- new Producer () {
329
-
330
- @ Override
331
- public Class <?> get () {
332
- return getCustomTarget (sourceType , requestedTargetType , readingPairs );
333
- }
334
- });
316
+ return customReadTargetTypes .computeIfAbsent (new ConvertiblePair (sourceType , requestedTargetType ),
317
+ it -> getCustomTarget (sourceType , requestedTargetType , readingPairs )).orElse (null );
335
318
}
336
319
337
320
/**
@@ -343,54 +326,26 @@ public Class<?> get() {
343
326
* @param pairs must not be {@literal null}.
344
327
* @return
345
328
*/
346
- private static Class <?> getCustomTarget (Class <?> sourceType , Class <?> requestedTargetType ,
329
+ private static Optional < Class <?> > getCustomTarget (Class <?> sourceType , Class <?> requestedTargetType ,
347
330
Collection <ConvertiblePair > pairs ) {
348
331
349
332
Assert .notNull (sourceType , "Source Class must not be null!" );
350
333
Assert .notNull (pairs , "Collection of ConvertiblePair must not be null!" );
351
334
352
335
if (requestedTargetType != null && pairs .contains (new ConvertiblePair (sourceType , requestedTargetType ))) {
353
- return requestedTargetType ;
336
+ return Optional . of ( requestedTargetType ) ;
354
337
}
355
338
356
339
for (ConvertiblePair typePair : pairs ) {
357
340
if (typePair .getSourceType ().isAssignableFrom (sourceType )) {
358
341
Class <?> targetType = typePair .getTargetType ();
359
342
if (requestedTargetType == null || targetType .isAssignableFrom (requestedTargetType )) {
360
- return targetType ;
343
+ return Optional . of ( targetType ) ;
361
344
}
362
345
}
363
346
}
364
347
365
- return null ;
366
- }
367
-
368
- /**
369
- * Will try to find a value for the given key in the given cache or produce one using the given {@link Producer} and
370
- * store it in the cache.
371
- *
372
- * @param key the key to lookup a potentially existing value, must not be {@literal null}.
373
- * @param cache the cache to find the value in, must not be {@literal null}.
374
- * @param producer the {@link Producer} to create values to cache, must not be {@literal null}.
375
- * @return
376
- */
377
- private static <T > Class <?> getOrCreateAndCache (T key , Map <T , CacheValue <Class <?>>> cache , Producer producer ) {
378
-
379
- CacheValue <Class <?>> cacheValue = cache .get (key );
380
-
381
- if (cacheValue != null ) {
382
- return cacheValue .getValue ();
383
- }
384
-
385
- Class <?> type = producer .get ();
386
- cache .put (key , CacheValue .<Class <?>> ofNullable (type ));
387
-
388
- return type ;
389
- }
390
-
391
- private interface Producer {
392
-
393
- Class <?> get ();
348
+ return Optional .empty ();
394
349
}
395
350
396
351
@ WritingConverter
0 commit comments