Skip to content

Commit c15e5b1

Browse files
committed
some cleanup to SqmInterpretationsKey
1 parent 646b876 commit c15e5b1

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

Diff for: hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
import java.util.HashSet;
8989
import java.util.List;
9090
import java.util.Map;
91-
import java.util.function.Supplier;
91+
import java.util.function.BooleanSupplier;
9292

9393
import static java.util.Collections.emptyMap;
9494
import static org.hibernate.jpa.HibernateHints.HINT_CACHEABLE;
@@ -339,7 +339,7 @@ protected boolean resolveJdbcParameterTypeIfNecessary() {
339339
}
340340

341341
@Override
342-
public Supplier<Boolean> hasMultiValuedParameterBindingsChecker() {
342+
public BooleanSupplier hasMultiValuedParameterBindingsChecker() {
343343
return this::hasMultiValuedParameterBindings;
344344
}
345345

Diff for: hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmInterpretationsKey.java

+28-34
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
package org.hibernate.query.sqm.internal;
66

77
import java.util.Collection;
8-
import java.util.Iterator;
98
import java.util.Objects;
109
import java.util.Set;
11-
import java.util.function.Supplier;
10+
import java.util.function.BooleanSupplier;
1211

1312
import org.hibernate.LockOptions;
1413
import org.hibernate.engine.spi.LoadQueryInfluencers;
@@ -18,20 +17,19 @@
1817
import org.hibernate.query.spi.QueryOptions;
1918
import org.hibernate.query.sqm.tree.SqmStatement;
2019

21-
import static java.lang.Boolean.TRUE;
2220

2321
/**
2422
* @author Steve Ebersole
2523
*/
26-
public final class SqmInterpretationsKey implements QueryInterpretationCache.Key {
24+
final class SqmInterpretationsKey implements QueryInterpretationCache.Key {
2725
public interface CacheabilityInfluencers {
2826
boolean isQueryPlanCacheable();
2927
String getQueryString();
3028
Object getQueryStringCacheKey();
3129
SqmStatement<?> getSqmStatement();
3230
QueryOptions getQueryOptions();
3331
LoadQueryInfluencers getLoadQueryInfluencers();
34-
Supplier<Boolean> hasMultiValuedParameterBindingsChecker();
32+
BooleanSupplier hasMultiValuedParameterBindingsChecker();
3533
}
3634

3735
public interface InterpretationsKeySource extends CacheabilityInfluencers {
@@ -61,17 +59,15 @@ private static Collection<String> memoryEfficientDefensiveSetCopy(final Set<Stri
6159
return null;
6260
}
6361
else {
64-
switch ( set.size() ) {
65-
case 0:
66-
return null;
67-
case 1:
68-
return Set.of( set.iterator().next() );
69-
case 2:
70-
final Iterator<String> iterator = set.iterator();
71-
return Set.of( iterator.next(), iterator.next() );
72-
default:
73-
return Set.copyOf( set );
74-
}
62+
return switch ( set.size() ) {
63+
case 0 -> null;
64+
case 1 -> Set.of( set.iterator().next() );
65+
case 2 -> {
66+
final var iterator = set.iterator();
67+
yield Set.of( iterator.next(), iterator.next() );
68+
}
69+
default -> Set.copyOf( set );
70+
};
7571
}
7672
}
7773

@@ -83,7 +79,7 @@ private static boolean isCacheable(InterpretationsKeySource keySource) {
8379
// parameters are part of the query string; with Criteria, they're not.
8480
return keySource.isQueryPlanCacheable()
8581
// At the moment we cannot cache query plan if there is filter enabled.
86-
&& ! keySource.getLoadQueryInfluencers().hasEnabledFilters()
82+
&& !keySource.getLoadQueryInfluencers().hasEnabledFilters()
8783
// At the moment we cannot cache query plan if it has an entity graph
8884
&& keySource.getQueryOptions().getAppliedGraph().getSemantic() == null
8985
// todo (6.0) : this one may be ok because of how I implemented multi-valued param handling
@@ -92,7 +88,7 @@ private static boolean isCacheable(InterpretationsKeySource keySource) {
9288
// in ConcreteSqmSelectQueryPlan is a concurrency issue when cached
9389
// - This could be solved by using a method-local clone of domainParameterXref
9490
// when multi-valued params exist
95-
&& ! keySource.hasMultiValuedParameterBindingsChecker().get() == TRUE;
91+
&& !keySource.hasMultiValuedParameterBindingsChecker().getAsBoolean();
9692
}
9793

9894
public static QueryInterpretationCache.Key generateNonSelectKey(InterpretationsKeySource keyDetails) {
@@ -109,7 +105,7 @@ public static QueryInterpretationCache.Key generateNonSelectKey(InterpretationsK
109105
private final TupleTransformer<?> tupleTransformer;
110106
private final ResultListTransformer<?> resultListTransformer;
111107
private final Collection<String> enabledFetchProfiles;
112-
private final int hashcode;
108+
private final int hashCode;
113109

114110
private SqmInterpretationsKey(
115111
Object query,
@@ -120,7 +116,7 @@ private SqmInterpretationsKey(
120116
ResultListTransformer<?> resultListTransformer,
121117
Collection<String> enabledFetchProfiles) {
122118
this.query = query;
123-
this.hashcode = hash;
119+
this.hashCode = hash;
124120
this.resultType = resultType;
125121
this.lockOptions = lockOptions;
126122
this.tupleTransformer = tupleTransformer;
@@ -132,7 +128,7 @@ private SqmInterpretationsKey(
132128
public QueryInterpretationCache.Key prepareForStore() {
133129
return new SqmInterpretationsKey(
134130
query,
135-
hashcode,
131+
hashCode,
136132
resultType,
137133
// Since lock options might be mutable, we need a copy for the cache key
138134
lockOptions.makeDefensiveCopy(),
@@ -148,26 +144,24 @@ public String getQueryString() {
148144
}
149145

150146
@Override
151-
public boolean equals(Object o) {
152-
if ( this == o ) {
147+
public boolean equals(Object other) {
148+
if ( this == other ) {
153149
return true;
154150
}
155-
if ( o == null || SqmInterpretationsKey.class != o.getClass() ) {
151+
if ( !(other instanceof SqmInterpretationsKey that)) {
156152
return false;
157153
}
158-
159-
final SqmInterpretationsKey that = (SqmInterpretationsKey) o;
160-
return this.hashcode == o.hashCode() //check this first as some other checks are expensive
161-
&& query.equals( that.query )
162-
&& Objects.equals( resultType, that.resultType )
163-
&& Objects.equals( lockOptions, that.lockOptions )
164-
&& Objects.equals( tupleTransformer, that.tupleTransformer )
165-
&& Objects.equals( resultListTransformer, that.resultListTransformer )
166-
&& Objects.equals( enabledFetchProfiles, that.enabledFetchProfiles );
154+
return this.hashCode == that.hashCode //check this first as some other checks are expensive
155+
&& this.query.equals( that.query )
156+
&& Objects.equals( this.resultType, that.resultType )
157+
&& Objects.equals( this.lockOptions, that.lockOptions )
158+
&& Objects.equals( this.tupleTransformer, that.tupleTransformer )
159+
&& Objects.equals( this.resultListTransformer, that.resultListTransformer )
160+
&& Objects.equals( this.enabledFetchProfiles, that.enabledFetchProfiles );
167161
}
168162

169163
@Override
170164
public int hashCode() {
171-
return hashcode;
165+
return hashCode;
172166
}
173167
}

Diff for: hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Date;
1212
import java.util.List;
1313
import java.util.Map;
14-
import java.util.function.Supplier;
14+
import java.util.function.BooleanSupplier;
1515

1616
import jakarta.persistence.CacheRetrieveMode;
1717
import jakarta.persistence.CacheStoreMode;
@@ -522,7 +522,7 @@ public LoadQueryInfluencers getLoadQueryInfluencers() {
522522
}
523523

524524
@Override
525-
public Supplier<Boolean> hasMultiValuedParameterBindingsChecker() {
525+
public BooleanSupplier hasMultiValuedParameterBindingsChecker() {
526526
return this::hasMultiValuedParameterBindings;
527527
}
528528

0 commit comments

Comments
 (0)