15
15
*/
16
16
package org .springframework .data .mapping .context ;
17
17
18
- import java .util .*;
18
+ import java .util .ArrayList ;
19
+ import java .util .Collection ;
20
+ import java .util .Collections ;
21
+ import java .util .Comparator ;
22
+ import java .util .HashSet ;
23
+ import java .util .Iterator ;
24
+ import java .util .List ;
25
+ import java .util .Map ;
26
+ import java .util .Optional ;
27
+ import java .util .Set ;
19
28
import java .util .function .Function ;
20
29
import java .util .function .Predicate ;
21
30
import java .util .stream .Collectors ;
34
43
import org .springframework .lang .Nullable ;
35
44
import org .springframework .util .Assert ;
36
45
import org .springframework .util .ConcurrentReferenceHashMap ;
37
- import org .springframework .util .ObjectUtils ;
38
46
import org .springframework .util .StringUtils ;
39
47
40
48
/**
41
49
* A factory implementation to create {@link PersistentPropertyPath} instances in various ways.
42
50
*
43
51
* @author Oliver Gierke
44
52
* @author Christoph Strobl
53
+ * @author Mark Paluch
45
54
* @since 2.1
46
55
* @soundtrack Cypress Hill - Boom Biddy Bye Bye (Fugees Remix, Unreleased & Revamped)
47
56
*/
@@ -172,7 +181,7 @@ private PersistentPropertyPath<P> getPersistentPropertyPath(TypeInformation<?> t
172
181
173
182
private PathResolution getPotentiallyCachedPath (TypeInformation <?> type , String propertyPath ) {
174
183
return propertyPaths .computeIfAbsent (TypeAndPath .of (type , propertyPath ),
175
- it -> createPersistentPropertyPath (it .getPath (), it .getType ()));
184
+ it -> createPersistentPropertyPath (it .path (), it .type ()));
176
185
}
177
186
178
187
/**
@@ -200,14 +209,14 @@ private PathResolution createPersistentPropertyPath(String propertyPath, TypeInf
200
209
Pair <DefaultPersistentPropertyPath <P >, E > pair = getPair (path , iterator , segment , current );
201
210
202
211
if (pair == null ) {
203
- return new PathResolution (parts , segment , type , currentPath );
212
+ return PathResolution . unresolved (parts , segment , type , currentPath );
204
213
}
205
214
206
215
path = pair .getFirst ();
207
216
current = pair .getSecond ();
208
217
}
209
218
210
- return new PathResolution (path );
219
+ return PathResolution . resolved (path );
211
220
}
212
221
213
222
@ Nullable
@@ -240,7 +249,7 @@ private Collection<PersistentPropertyPath<P>> from(E entity, Predicate<? super P
240
249
241
250
Set <PersistentPropertyPath <P >> properties = new HashSet <>();
242
251
243
- PropertyHandler <P > propertyTester = ( PropertyHandler < P >) persistentProperty -> {
252
+ PropertyHandler <P > propertyTester = persistentProperty -> {
244
253
245
254
TypeInformation <?> typeInformation = persistentProperty .getTypeInformation ();
246
255
TypeInformation <?> actualPropertyType = typeInformation .getActualType ();
@@ -263,64 +272,19 @@ private Collection<PersistentPropertyPath<P>> from(E entity, Predicate<? super P
263
272
264
273
entity .doWithProperties (propertyTester );
265
274
266
- AssociationHandler <P > handler = ( AssociationHandler < P >) association -> propertyTester
275
+ AssociationHandler <P > handler = association -> propertyTester
267
276
.doWithPersistentProperty (association .getInverse ());
268
277
entity .doWithAssociations (handler );
269
278
270
279
return properties ;
271
280
}
272
281
273
- static final class TypeAndPath {
274
-
275
- private final TypeInformation <?> type ;
276
- private final String path ;
277
-
278
- private TypeAndPath (TypeInformation <?> type , String path ) {
279
- this .type = type ;
280
- this .path = path ;
281
- }
282
+ record TypeAndPath (TypeInformation <?> type , String path ) {
282
283
283
284
public static TypeAndPath of (TypeInformation <?> type , String path ) {
284
285
return new TypeAndPath (type , path );
285
286
}
286
287
287
- public TypeInformation <?> getType () {
288
- return this .type ;
289
- }
290
-
291
- public String getPath () {
292
- return this .path ;
293
- }
294
-
295
- @ Override
296
- public boolean equals (@ Nullable Object o ) {
297
-
298
- if (this == o ) {
299
- return true ;
300
- }
301
-
302
- if (!(o instanceof TypeAndPath that )) {
303
- return false ;
304
- }
305
-
306
- if (!ObjectUtils .nullSafeEquals (type , that .type )) {
307
- return false ;
308
- }
309
-
310
- return ObjectUtils .nullSafeEquals (path , that .path );
311
- }
312
-
313
- @ Override
314
- public int hashCode () {
315
- int result = ObjectUtils .nullSafeHashCode (type );
316
- result = 31 * result + ObjectUtils .nullSafeHashCode (path );
317
- return result ;
318
- }
319
-
320
- @ Override
321
- public String toString () {
322
- return "PersistentPropertyPathFactory.TypeAndPath(type=" + this .getType () + ", path=" + this .getPath () + ")" ;
323
- }
324
288
}
325
289
326
290
static class DefaultPersistentPropertyPaths <T , P extends PersistentProperty <P >>
@@ -389,7 +353,7 @@ public PersistentPropertyPaths<T, P> dropPathIfSegmentMatches(Predicate<? super
389
353
Assert .notNull (predicate , "Predicate must not be null" );
390
354
391
355
List <PersistentPropertyPath <P >> paths = this .stream () //
392
- .filter (it -> ! it .stream ().anyMatch (predicate )) //
356
+ .filter (it -> it .stream ().noneMatch (predicate )) //
393
357
.collect (Collectors .toList ());
394
358
395
359
return paths .equals (this .paths ) ? this : new DefaultPersistentPropertyPaths <>(type , paths );
@@ -408,17 +372,15 @@ public String toString() {
408
372
* @author Oliver Gierke
409
373
* @since 2.1
410
374
*/
411
- private enum ShortestSegmentFirst
412
- implements Comparator <PersistentPropertyPath <? extends PersistentProperty <?>>> {
375
+ private enum ShortestSegmentFirst implements Comparator <PersistentPropertyPath <? extends PersistentProperty <?>>> {
413
376
414
377
INSTANCE ;
415
378
416
379
@ Override
417
380
@ SuppressWarnings ("null" )
418
381
public int compare (PersistentPropertyPath <?> left , PersistentPropertyPath <?> right ) {
419
382
420
- Function <PersistentProperty <?>, Integer > mapper = (Function <PersistentProperty <?>, Integer >) it -> it .getName ()
421
- .length ();
383
+ Function <PersistentProperty <?>, Integer > mapper = it -> it .getName ().length ();
422
384
423
385
Stream <Integer > leftNames = left .stream ().map (mapper );
424
386
Stream <Integer > rightNames = right .stream ().map (mapper );
@@ -440,16 +402,22 @@ private static class PathResolution {
440
402
441
403
private final PersistentPropertyPath <?> path ;
442
404
private final boolean resolvable ;
443
- private String source , segment ;
444
- private TypeInformation <?> type ;
405
+ private @ Nullable final String source ;
445
406
446
- public PathResolution (PersistentPropertyPath <?> path ) {
407
+ private @ Nullable final String segment ;
408
+ private @ Nullable final TypeInformation <?> type ;
409
+
410
+ private PathResolution (PersistentPropertyPath <?> path ) {
447
411
448
412
this .path = path ;
449
413
this .resolvable = true ;
414
+ this .source = null ;
415
+ this .segment = null ;
416
+ this .type = null ;
450
417
}
451
418
452
- PathResolution (List <String > parts , String segment , TypeInformation <?> type , PersistentPropertyPath <?> path ) {
419
+ private PathResolution (List <String > parts , String segment , TypeInformation <?> type ,
420
+ PersistentPropertyPath <?> path ) {
453
421
454
422
this .source = StringUtils .collectionToDelimitedString (parts , "." );
455
423
this .segment = segment ;
@@ -458,15 +426,26 @@ public PathResolution(PersistentPropertyPath<?> path) {
458
426
this .resolvable = false ;
459
427
}
460
428
429
+ static PathResolution unresolved (List <String > parts , String segment , TypeInformation <?> type ,
430
+ PersistentPropertyPath <?> path ) {
431
+ return new PathResolution (parts , segment , type , path );
432
+ }
433
+
434
+ static PathResolution resolved (PersistentPropertyPath <?> path ) {
435
+ return new PathResolution (path );
436
+ }
437
+
461
438
/**
462
439
* @return the path if available.
463
440
* @throws InvalidPersistentPropertyPath when the path could not be resolved to an actual property
464
441
*/
465
- PersistentPropertyPath getResolvedPath () {
442
+ @ SuppressWarnings ("unchecked" )
443
+ <P extends PersistentProperty <P >> PersistentPropertyPath <P > getResolvedPath () {
466
444
467
445
if (resolvable ) {
468
- return path ;
446
+ return ( PersistentPropertyPath < P >) path ;
469
447
}
448
+
470
449
throw new InvalidPersistentPropertyPath (source , type , segment , path );
471
450
}
472
451
}
0 commit comments