88import java .util .Objects ;
99
1010import org .hibernate .Incubating ;
11- import org .hibernate .internal .util .StringHelper ;
1211
1312import org .checkerframework .checker .nullness .qual .Nullable ;
1413
14+ import static org .hibernate .internal .util .StringHelper .isEmpty ;
15+ import static org .hibernate .internal .util .StringHelper .nullIfEmpty ;
16+
1517/**
1618 * A compound name where the root path element is an entity name or a collection role
1719 * and each the path sub-path from the root references a domain or mapping model part
@@ -35,10 +37,9 @@ public NavigablePath(String localName) {
3537
3638 public NavigablePath (String rootName , @ Nullable String alias ) {
3739 this .parent = null ;
38- this .alias = alias = StringHelper . nullIfEmpty ( alias );
40+ this .alias = alias = nullIfEmpty ( alias );
3941 this .localName = rootName ;
4042 this .identifierForTableGroup = rootName ;
41-
4243 this .hashCode = localName .hashCode () + ( alias == null ? 0 : alias .hashCode () );
4344 }
4445
@@ -48,14 +49,12 @@ public NavigablePath(NavigablePath parent, String navigableName) {
4849
4950 public NavigablePath (NavigablePath parent , String localName , @ Nullable String alias ) {
5051 assert parent != null ;
51-
5252 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 + ')' ;
5958 this .hashCode = parent .hashCode () + aliasedLocalName .hashCode ();
6059
6160 // the _identifierMapper is a "hidden property" on entities with composite keys.
@@ -68,9 +67,10 @@ public NavigablePath(NavigablePath parent, String localName, @Nullable String al
6867 else {
6968 this .localName = localName ;
7069 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 ;
7474 }
7575 }
7676
@@ -83,7 +83,7 @@ public NavigablePath(
8383 this .parent = parent ;
8484 this .localName = localName ;
8585 this .hashCode = hashCode ;
86- this .alias = StringHelper . nullIfEmpty ( alias );
86+ this .alias = nullIfEmpty ( alias );
8787 this .identifierForTableGroup = identifierForTableGroup ;
8888 }
8989
@@ -119,32 +119,27 @@ public boolean equals(@Nullable Object other) {
119119 if ( this == other ) {
120120 return true ;
121121 }
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 ) ) {
129123 return false ;
130124 }
131-
132- if ( otherPath instanceof NavigablePath otherNavigablePath ) {
133- if ( ! Objects .equals ( getAlias (), otherNavigablePath .getAlias () ) ) {
125+ else {
126+ if ( !localNamesMatch ( otherPath ) ) {
134127 return false ;
135128 }
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+ }
137136 }
138-
139- return Objects .equals ( getParent (), otherPath .getParent () );
140137 }
141138
142139 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 () );
148143 }
149144
150145 protected boolean localNamesMatch (EntityIdentifierNavigablePath other ) {
@@ -192,11 +187,14 @@ public boolean isSuffix(@Nullable DotIdentifierSequence dotIdentifierSequence) {
192187 if ( dotIdentifierSequence == null ) {
193188 return true ;
194189 }
195- if ( !localNamesMatch ( dotIdentifierSequence ) ) {
190+ else if ( !localNamesMatch ( dotIdentifierSequence ) ) {
196191 return false ;
197192 }
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+ }
200198 }
201199
202200 /**
@@ -214,14 +212,15 @@ public boolean isSuffix(@Nullable DotIdentifierSequence dotIdentifierSequence) {
214212 if ( suffix == null ) {
215213 return this ;
216214 }
217- if ( !getLocalName ().equals ( suffix .getLocalName () ) ) {
215+ else if ( !getLocalName ().equals ( suffix .getLocalName () ) ) {
218216 return null ;
219217 }
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 ;
223223 }
224- return null ;
225224 }
226225
227226 /**
@@ -237,9 +236,13 @@ public boolean isParentOrEqual(@Nullable NavigablePath navigablePath) {
237236 return false ;
238237 }
239238
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+ );
243246 }
244247
245248 /**
@@ -255,7 +258,7 @@ public boolean pathsMatch(@Nullable NavigablePath p) {
255258 // - this = Root.sub.leaf.terminal
256259 // - result = leaf.terminal
257260
258- final RelativePathCollector pathCollector = new RelativePathCollector ();
261+ final var pathCollector = new RelativePathCollector ();
259262 relativize ( base , pathCollector );
260263 return pathCollector .resolve ();
261264 }
@@ -265,47 +268,48 @@ protected static class RelativePathCollector {
265268 private StringBuilder buffer ;
266269
267270 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 );
270280 }
271-
272- if ( buffer == null ) {
273- buffer = new StringBuilder ();
274- }
275- else {
276- buffer .append ( '.' );
277- }
278-
279- buffer .append ( path );
280281 }
281282
282283 public @ Nullable String resolve () {
283284 if ( buffer == null ) {
284285 // Return an empty string instead of null in case the two navigable paths are equal
285286 return matchedBase ? "" : null ;
286287 }
287- return buffer .toString ();
288+ else {
289+ return buffer .toString ();
290+ }
288291 }
289292 }
290293
291294 protected void relativize (NavigablePath base , RelativePathCollector collector ) {
292295 if ( this .equals ( base ) ) {
293296 collector .matchedBase = true ;
294- return ;
295297 }
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+ }
300303 }
304+ collector .collectPath ( getLocalName () );
301305 }
302-
303- collector .collectPath ( getLocalName () );
304306 }
305307
306308 @ Override
307309 public String getFullPath () {
308- return alias == null ? identifierForTableGroup : identifierForTableGroup + "(" + alias + ")" ;
310+ return alias == null
311+ ? identifierForTableGroup
312+ : identifierForTableGroup + "(" + alias + ")" ;
309313 }
310314
311315 @ Override
0 commit comments