Skip to content

Commit 79ae586

Browse files
committed
minor cleanups to NavigableRole and NavigablePath
1 parent e6f5284 commit 79ae586

File tree

2 files changed

+88
-84
lines changed

2 files changed

+88
-84
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/NavigableRole.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
package org.hibernate.metamodel.model.domain;
66

77
import java.io.Serializable;
8+
import java.util.Objects;
89

910
import org.hibernate.Incubating;
10-
import org.hibernate.internal.util.StringHelper;
1111
import org.hibernate.spi.DotIdentifierSequence;
1212
import org.hibernate.spi.NavigablePath;
1313

14+
import static org.hibernate.internal.util.StringHelper.isEmpty;
15+
1416
/**
1517
* A compound path which represents a {@link org.hibernate.metamodel.mapping.ModelPart}
1618
* and uniquely identifies it with the runtime metamodel.
@@ -41,29 +43,26 @@ public NavigableRole(NavigableRole parent, String localName) {
4143
public NavigableRole(NavigableRole parent, String localName, char separator) {
4244
this.parent = parent;
4345
this.localName = localName;
46+
this.fullPath = fullPath( parent, localName, separator );
47+
}
4448

49+
private String fullPath(NavigableRole parent, String localName, char separator) {
4550
// the _identifierMapper is a "hidden" property on entities with composite keys.
4651
// concatenating it will prevent the path from correctly being used to look up
4752
// various things such as criteria paths and fetch profile association paths
4853
if ( IDENTIFIER_MAPPER_PROPERTY.equals( localName ) ) {
49-
this.fullPath = parent != null ? parent.getFullPath() : "";
54+
return parent == null ? "" : parent.getFullPath();
5055
}
5156
else {
5257
final String prefix;
5358
if ( parent != null ) {
5459
final String resolvedParent = parent.getFullPath();
55-
if ( StringHelper.isEmpty( resolvedParent ) ) {
56-
prefix = "";
57-
}
58-
else {
59-
prefix = resolvedParent + separator;
60-
}
60+
prefix = isEmpty( resolvedParent ) ? "" : resolvedParent + separator;
6161
}
6262
else {
6363
prefix = "";
6464
}
65-
66-
this.fullPath = prefix + localName;
65+
return prefix + localName;
6766
}
6867
}
6968

@@ -80,10 +79,10 @@ public NavigableRole append(String name) {
8079
}
8180

8281
/**
83-
* Uses `#` as the separator rather than `.`. The intention being that the incoming name is a
84-
* {@link org.hibernate.metamodel.mapping.ModelPartContainer} of some sort
85-
*
86-
* todo (6.0) : better name?
82+
* Uses {@code #} as the separator rather than a period,
83+
* the intention being that the incoming name is a
84+
* {@link org.hibernate.metamodel.mapping.ModelPartContainer}
85+
* of some sort.
8786
*/
8887
public NavigableRole appendContainer(String name) {
8988
return new NavigableRole( this, name, '#' );
@@ -112,20 +111,21 @@ public String toString() {
112111
}
113112

114113
@Override
115-
public boolean equals(final Object o) {
116-
if ( this == o ) {
114+
public boolean equals(final Object object) {
115+
if ( this == object ) {
117116
return true;
118117
}
119-
if ( o == null || NavigableRole.class != o.getClass() ) {
118+
else if ( !(object instanceof NavigableRole that) ) {
120119
return false;
121120
}
122-
NavigableRole that = (NavigableRole) o;
123-
return fullPath.equals( that.fullPath );
121+
else {
122+
return Objects.equals( this.fullPath, that.fullPath );
123+
}
124124
}
125125

126126
@Override
127127
public int hashCode() {
128-
return this.fullPath.hashCode();
128+
return fullPath.hashCode();
129129
}
130130

131131
}

hibernate-core/src/main/java/org/hibernate/spi/NavigablePath.java

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import java.util.Objects;
99

1010
import org.hibernate.Incubating;
11-
import org.hibernate.internal.util.StringHelper;
1211

1312
import 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

Comments
 (0)