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;
}
switch ( storeMode ) {
case USE: {
switch ( retrieveMode ) {
case USE:
return NORMAL;
case BYPASS:
return PUT;
}
}
case BYPASS: {
switch ( retrieveMode ) {
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 );
}
}
return switch (storeMode) {
case USE -> switch (retrieveMode) {
case USE -> NORMAL;
case BYPASS -> PUT;
};
case BYPASS -> switch (retrieveMode) {
case USE -> GET;
case BYPASS -> IGNORE;
};
// technically should combo CacheStoreMode#REFRESH and CacheRetrieveMode#USE be illegal?
case REFRESH -> REFRESH;
};
}
}

View File

@ -6,7 +6,7 @@
*/
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}
@ -32,11 +32,9 @@ public enum ConnectionAcquisitionMode {
AS_NEEDED;
public static ConnectionAcquisitionMode interpret(String value) {
if ( "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value ) ) {
return IMMEDIATELY;
}
return AS_NEEDED;
return "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value )
? IMMEDIATELY
: AS_NEEDED;
}
public static ConnectionAcquisitionMode interpret(Object setting) {
@ -44,12 +42,12 @@ public enum ConnectionAcquisitionMode {
return null;
}
if ( setting instanceof ConnectionAcquisitionMode ) {
return (ConnectionAcquisitionMode) setting;
if ( setting instanceof ConnectionAcquisitionMode mode ) {
return mode;
}
final String value = setting.toString();
if ( StringHelper.isEmpty( value ) ) {
if ( isEmpty( value ) ) {
return null;
}

View File

@ -8,7 +8,7 @@ package org.hibernate;
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
@ -74,12 +74,12 @@ public enum ConnectionReleaseMode{
return null;
}
if ( setting instanceof ConnectionReleaseMode ) {
return (ConnectionReleaseMode) setting;
if ( setting instanceof ConnectionReleaseMode mode ) {
return mode;
}
final String value = setting.toString();
if ( StringHelper.isEmpty( value ) ) {
if ( isEmpty( value ) ) {
return null;
}

View File

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

View File

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

View File

@ -20,7 +20,7 @@ public enum ScrollMode {
*
* @see ResultSet#TYPE_FORWARD_ONLY
*/
FORWARD_ONLY( ResultSet.TYPE_FORWARD_ONLY ),
FORWARD_ONLY,
/**
* Requests a scrollable result which is sensitive to changes
@ -28,25 +28,19 @@ public enum ScrollMode {
*
* @see ResultSet#TYPE_SCROLL_SENSITIVE
*/
SCROLL_SENSITIVE( ResultSet.TYPE_SCROLL_SENSITIVE ),
SCROLL_SENSITIVE,
/**
* Requests a scrollable result which is insensitive to changes
* in the underlying data.
*
* <p>
* Note that since the Hibernate session acts as a cache, you
* might need to explicitly evict objects, if you need to see
* changes made by other transactions.
*
* @see ResultSet#TYPE_SCROLL_INSENSITIVE
*/
SCROLL_INSENSITIVE( ResultSet.TYPE_SCROLL_INSENSITIVE );
private final int resultSetType;
ScrollMode(int level) {
this.resultSetType = level;
}
SCROLL_INSENSITIVE;
/**
* Get the corresponding JDBC scroll type code constant value.
@ -54,7 +48,11 @@ public enum ScrollMode {
* @return the JDBC result set type code
*/
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.
*/
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:
* <ul>
* <li>when persisting to the database, normalizes JDBC timestamps to the
* {@linkplain org.hibernate.cfg.AvailableSettings#JDBC_TIME_ZONE}
* or to the JVM default time zone otherwise.
* {@linkplain org.hibernate.cfg.AvailableSettings#JDBC_TIME_ZONE
* 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
* of {@code OffsetDateTime}/{@code ZonedDateTime} properties
* to the JVM default time zone.
* of {@code OffsetDateTime}/{@code ZonedDateTime} properties
* to the JVM default time zone.
* </ul>
* <p>
* Provided partly for backward compatibility with older

View File

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

View File

@ -42,5 +42,5 @@ public enum CacheLayout {
* 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.
*/
FULL;
FULL
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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