23
23
import org .springframework .util .Assert ;
24
24
import org .springframework .util .StringUtils ;
25
25
26
+ import java .util .Objects ;
27
+
26
28
/**
27
29
* Represents a path within an aggregate starting from the aggregate root.
28
30
*
@@ -69,24 +71,14 @@ public boolean isRoot() {
69
71
return path == null ;
70
72
}
71
73
72
- /**
73
- * Tests if {@code this} and the argument represent the same path.
74
- *
75
- * @param path to which this path gets compared. May be {@literal null}.
76
- * @return Whence the argument matches the path represented by this instance.
77
- */
78
- public boolean matches (@ Nullable PersistentPropertyPath <RelationalPersistentProperty > path ) {
79
- return this .path == null ? path == null || path .isEmpty () : this .path .equals (path );
80
- }
81
-
82
74
/**
83
75
* The name of the column used to reference the id in the parent table.
84
76
*
85
77
* @throws IllegalStateException when called on an empty path.
86
78
*/
87
79
public SqlIdentifier getReverseColumnName () {
88
80
89
- Assert .state (path != null , "Empty paths don't have a reverse column name" );
81
+ Assert .state (! isRoot () , "Empty paths don't have a reverse column name" );
90
82
91
83
return path .getLeafProperty ().getReverseColumnName (this );
92
84
}
@@ -99,7 +91,7 @@ public SqlIdentifier getReverseColumnName() {
99
91
*/
100
92
public AggregatePath getParentPath () {
101
93
102
- if (path == null ) {
94
+ if (isRoot () ) {
103
95
throw new IllegalStateException ("The parent path of a root path is not defined." );
104
96
}
105
97
@@ -117,7 +109,27 @@ public AggregatePath getParentPath() {
117
109
*/
118
110
@ Nullable
119
111
public RelationalPersistentEntity <?> getLeafEntity () {
120
- return path == null ? rootType : context .getPersistentEntity (path .getLeafProperty ().getActualType ());
112
+ return isRoot () ? rootType : context .getPersistentEntity (path .getLeafProperty ().getActualType ());
113
+ }
114
+
115
+ /**
116
+ * The {@link RelationalPersistentEntity} associated with the leaf of this path or throw {@link IllegalStateException}
117
+ * if the leaf cannot be resolved.
118
+ *
119
+ * @return the required {@link RelationalPersistentEntity} associated with the leaf of this path.
120
+ * @throws IllegalStateException if the persistent entity cannot be resolved.
121
+ */
122
+ public RelationalPersistentEntity <?> getRequiredLeafEntity () {
123
+
124
+ RelationalPersistentEntity <?> entity = getLeafEntity ();
125
+
126
+ if (entity == null ) {
127
+
128
+ throw new IllegalStateException (
129
+ String .format ("Couldn't resolve leaf PersistentEntity for type %s" , path .getLeafProperty ().getActualType ()));
130
+ }
131
+
132
+ return entity ;
121
133
}
122
134
123
135
/**
@@ -138,7 +150,7 @@ public AggregatePath getIdDefiningParentPath() {
138
150
139
151
AggregatePath parent = getParentPath ();
140
152
141
- if (parent . path == null ) {
153
+ if (isRoot () ) {
142
154
return parent ;
143
155
}
144
156
@@ -149,33 +161,13 @@ public AggregatePath getIdDefiningParentPath() {
149
161
return parent ;
150
162
}
151
163
152
- /**
153
- * The {@link RelationalPersistentEntity} associated with the leaf of this path or throw {@link IllegalStateException}
154
- * if the leaf cannot be resolved.
155
- *
156
- * @return the required {@link RelationalPersistentEntity} associated with the leaf of this path.
157
- * @throws IllegalStateException if the persistent entity cannot be resolved.
158
- */
159
- public RelationalPersistentEntity <?> getRequiredLeafEntity () {
160
-
161
- RelationalPersistentEntity <?> entity = getLeafEntity ();
162
-
163
- if (entity == null ) {
164
-
165
- throw new IllegalStateException (
166
- String .format ("Couldn't resolve leaf PersistentEntity for type %s" , path .getLeafProperty ().getActualType ()));
167
- }
168
-
169
- return entity ;
170
- }
171
-
172
164
public RelationalPersistentProperty getRequiredIdProperty () {
173
- return this . path == null ? rootType .getRequiredIdProperty () : getRequiredLeafEntity ().getRequiredIdProperty ();
165
+ return isRoot () ? rootType .getRequiredIdProperty () : getRequiredLeafEntity ().getRequiredIdProperty ();
174
166
175
167
}
176
168
177
169
public int getLength () {
178
- return path == null ? 0 : path .getLength ();
170
+ return isRoot () ? 0 : path .getLength ();
179
171
}
180
172
181
173
/**
@@ -185,7 +177,7 @@ public int getLength() {
185
177
*/
186
178
@ Nullable
187
179
public SqlIdentifier getQualifierColumn () {
188
- return path == null ? null : path .getLeafProperty ().getKeyColumn ();
180
+ return isRoot () ? null : path .getLeafProperty ().getKeyColumn ();
189
181
}
190
182
191
183
/**
@@ -196,7 +188,7 @@ public SqlIdentifier getQualifierColumn() {
196
188
@ Nullable
197
189
public Class <?> getQualifierColumnType () {
198
190
199
- if (path == null ) {
191
+ if (isRoot () ) {
200
192
return null ;
201
193
}
202
194
if (!path .getLeafProperty ().isQualified ()) {
@@ -211,14 +203,14 @@ public Class<?> getQualifierColumnType() {
211
203
* @return if the leaf property is embedded.
212
204
*/
213
205
public boolean isEmbedded () {
214
- return path != null && path .getLeafProperty ().isEmbedded ();
206
+ return ! isRoot () && path .getLeafProperty ().isEmbedded ();
215
207
}
216
208
217
209
/**
218
210
* @return {@literal true} when this is an empty path or the path references an entity.
219
211
*/
220
212
public boolean isEntity () {
221
- return path == null || path .getLeafProperty ().isEntity ();
213
+ return isRoot () || path .getLeafProperty ().isEntity ();
222
214
}
223
215
224
216
/**
@@ -235,7 +227,7 @@ private AggregatePath getTableOwningAncestor() {
235
227
@ Nullable
236
228
private SqlIdentifier assembleTableAlias () {
237
229
238
- Assert .state (path != null , "Path is null" );
230
+ Assert .state (! isRoot () , "Path is null" );
239
231
240
232
RelationalPersistentProperty leafProperty = path .getLeafProperty ();
241
233
String prefix ;
@@ -273,7 +265,7 @@ public SqlIdentifier getTableAlias() {
273
265
274
266
AggregatePath tableOwner = getTableOwningAncestor ();
275
267
276
- return tableOwner .path == null ? null : tableOwner .assembleTableAlias ();
268
+ return tableOwner .isRoot () ? null : tableOwner .assembleTableAlias ();
277
269
278
270
}
279
271
@@ -295,7 +287,7 @@ public SqlIdentifier getQualifiedTableName() {
295
287
*/
296
288
public SqlIdentifier getColumnName () {
297
289
298
- Assert .state (path != null , "Path is null" );
290
+ Assert .state (! isRoot () , "Path is null" );
299
291
300
292
return assembleColumnName (path .getLeafProperty ().getColumnName ());
301
293
}
@@ -323,12 +315,12 @@ public SqlIdentifier getReverseColumnNameAlias() {
323
315
public String toString () {
324
316
return "AggregatePath["
325
317
+ (rootType == null ? path .getBaseProperty ().getOwner ().getType ().getName () : rootType .getName ()) + "]"
326
- + ((path == null ) ? "/" : path .toDotPath ());
318
+ + ((isRoot () ) ? "/" : path .toDotPath ());
327
319
}
328
320
329
321
private SqlIdentifier assembleColumnName (SqlIdentifier suffix ) {
330
322
331
- Assert .state (path != null , "Path is null" );
323
+ Assert .state (! isRoot () , "Path is null" );
332
324
333
325
if (path .getLength () <= 1 ) {
334
326
return suffix ;
@@ -353,7 +345,7 @@ private SqlIdentifier prefixWithTableAlias(SqlIdentifier columnName) {
353
345
}
354
346
355
347
public String toDotPath () {
356
- return path == null ? "" : path .toDotPath ();
348
+ return isRoot () ? "" : path .toDotPath ();
357
349
}
358
350
359
351
/**
@@ -364,7 +356,7 @@ public String toDotPath() {
364
356
*/
365
357
public boolean isMultiValued () {
366
358
367
- return path != null && //
359
+ return ! isRoot () && //
368
360
(path .getLeafProperty ().isCollectionLike () //
369
361
|| path .getLeafProperty ().isQualified () //
370
362
|| getParentPath ().isMultiValued () //
@@ -376,27 +368,27 @@ public boolean isMultiValued() {
376
368
* @see RelationalPersistentProperty#isMap()
377
369
*/
378
370
public boolean isMap () {
379
- return path != null && path .getLeafProperty ().isMap ();
371
+ return ! isRoot () && path .getLeafProperty ().isMap ();
380
372
}
381
373
382
374
/**
383
375
* @return {@literal true} when this is references a {@link java.util.List} or {@link java.util.Map}.
384
376
*/
385
377
public boolean isQualified () {
386
- return path != null && path .getLeafProperty ().isQualified ();
378
+ return ! isRoot () && path .getLeafProperty ().isQualified ();
387
379
}
388
380
389
381
public RelationalPersistentProperty getRequiredLeafProperty () {
390
382
391
- if (path == null ) {
383
+ if (isRoot () ) {
392
384
throw new IllegalStateException ("root path does not have a leaf property" );
393
385
}
394
386
return path .getLeafProperty ();
395
387
}
396
388
397
389
public RelationalPersistentProperty getBaseProperty () {
398
390
399
- if (path == null ) {
391
+ if (isRoot () ) {
400
392
throw new IllegalStateException ("root path does not have a base property" );
401
393
}
402
394
return path .getBaseProperty ();
@@ -413,15 +405,15 @@ public SqlIdentifier getIdColumnName() {
413
405
* @return {@literal true} when this is references a {@link java.util.Collection} or an array.
414
406
*/
415
407
public boolean isCollectionLike () {
416
- return path != null && path .getLeafProperty ().isCollectionLike ();
408
+ return ! isRoot () && path .getLeafProperty ().isCollectionLike ();
417
409
}
418
410
419
411
/**
420
412
* @return whether the leaf end of the path is ordered, i.e. the data to populate must be ordered.
421
413
* @see RelationalPersistentProperty#isOrdered()
422
414
*/
423
415
public boolean isOrdered () {
424
- return path != null && path .getLeafProperty ().isOrdered ();
416
+ return ! isRoot () && path .getLeafProperty ().isOrdered ();
425
417
}
426
418
427
419
/**
@@ -430,9 +422,9 @@ public boolean isOrdered() {
430
422
* @param property must not be {@literal null}.
431
423
* @return Guaranteed to be not {@literal null}.
432
424
*/
433
- public AggregatePath extendBy (RelationalPersistentProperty property ) {
425
+ public AggregatePath append (RelationalPersistentProperty property ) {
434
426
435
- PersistentPropertyPath <? extends RelationalPersistentProperty > newPath = path == null //
427
+ PersistentPropertyPath <? extends RelationalPersistentProperty > newPath = isRoot () //
436
428
? context .getPersistentPropertyPath (property .getName (), rootType .getType ()) //
437
429
: context .getPersistentPropertyPath (path .toDotPath () + "." + property .getName (),
438
430
path .getBaseProperty ().getOwner ().getType ());
@@ -442,7 +434,7 @@ public AggregatePath extendBy(RelationalPersistentProperty property) {
442
434
443
435
public PersistentPropertyPath <? extends RelationalPersistentProperty > getRequiredPersistentPropertyPath () {
444
436
445
- Assert .state (path != null , "path must not be null" );
437
+ Assert .state (! isRoot () , "path must not be null" );
446
438
return path ;
447
439
}
448
440
@@ -453,15 +445,30 @@ public PersistentPropertyPath<? extends RelationalPersistentProperty> getRequire
453
445
public SqlIdentifier getEffectiveIdColumnName () {
454
446
455
447
AggregatePath owner = getTableOwningAncestor ();
456
- return owner .path == null ? owner .getRequiredLeafEntity ().getIdColumn () : owner .getReverseColumnName ();
448
+ return owner .isRoot () ? owner .getRequiredLeafEntity ().getIdColumn () : owner .getReverseColumnName ();
457
449
}
458
450
459
451
@ Nullable
460
452
public PersistentPropertyPathExtension getPathExtension () {
461
453
462
- if (path == null ) {
454
+ if (isRoot () ) {
463
455
return new PersistentPropertyPathExtension (context , rootType );
464
456
}
465
457
return new PersistentPropertyPathExtension (context , path );
466
458
}
459
+
460
+ @ Override
461
+ public boolean equals (Object o ) {
462
+
463
+ if (this == o ) return true ;
464
+ if (o == null || getClass () != o .getClass ()) return false ;
465
+ AggregatePath that = (AggregatePath ) o ;
466
+ return Objects .equals (context , that .context ) && Objects .equals (rootType , that .rootType ) && Objects .equals (path , that .path );
467
+ }
468
+
469
+ @ Override
470
+ public int hashCode () {
471
+
472
+ return Objects .hash (context , rootType , path );
473
+ }
467
474
}
0 commit comments