use switch expressions in main enums

This commit is contained in:
Gavin King 2024-09-09 07:56:14 +02:00
parent eed7ec0837
commit 22aba27cff
18 changed files with 131 additions and 224 deletions

View File

@ -162,30 +162,17 @@ public enum CacheMode implements FindOption {
retrieveMode = CacheRetrieveMode.BYPASS; retrieveMode = CacheRetrieveMode.BYPASS;
} }
switch ( storeMode ) { return switch (storeMode) {
case USE: { case USE -> switch (retrieveMode) {
switch ( retrieveMode ) { case USE -> NORMAL;
case USE: case BYPASS -> PUT;
return NORMAL; };
case BYPASS: case BYPASS -> switch (retrieveMode) {
return PUT; case USE -> GET;
} case BYPASS -> IGNORE;
} };
case BYPASS: { // technically should combo CacheStoreMode#REFRESH and CacheRetrieveMode#USE be illegal?
switch ( retrieveMode ) { case REFRESH -> REFRESH;
case USE: };
return GET;
case BYPASS:
return IGNORE;
}
}
case REFRESH: {
// technically should combo CacheStoreMode#REFRESH and CacheRetrieveMode#USE be illegal?
return REFRESH;
}
default: {
throw new AssertionFailure( "Unrecognized CacheStoreMode: " + storeMode );
}
}
} }
} }

View File

@ -6,7 +6,7 @@
*/ */
package org.hibernate; package org.hibernate;
import org.hibernate.internal.util.StringHelper; import static org.hibernate.internal.util.StringHelper.isEmpty;
/** /**
* Indicates the manner in which JDBC {@linkplain java.sql.Connection connections} * Indicates the manner in which JDBC {@linkplain java.sql.Connection connections}
@ -32,11 +32,9 @@ public enum ConnectionAcquisitionMode {
AS_NEEDED; AS_NEEDED;
public static ConnectionAcquisitionMode interpret(String value) { public static ConnectionAcquisitionMode interpret(String value) {
if ( "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value ) ) { return "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value )
return IMMEDIATELY; ? IMMEDIATELY
} : AS_NEEDED;
return AS_NEEDED;
} }
public static ConnectionAcquisitionMode interpret(Object setting) { public static ConnectionAcquisitionMode interpret(Object setting) {
@ -44,12 +42,12 @@ public enum ConnectionAcquisitionMode {
return null; return null;
} }
if ( setting instanceof ConnectionAcquisitionMode ) { if ( setting instanceof ConnectionAcquisitionMode mode ) {
return (ConnectionAcquisitionMode) setting; return mode;
} }
final String value = setting.toString(); final String value = setting.toString();
if ( StringHelper.isEmpty( value ) ) { if ( isEmpty( value ) ) {
return null; return null;
} }

View File

@ -8,7 +8,7 @@ package org.hibernate;
import java.util.Locale; import java.util.Locale;
import org.hibernate.internal.util.StringHelper; import static org.hibernate.internal.util.StringHelper.isEmpty;
/** /**
* Enumerates various policies for releasing JDBC {@linkplain java.sql.Connection * Enumerates various policies for releasing JDBC {@linkplain java.sql.Connection
@ -74,12 +74,12 @@ public enum ConnectionReleaseMode{
return null; return null;
} }
if ( setting instanceof ConnectionReleaseMode ) { if ( setting instanceof ConnectionReleaseMode mode ) {
return (ConnectionReleaseMode) setting; return mode;
} }
final String value = setting.toString(); final String value = setting.toString();
if ( StringHelper.isEmpty( value ) ) { if ( isEmpty( value ) ) {
return null; return null;
} }

View File

@ -83,18 +83,12 @@ public enum FlushMode {
} }
private int level() { private int level() {
switch (this) { return switch (this) {
case ALWAYS: case ALWAYS -> 20;
return 20; case AUTO -> 10;
case AUTO: case COMMIT -> 5;
return 10; case MANUAL -> 0;
case COMMIT: };
return 5;
case MANUAL:
return 0;
default:
throw new AssertionFailure("Impossible FlushMode");
}
} }
public static FlushMode fromJpaFlushMode(FlushModeType flushModeType) { public static FlushMode fromJpaFlushMode(FlushModeType flushModeType) {

View File

@ -245,27 +245,15 @@ public enum LockMode implements FindOption, RefreshOption {
* forced version increment will occur. * forced version increment will occur.
*/ */
private int level() { private int level() {
switch (this) { return switch (this) {
case NONE: case NONE -> 0;
return 0; case READ -> 1;
case READ: case OPTIMISTIC -> 2;
return 1; case OPTIMISTIC_FORCE_INCREMENT -> 3;
case OPTIMISTIC: case PESSIMISTIC_READ -> 4;
return 2; case UPGRADE_NOWAIT, UPGRADE_SKIPLOCKED, PESSIMISTIC_WRITE -> 5;
case OPTIMISTIC_FORCE_INCREMENT: case PESSIMISTIC_FORCE_INCREMENT, WRITE -> 6;
return 3; };
case PESSIMISTIC_READ:
return 4;
case UPGRADE_NOWAIT:
case UPGRADE_SKIPLOCKED:
case PESSIMISTIC_WRITE:
return 5;
case PESSIMISTIC_FORCE_INCREMENT:
case WRITE:
return 6;
default:
throw new AssertionFailure( "Unrecognized LockMode: " + this );
}
} }
public static LockMode fromExternalForm(String externalForm) { public static LockMode fromExternalForm(String externalForm) {
@ -291,29 +279,17 @@ public enum LockMode implements FindOption, RefreshOption {
* all other settings defaulted. * all other settings defaulted.
*/ */
public LockOptions toLockOptions() { public LockOptions toLockOptions() {
switch (this) { return switch (this) {
case NONE: case NONE -> LockOptions.NONE;
return LockOptions.NONE; case READ -> LockOptions.READ;
case READ: case OPTIMISTIC -> LockOptions.OPTIMISTIC;
return LockOptions.READ; case OPTIMISTIC_FORCE_INCREMENT -> LockOptions.OPTIMISTIC_FORCE_INCREMENT;
case OPTIMISTIC: case UPGRADE_NOWAIT -> LockOptions.UPGRADE_NOWAIT;
return LockOptions.OPTIMISTIC; case UPGRADE_SKIPLOCKED -> LockOptions.UPGRADE_SKIPLOCKED;
case OPTIMISTIC_FORCE_INCREMENT: case PESSIMISTIC_READ -> LockOptions.PESSIMISTIC_READ;
return LockOptions.OPTIMISTIC_FORCE_INCREMENT; case PESSIMISTIC_WRITE -> LockOptions.PESSIMISTIC_WRITE;
case UPGRADE_NOWAIT: case PESSIMISTIC_FORCE_INCREMENT -> LockOptions.PESSIMISTIC_FORCE_INCREMENT;
return LockOptions.UPGRADE_NOWAIT; case WRITE -> throw new UnsupportedOperationException( "WRITE is not a valid LockMode as an argument" );
case UPGRADE_SKIPLOCKED: };
return LockOptions.UPGRADE_SKIPLOCKED;
case PESSIMISTIC_READ:
return LockOptions.PESSIMISTIC_READ;
case PESSIMISTIC_WRITE:
return LockOptions.PESSIMISTIC_WRITE;
case PESSIMISTIC_FORCE_INCREMENT:
return LockOptions.PESSIMISTIC_FORCE_INCREMENT;
case WRITE:
throw new UnsupportedOperationException("WRITE is not a valid LockMode as an argument");
default:
throw new AssertionFailure( "Unrecognized LockMode: " + this );
}
} }
} }

View File

@ -20,7 +20,7 @@ public enum ScrollMode {
* *
* @see ResultSet#TYPE_FORWARD_ONLY * @see ResultSet#TYPE_FORWARD_ONLY
*/ */
FORWARD_ONLY( ResultSet.TYPE_FORWARD_ONLY ), FORWARD_ONLY,
/** /**
* Requests a scrollable result which is sensitive to changes * Requests a scrollable result which is sensitive to changes
@ -28,25 +28,19 @@ public enum ScrollMode {
* *
* @see ResultSet#TYPE_SCROLL_SENSITIVE * @see ResultSet#TYPE_SCROLL_SENSITIVE
*/ */
SCROLL_SENSITIVE( ResultSet.TYPE_SCROLL_SENSITIVE ), SCROLL_SENSITIVE,
/** /**
* Requests a scrollable result which is insensitive to changes * Requests a scrollable result which is insensitive to changes
* in the underlying data. * in the underlying data.
* * <p>
* Note that since the Hibernate session acts as a cache, you * Note that since the Hibernate session acts as a cache, you
* might need to explicitly evict objects, if you need to see * might need to explicitly evict objects, if you need to see
* changes made by other transactions. * changes made by other transactions.
* *
* @see ResultSet#TYPE_SCROLL_INSENSITIVE * @see ResultSet#TYPE_SCROLL_INSENSITIVE
*/ */
SCROLL_INSENSITIVE( ResultSet.TYPE_SCROLL_INSENSITIVE ); SCROLL_INSENSITIVE;
private final int resultSetType;
ScrollMode(int level) {
this.resultSetType = level;
}
/** /**
* Get the corresponding JDBC scroll type code constant value. * Get the corresponding JDBC scroll type code constant value.
@ -54,7 +48,11 @@ public enum ScrollMode {
* @return the JDBC result set type code * @return the JDBC result set type code
*/ */
public int toResultSetType() { public int toResultSetType() {
return resultSetType; return switch (this) {
case FORWARD_ONLY -> ResultSet.TYPE_FORWARD_ONLY;
case SCROLL_SENSITIVE -> ResultSet.TYPE_SCROLL_SENSITIVE;
case SCROLL_INSENSITIVE -> ResultSet.TYPE_SCROLL_INSENSITIVE;
};
} }
/** /**
@ -65,7 +63,6 @@ public enum ScrollMode {
* @return {@code true} if this mode is less than the other. * @return {@code true} if this mode is less than the other.
*/ */
public boolean lessThan(ScrollMode other) { public boolean lessThan(ScrollMode other) {
return this.resultSetType < other.resultSetType; return this.toResultSetType() < other.toResultSetType();
} }
} }

View File

@ -31,11 +31,12 @@ public enum TimeZoneStorageStrategy {
* Does not store the time zone, and instead: * Does not store the time zone, and instead:
* <ul> * <ul>
* <li>when persisting to the database, normalizes JDBC timestamps to the * <li>when persisting to the database, normalizes JDBC timestamps to the
* {@linkplain org.hibernate.cfg.AvailableSettings#JDBC_TIME_ZONE} * {@linkplain org.hibernate.cfg.AvailableSettings#JDBC_TIME_ZONE
* or to the JVM default time zone otherwise. * configured JDBC time zone}, or to the JVM default time zone
* id no JDBC time zone is configured, or
* <li>when reading back from the database, sets the offset or zone * <li>when reading back from the database, sets the offset or zone
* of {@code OffsetDateTime}/{@code ZonedDateTime} properties * of {@code OffsetDateTime}/{@code ZonedDateTime} properties
* to the JVM default time zone. * to the JVM default time zone.
* </ul> * </ul>
* <p> * <p>
* Provided partly for backward compatibility with older * Provided partly for backward compatibility with older

View File

@ -6,7 +6,6 @@
*/ */
package org.hibernate.annotations; package org.hibernate.annotations;
import org.hibernate.AssertionFailure;
import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.AccessType;
/** /**
@ -140,20 +139,13 @@ public enum CacheConcurrencyStrategy {
* return {@code null} for {@link #NONE}. * return {@code null} for {@link #NONE}.
*/ */
public AccessType toAccessType() { public AccessType toAccessType() {
switch ( this ) { return switch (this) {
case NONE: case NONE -> null;
return null; case READ_ONLY -> AccessType.READ_ONLY;
case READ_ONLY: case NONSTRICT_READ_WRITE -> AccessType.NONSTRICT_READ_WRITE;
return AccessType.READ_ONLY; case READ_WRITE -> AccessType.READ_WRITE;
case NONSTRICT_READ_WRITE: case TRANSACTIONAL -> AccessType.TRANSACTIONAL;
return AccessType.NONSTRICT_READ_WRITE; };
case READ_WRITE:
return AccessType.READ_WRITE;
case TRANSACTIONAL:
return AccessType.TRANSACTIONAL;
default:
throw new AssertionFailure( "unknown CacheConcurrencyStrategy" );
}
} }
/** /**
@ -170,18 +162,12 @@ public enum CacheConcurrencyStrategy {
return NONE; return NONE;
} }
else { else {
switch ( accessType ) { return switch (accessType) {
case READ_ONLY: case READ_ONLY -> READ_ONLY;
return READ_ONLY; case READ_WRITE -> READ_WRITE;
case READ_WRITE: case NONSTRICT_READ_WRITE -> NONSTRICT_READ_WRITE;
return READ_WRITE; case TRANSACTIONAL -> TRANSACTIONAL;
case NONSTRICT_READ_WRITE: };
return NONSTRICT_READ_WRITE;
case TRANSACTIONAL:
return TRANSACTIONAL;
default:
return NONE;
}
} }
} }

View File

@ -42,5 +42,5 @@ public enum CacheLayout {
* Stores the full state into the query cache. * Stores the full state into the query cache.
* This is useful when the chances for the object being part of the second level cache are very low. * This is useful when the chances for the object being part of the second level cache are very low.
*/ */
FULL; FULL
} }

View File

@ -66,8 +66,8 @@ public enum OnDeleteAction {
return null; return null;
} }
if ( value instanceof OnDeleteAction ) { if ( value instanceof OnDeleteAction onDeleteAction ) {
return (OnDeleteAction) value; return onDeleteAction;
} }
final String valueString = value.toString(); final String valueString = value.toString();

View File

@ -32,5 +32,5 @@ public enum SourceType {
* For a generated timestamp, the {@code current_timestamp} function * For a generated timestamp, the {@code current_timestamp} function
* might be the source. * might be the source.
*/ */
DB; DB
} }

View File

@ -25,13 +25,9 @@ public enum FetchTiming {
DELAYED; DELAYED;
public static FetchTiming forType(FetchType type) { public static FetchTiming forType(FetchType type) {
switch ( type ) { return switch (type) {
case EAGER: case EAGER -> IMMEDIATE;
return IMMEDIATE; case LAZY -> DELAYED;
case LAZY: };
return DELAYED;
default:
throw new IllegalArgumentException( "Unknown FetchType" );
}
} }
} }

View File

@ -39,19 +39,11 @@ public enum TypeNullability {
* @return The corresponding enum. * @return The corresponding enum.
*/ */
public static TypeNullability interpret(short code) { public static TypeNullability interpret(short code) {
switch ( code ) { return switch (code) {
case DatabaseMetaData.typeNullable: { case DatabaseMetaData.typeNullable -> NULLABLE;
return NULLABLE; case DatabaseMetaData.typeNoNulls -> NON_NULLABLE;
} case DatabaseMetaData.typeNullableUnknown -> UNKNOWN;
case DatabaseMetaData.typeNoNulls: { default -> throw new IllegalArgumentException( "Unknown type nullability code [" + code + "] encountered" );
return NON_NULLABLE; };
}
case DatabaseMetaData.typeNullableUnknown: {
return UNKNOWN;
}
default: {
throw new IllegalArgumentException( "Unknown type nullability code [" + code + "] encountered" );
}
}
} }
} }

View File

@ -44,22 +44,12 @@ public enum TypeSearchability {
* @return The corresponding enum. * @return The corresponding enum.
*/ */
public static TypeSearchability interpret(short code) { public static TypeSearchability interpret(short code) {
switch ( code ) { return switch (code) {
case DatabaseMetaData.typeSearchable: { case DatabaseMetaData.typeSearchable -> FULL;
return FULL; case DatabaseMetaData.typePredNone -> NONE;
} case DatabaseMetaData.typePredBasic -> BASIC;
case DatabaseMetaData.typePredNone: { case DatabaseMetaData.typePredChar -> CHAR;
return NONE; default -> throw new IllegalArgumentException( "Unknown type searchability code [" + code + "] encountered" );
} };
case DatabaseMetaData.typePredBasic: {
return BASIC;
}
case DatabaseMetaData.typePredChar: {
return CHAR;
}
default: {
throw new IllegalArgumentException( "Unknown type searchability code [" + code + "] encountered" );
}
}
} }
} }

View File

@ -25,8 +25,7 @@ public enum CallbackType {
POST_PERSIST( PostPersist.class ), POST_PERSIST( PostPersist.class ),
PRE_REMOVE( PreRemove.class ), PRE_REMOVE( PreRemove.class ),
POST_REMOVE( PostRemove.class ), POST_REMOVE( PostRemove.class ),
POST_LOAD( PostLoad.class ) POST_LOAD( PostLoad.class );
;
private final Class<? extends Annotation> callbackAnnotation; private final Class<? extends Annotation> callbackAnnotation;

View File

@ -42,8 +42,9 @@ public class FlushModeTypeHelper {
else if ( flushMode == FlushMode.AUTO ) { else if ( flushMode == FlushMode.AUTO ) {
return FlushModeType.AUTO; return FlushModeType.AUTO;
} }
else {
throw new AssertionFailure( "unhandled FlushMode " + flushMode ); throw new AssertionFailure( "unhandled FlushMode " + flushMode );
}
} }
public static FlushMode getFlushMode(FlushModeType flushModeType) { public static FlushMode getFlushMode(FlushModeType flushModeType) {
@ -53,26 +54,27 @@ public class FlushModeTypeHelper {
else if ( flushModeType == FlushModeType.COMMIT ) { else if ( flushModeType == FlushModeType.COMMIT ) {
return FlushMode.COMMIT; return FlushMode.COMMIT;
} }
else {
throw new AssertionFailure( "unhandled FlushModeType " + flushModeType ); throw new AssertionFailure( "unhandled FlushModeType " + flushModeType );
}
} }
public static FlushMode interpretFlushMode(Object value) { public static FlushMode interpretFlushMode(Object value) {
if ( value == null ) { if ( value == null ) {
return FlushMode.AUTO; return FlushMode.AUTO;
} }
if (value instanceof FlushMode) { if ( value instanceof FlushMode flushMode ) {
return (FlushMode) value; return flushMode;
} }
else if (value instanceof FlushModeType) { else if ( value instanceof FlushModeType flushModeType ) {
return getFlushMode( (FlushModeType) value ); return getFlushMode( flushModeType );
} }
else if (value instanceof String) { else if ( value instanceof String string ) {
return interpretExternalSetting( (String) value ); return interpretExternalSetting( string );
}
else {
throw new IllegalArgumentException( "Unknown FlushMode source : " + value );
} }
throw new IllegalArgumentException( "Unknown FlushMode source : " + value );
} }
public static FlushMode interpretExternalSetting(String externalName) { public static FlushMode interpretExternalSetting(String externalName) {

View File

@ -9,7 +9,6 @@ package org.hibernate.tuple;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Locale; import java.util.Locale;
import org.hibernate.AssertionFailure;
import org.hibernate.generator.EventType; import org.hibernate.generator.EventType;
/** /**
@ -55,31 +54,21 @@ public enum GenerationTiming {
} }
public boolean includes(GenerationTiming timing) { public boolean includes(GenerationTiming timing) {
switch (this) { return switch (this) {
case NEVER: case NEVER -> timing == NEVER;
return timing == NEVER; case INSERT -> timing.includesInsert();
case INSERT: case UPDATE -> timing.includesUpdate();
return timing.includesInsert(); case ALWAYS -> true;
case UPDATE: };
return timing.includesUpdate();
case ALWAYS:
return true;
default:
throw new AssertionFailure("unknown timing");
}
} }
public static GenerationTiming parseFromName(String name) { public static GenerationTiming parseFromName(String name) {
switch ( name.toLowerCase(Locale.ROOT) ) { return switch (name.toLowerCase(Locale.ROOT)) {
case "insert": case "insert" -> INSERT;
return INSERT; case "update" -> UPDATE;
case "update": case "always" -> ALWAYS;
return UPDATE; default -> NEVER;
case "always": };
return ALWAYS;
default:
return NEVER;
}
} }
/** /**

View File

@ -63,8 +63,8 @@ public enum WrapperArrayHandling {
throw new IllegalArgumentException( "Null value passed to convert" ); throw new IllegalArgumentException( "Null value passed to convert" );
} }
return value instanceof WrapperArrayHandling return value instanceof WrapperArrayHandling wrapperArrayHandling
? (WrapperArrayHandling) value ? wrapperArrayHandling
: valueOf( value.toString().toUpperCase( Locale.ROOT ) ); : valueOf( value.toString().toUpperCase( Locale.ROOT ) );
} }
@ -77,8 +77,8 @@ public enum WrapperArrayHandling {
return null; return null;
} }
return value instanceof WrapperArrayHandling return value instanceof WrapperArrayHandling wrapperArrayHandling
? (WrapperArrayHandling) value ? wrapperArrayHandling
: valueOf( value.toString().toUpperCase( Locale.ROOT ) ); : valueOf( value.toString().toUpperCase( Locale.ROOT ) );
} }
} }