Skip to content

Commit 424460a

Browse files
committed
move Caching to spi package as planned
clean it up a bit
1 parent 9303f1a commit 424460a

File tree

8 files changed

+70
-73
lines changed

8 files changed

+70
-73
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/TruthValue.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@ public enum TruthValue {
1818
FALSE,
1919
UNKNOWN;
2020

21+
public static TruthValue of(boolean bool) {
22+
return bool ? TRUE : FALSE;
23+
}
24+
2125
public boolean toBoolean(boolean defaultValue) {
22-
switch (this) {
23-
case TRUE:
24-
return true;
25-
case FALSE:
26-
return false;
27-
default:
28-
return defaultValue;
29-
}
26+
return switch ( this ) {
27+
case TRUE -> true;
28+
case FALSE -> false;
29+
default -> defaultValue;
30+
};
3031
}
3132

33+
/**
34+
* @deprecated No longer used
35+
*/
36+
@Deprecated(since = "7", forRemoval = true)
3237
public static boolean toBoolean(TruthValue value, boolean defaultValue) {
3338
return value != null ? value.toBoolean( defaultValue ) : defaultValue;
3439
}

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/AbstractPluralAttributeSourceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType;
1818
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSynchronizeType;
1919
import org.hibernate.boot.jaxb.hbm.spi.PluralAttributeInfo;
20-
import org.hibernate.boot.model.Caching;
20+
import org.hibernate.boot.model.source.spi.Caching;
2121
import org.hibernate.boot.model.CustomSql;
2222
import org.hibernate.boot.model.source.spi.AttributePath;
2323
import org.hibernate.boot.model.source.spi.AttributeRole;

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/EntityHierarchySourceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmPolymorphismEnum;
1919
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType;
2020
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSimpleIdType;
21-
import org.hibernate.boot.model.Caching;
21+
import org.hibernate.boot.model.source.spi.Caching;
2222
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
2323
import org.hibernate.boot.model.naming.EntityNaming;
2424
import org.hibernate.boot.model.source.spi.DiscriminatorSource;

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/Helper.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmUnionSubclassEntityType;
2121
import org.hibernate.boot.jaxb.hbm.spi.TableInformationContainer;
2222
import org.hibernate.boot.jaxb.hbm.spi.ToolingHintContainer;
23-
import org.hibernate.boot.model.Caching;
23+
import org.hibernate.boot.model.source.spi.Caching;
2424
import org.hibernate.boot.model.CustomSql;
25-
import org.hibernate.boot.model.TruthValue;
2625
import org.hibernate.boot.model.source.spi.InheritanceType;
2726
import org.hibernate.boot.model.source.spi.SizeSource;
2827
import org.hibernate.boot.model.source.spi.TableSpecificationSource;
@@ -75,32 +74,26 @@ public static CustomSql buildCustomSql(JaxbHbmCustomSqlDmlType customSqlElement)
7574
}
7675

7776
public static Caching createCaching(JaxbHbmCacheType cacheElement) {
78-
if ( cacheElement == null ) {
79-
return new Caching( TruthValue.UNKNOWN );
80-
}
81-
else {
82-
return new Caching(
83-
cacheElement.getRegion(),
84-
cacheElement.getUsage(),
85-
cacheElement.getInclude() == null
86-
|| !"non-lazy".equals( cacheElement.getInclude().value() ),
87-
TruthValue.TRUE
88-
);
89-
}
77+
return cacheElement == null
78+
? new Caching()
79+
: new Caching(
80+
cacheElement.getRegion(),
81+
cacheElement.getUsage(),
82+
cacheElement.getInclude() == null
83+
|| !"non-lazy".equals( cacheElement.getInclude().value() ),
84+
true
85+
);
9086
}
9187

9288
public static Caching createNaturalIdCaching(JaxbHbmNaturalIdCacheType cacheElement) {
93-
if ( cacheElement == null ) {
94-
return new Caching( TruthValue.UNKNOWN );
95-
}
96-
else {
97-
return new Caching(
98-
nullIfEmpty( cacheElement.getRegion() ),
99-
null,
100-
false,
101-
TruthValue.TRUE
102-
);
103-
}
89+
return cacheElement == null
90+
? new Caching()
91+
: new Caching(
92+
nullIfEmpty( cacheElement.getRegion() ),
93+
null,
94+
false,
95+
true
96+
);
10497
}
10598

10699
public static String getPropertyAccessorName(String access, boolean isEmbedded, String defaultAccess) {

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import org.hibernate.boot.jaxb.Origin;
2323
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNamedNativeQueryType;
2424
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNamedQueryType;
25-
import org.hibernate.boot.model.Caching;
25+
import org.hibernate.boot.model.source.spi.Caching;
2626
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
27-
import org.hibernate.boot.model.TruthValue;
2827
import org.hibernate.boot.model.TypeDefinition;
2928
import org.hibernate.boot.model.internal.FkSecondPass;
3029
import org.hibernate.boot.model.internal.SimpleToOneFkSecondPass;
@@ -278,7 +277,7 @@ private void bindRootEntity(EntityHierarchySourceImpl hierarchySource, RootClass
278277
);
279278

280279
if ( hierarchySource.getNaturalIdCaching() != null ) {
281-
if ( hierarchySource.getNaturalIdCaching().getRequested() == TruthValue.TRUE ) {
280+
if ( hierarchySource.getNaturalIdCaching().isRequested() ) {
282281
rootEntityDescriptor.setNaturalIdCacheRegionName( hierarchySource.getNaturalIdCaching().getRegion() );
283282
}
284283
}
@@ -303,7 +302,7 @@ private static boolean isCacheEnabled(MappingDocument mappingDocument, Caching c
303302
return switch ( mappingDocument.getBuildingOptions().getSharedCacheMode() ) {
304303
case UNSPECIFIED, ENABLE_SELECTIVE ->
305304
// this is default behavior for hbm.xml
306-
caching != null && caching.getRequested().toBoolean(false);
305+
caching != null && caching.isRequested(false);
307306
case NONE ->
308307
// this option is actually really useful
309308
false;
@@ -314,7 +313,7 @@ private static boolean isCacheEnabled(MappingDocument mappingDocument, Caching c
314313
case DISABLE_SELECTIVE ->
315314
// makes no sense for hbm.xml, and also goes against our
316315
// ideology, and so it hurts me to support it here
317-
caching == null || caching.getRequested().toBoolean(true);
316+
caching == null || caching.isRequested(true);
318317
};
319318
}
320319

hibernate-core/src/main/java/org/hibernate/boot/model/Caching.java renamed to hibernate-core/src/main/java/org/hibernate/boot/model/source/spi/Caching.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,40 @@
22
* SPDX-License-Identifier: LGPL-2.1-or-later
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.boot.model;
5+
package org.hibernate.boot.model.source.spi;
66

77
import org.hibernate.boot.CacheRegionDefinition;
8+
import org.hibernate.boot.model.TruthValue;
89
import org.hibernate.cache.spi.access.AccessType;
9-
import org.hibernate.internal.util.StringHelper;
10+
11+
import static org.hibernate.internal.util.StringHelper.isEmpty;
1012

1113
/**
1214
* Models the caching options for an entity, natural id, or collection.
1315
*
1416
* @author Steve Ebersole
1517
* @author Hardy Ferentschik
16-
*
17-
* @deprecated will move to {@link org.hibernate.boot.model.source.spi}, where its only uses are
1818
*/
19-
@Deprecated(since = "6") // because it is moving packages
2019
public class Caching {
2120
// NOTE : TruthValue for now because I need to look at how JPA's SharedCacheMode concept is handled
2221
private TruthValue requested;
2322
private String region;
2423
private AccessType accessType;
2524
private boolean cacheLazyProperties;
2625

27-
public Caching(TruthValue requested) {
28-
this.requested = requested;
26+
public Caching() {
27+
this.requested = TruthValue.UNKNOWN;
2928
}
3029

3130
public Caching(String region, AccessType accessType, boolean cacheLazyProperties) {
32-
this( region, accessType, cacheLazyProperties, TruthValue.UNKNOWN );
31+
this.requested = TruthValue.UNKNOWN;
32+
this.region = region;
33+
this.accessType = accessType;
34+
this.cacheLazyProperties = cacheLazyProperties;
3335
}
3436

35-
public Caching(String region, AccessType accessType, boolean cacheLazyProperties, TruthValue requested) {
36-
this.requested = requested;
37+
public Caching(String region, AccessType accessType, boolean cacheLazyProperties, boolean requested) {
38+
this.requested = TruthValue.of( requested );
3739
this.region = region;
3840
this.accessType = accessType;
3941
this.cacheLazyProperties = cacheLazyProperties;
@@ -63,37 +65,37 @@ public void setCacheLazyProperties(boolean cacheLazyProperties) {
6365
this.cacheLazyProperties = cacheLazyProperties;
6466
}
6567

66-
public TruthValue getRequested() {
67-
return requested;
68+
public boolean isRequested() {
69+
return requested == TruthValue.TRUE;
6870
}
6971

70-
public void setRequested(TruthValue requested) {
71-
this.requested = requested;
72+
public boolean isRequested(boolean defaultValue) {
73+
return requested == TruthValue.UNKNOWN ? defaultValue : isRequested();
7274
}
7375

74-
public void overlay(CacheRegionDefinition overrides) {
75-
if ( overrides == null ) {
76-
return;
77-
}
76+
public void setRequested(boolean requested) {
77+
this.requested = TruthValue.of(requested);
78+
}
7879

79-
requested = TruthValue.TRUE;
80-
accessType = AccessType.fromExternalName( overrides.getUsage() );
81-
if ( StringHelper.isEmpty( overrides.getRegion() ) ) {
82-
region = overrides.getRegion();
80+
public void overlay(CacheRegionDefinition overrides) {
81+
if ( overrides != null ) {
82+
requested = TruthValue.TRUE;
83+
accessType = AccessType.fromExternalName( overrides.getUsage() );
84+
if ( isEmpty( overrides.getRegion() ) ) {
85+
region = overrides.getRegion();
86+
}
87+
// ugh, primitive boolean
88+
cacheLazyProperties = overrides.isCacheLazy();
8389
}
84-
// ugh, primitive boolean
85-
cacheLazyProperties = overrides.isCacheLazy();
8690
}
8791

8892
public void overlay(Caching overrides) {
89-
if ( overrides == null ) {
90-
return;
93+
if ( overrides != null ) {
94+
this.requested = overrides.requested;
95+
this.accessType = overrides.accessType;
96+
this.region = overrides.region;
97+
this.cacheLazyProperties = overrides.cacheLazyProperties;
9198
}
92-
93-
this.requested = overrides.requested;
94-
this.accessType = overrides.accessType;
95-
this.region = overrides.region;
96-
this.cacheLazyProperties = overrides.cacheLazyProperties;
9799
}
98100

99101
@Override

hibernate-core/src/main/java/org/hibernate/boot/model/source/spi/EntityHierarchySource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.hibernate.boot.model.source.spi;
66

7-
import org.hibernate.boot.model.Caching;
87
import org.hibernate.engine.OptimisticLockStyle;
98

109
/**

hibernate-core/src/main/java/org/hibernate/boot/model/source/spi/PluralAttributeSource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.hibernate.boot.model.source.spi;
66

7-
import org.hibernate.boot.model.Caching;
87
import org.hibernate.boot.model.CustomSql;
98

109
/**

0 commit comments

Comments
 (0)