8
8
import java .util .Objects ;
9
9
10
10
import org .hibernate .Incubating ;
11
- import org .hibernate .internal .util .StringHelper ;
12
11
13
12
import org .checkerframework .checker .nullness .qual .Nullable ;
14
13
14
+ import static org .hibernate .internal .util .StringHelper .isEmpty ;
15
+ import static org .hibernate .internal .util .StringHelper .nullIfEmpty ;
16
+
15
17
/**
16
18
* A compound name where the root path element is an entity name or a collection role
17
19
* and each the path sub-path from the root references a domain or mapping model part
@@ -35,10 +37,9 @@ public NavigablePath(String localName) {
35
37
36
38
public NavigablePath (String rootName , @ Nullable String alias ) {
37
39
this .parent = null ;
38
- this .alias = alias = StringHelper . nullIfEmpty ( alias );
40
+ this .alias = alias = nullIfEmpty ( alias );
39
41
this .localName = rootName ;
40
42
this .identifierForTableGroup = rootName ;
41
-
42
43
this .hashCode = localName .hashCode () + ( alias == null ? 0 : alias .hashCode () );
43
44
}
44
45
@@ -48,14 +49,12 @@ public NavigablePath(NavigablePath parent, String navigableName) {
48
49
49
50
public NavigablePath (NavigablePath parent , String localName , @ Nullable String alias ) {
50
51
assert parent != null ;
51
-
52
52
this .parent = parent ;
53
- this .alias = alias = StringHelper .nullIfEmpty ( alias );
54
-
55
- final String aliasedLocalName = alias == null
56
- ? localName
57
- : localName + '(' + alias + ')' ;
58
-
53
+ this .alias = alias = nullIfEmpty ( alias );
54
+ final String aliasedLocalName =
55
+ alias == null
56
+ ? localName
57
+ : localName + '(' + alias + ')' ;
59
58
this .hashCode = parent .hashCode () + aliasedLocalName .hashCode ();
60
59
61
60
// the _identifierMapper is a "hidden property" on entities with composite keys.
@@ -68,9 +67,10 @@ public NavigablePath(NavigablePath parent, String localName, @Nullable String al
68
67
else {
69
68
this .localName = localName ;
70
69
final String parentFullPath = parent .getFullPath ();
71
- this .identifierForTableGroup = StringHelper .isEmpty ( parentFullPath )
72
- ? aliasedLocalName
73
- : parentFullPath + "." + localName ;
70
+ this .identifierForTableGroup =
71
+ isEmpty ( parentFullPath )
72
+ ? aliasedLocalName
73
+ : parentFullPath + "." + localName ;
74
74
}
75
75
}
76
76
@@ -83,7 +83,7 @@ public NavigablePath(
83
83
this .parent = parent ;
84
84
this .localName = localName ;
85
85
this .hashCode = hashCode ;
86
- this .alias = StringHelper . nullIfEmpty ( alias );
86
+ this .alias = nullIfEmpty ( alias );
87
87
this .identifierForTableGroup = identifierForTableGroup ;
88
88
}
89
89
@@ -119,32 +119,27 @@ public boolean equals(@Nullable Object other) {
119
119
if ( this == other ) {
120
120
return true ;
121
121
}
122
-
123
- if ( other == null ) {
124
- return false ;
125
- }
126
-
127
- final DotIdentifierSequence otherPath = (DotIdentifierSequence ) other ;
128
- if ( ! localNamesMatch ( otherPath ) ) {
122
+ else if ( !(other instanceof DotIdentifierSequence otherPath ) ) {
129
123
return false ;
130
124
}
131
-
132
- if ( otherPath instanceof NavigablePath otherNavigablePath ) {
133
- if ( ! Objects .equals ( getAlias (), otherNavigablePath .getAlias () ) ) {
125
+ else {
126
+ if ( !localNamesMatch ( otherPath ) ) {
134
127
return false ;
135
128
}
136
- return Objects .equals ( getRealParent (), otherNavigablePath .getRealParent () );
129
+ else if ( otherPath instanceof NavigablePath otherNavigablePath ) {
130
+ return Objects .equals ( getAlias (), otherNavigablePath .getAlias () )
131
+ && Objects .equals ( getRealParent (), otherNavigablePath .getRealParent () );
132
+ }
133
+ else {
134
+ return Objects .equals ( getParent (), otherPath .getParent () );
135
+ }
137
136
}
138
-
139
- return Objects .equals ( getParent (), otherPath .getParent () );
140
137
}
141
138
142
139
protected boolean localNamesMatch (DotIdentifierSequence other ) {
143
- if ( other instanceof EntityIdentifierNavigablePath entityIdentifierNavigablePath ) {
144
- return localNamesMatch ( entityIdentifierNavigablePath );
145
- }
146
-
147
- return Objects .equals ( getLocalName (), other .getLocalName () );
140
+ return other instanceof EntityIdentifierNavigablePath entityIdentifierNavigablePath
141
+ ? localNamesMatch ( entityIdentifierNavigablePath )
142
+ : Objects .equals ( getLocalName (), other .getLocalName () );
148
143
}
149
144
150
145
protected boolean localNamesMatch (EntityIdentifierNavigablePath other ) {
@@ -192,11 +187,14 @@ public boolean isSuffix(@Nullable DotIdentifierSequence dotIdentifierSequence) {
192
187
if ( dotIdentifierSequence == null ) {
193
188
return true ;
194
189
}
195
- if ( !localNamesMatch ( dotIdentifierSequence ) ) {
190
+ else if ( !localNamesMatch ( dotIdentifierSequence ) ) {
196
191
return false ;
197
192
}
198
- NavigablePath parent = getParent ();
199
- return parent != null && parent .isSuffix ( dotIdentifierSequence .getParent () );
193
+ else {
194
+ final NavigablePath parent = getParent ();
195
+ return parent != null
196
+ && parent .isSuffix ( dotIdentifierSequence .getParent () );
197
+ }
200
198
}
201
199
202
200
/**
@@ -214,14 +212,15 @@ public boolean isSuffix(@Nullable DotIdentifierSequence dotIdentifierSequence) {
214
212
if ( suffix == null ) {
215
213
return this ;
216
214
}
217
- if ( !getLocalName ().equals ( suffix .getLocalName () ) ) {
215
+ else if ( !getLocalName ().equals ( suffix .getLocalName () ) ) {
218
216
return null ;
219
217
}
220
- NavigablePath parent = getParent ();
221
- if ( parent != null ) {
222
- return parent .trimSuffix ( suffix .getParent () );
218
+ else {
219
+ final NavigablePath parent = getParent ();
220
+ return parent != null
221
+ ? parent .trimSuffix ( suffix .getParent () )
222
+ : null ;
223
223
}
224
- return null ;
225
224
}
226
225
227
226
/**
@@ -237,9 +236,13 @@ public boolean isParentOrEqual(@Nullable NavigablePath navigablePath) {
237
236
return false ;
238
237
}
239
238
240
- public boolean pathsMatch (@ Nullable NavigablePath p ) {
241
- return this == p || p != null && localName .equals ( p .localName )
242
- && ( parent == null ? p .parent == null && Objects .equals ( alias , p .alias ) : parent .pathsMatch ( p .parent ) );
239
+ public boolean pathsMatch (@ Nullable NavigablePath path ) {
240
+ return this == path
241
+ || path != null && localName .equals ( path .localName ) && (
242
+ parent == null
243
+ ? path .parent == null && Objects .equals ( alias , path .alias )
244
+ : parent .pathsMatch ( path .parent )
245
+ );
243
246
}
244
247
245
248
/**
@@ -255,7 +258,7 @@ public boolean pathsMatch(@Nullable NavigablePath p) {
255
258
// - this = Root.sub.leaf.terminal
256
259
// - result = leaf.terminal
257
260
258
- final RelativePathCollector pathCollector = new RelativePathCollector ();
261
+ final var pathCollector = new RelativePathCollector ();
259
262
relativize ( base , pathCollector );
260
263
return pathCollector .resolve ();
261
264
}
@@ -265,47 +268,48 @@ protected static class RelativePathCollector {
265
268
private StringBuilder buffer ;
266
269
267
270
public void collectPath (String path ) {
268
- if ( !matchedBase ) {
269
- return ;
271
+ if ( matchedBase ) {
272
+ if ( buffer == null ) {
273
+ buffer = new StringBuilder ();
274
+ }
275
+ else {
276
+ buffer .append ( '.' );
277
+ }
278
+
279
+ buffer .append ( path );
270
280
}
271
-
272
- if ( buffer == null ) {
273
- buffer = new StringBuilder ();
274
- }
275
- else {
276
- buffer .append ( '.' );
277
- }
278
-
279
- buffer .append ( path );
280
281
}
281
282
282
283
public @ Nullable String resolve () {
283
284
if ( buffer == null ) {
284
285
// Return an empty string instead of null in case the two navigable paths are equal
285
286
return matchedBase ? "" : null ;
286
287
}
287
- return buffer .toString ();
288
+ else {
289
+ return buffer .toString ();
290
+ }
288
291
}
289
292
}
290
293
291
294
protected void relativize (NavigablePath base , RelativePathCollector collector ) {
292
295
if ( this .equals ( base ) ) {
293
296
collector .matchedBase = true ;
294
- return ;
295
297
}
296
-
297
- if ( ! collector .matchedBase ) {
298
- if ( parent != null ) {
299
- parent .relativize ( base , collector );
298
+ else {
299
+ if ( !collector .matchedBase ) {
300
+ if ( parent != null ) {
301
+ parent .relativize ( base , collector );
302
+ }
300
303
}
304
+ collector .collectPath ( getLocalName () );
301
305
}
302
-
303
- collector .collectPath ( getLocalName () );
304
306
}
305
307
306
308
@ Override
307
309
public String getFullPath () {
308
- return alias == null ? identifierForTableGroup : identifierForTableGroup + "(" + alias + ")" ;
310
+ return alias == null
311
+ ? identifierForTableGroup
312
+ : identifierForTableGroup + "(" + alias + ")" ;
309
313
}
310
314
311
315
@ Override
0 commit comments