Skip to content

Commit 22aba27

Browse files
committed
use switch expressions in main enums
1 parent eed7ec0 commit 22aba27

18 files changed

+131
-224
lines changed

hibernate-core/src/main/java/org/hibernate/CacheMode.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,17 @@ public static CacheMode fromJpaModes(CacheRetrieveMode retrieveMode, CacheStoreM
162162
retrieveMode = CacheRetrieveMode.BYPASS;
163163
}
164164

165-
switch ( storeMode ) {
166-
case USE: {
167-
switch ( retrieveMode ) {
168-
case USE:
169-
return NORMAL;
170-
case BYPASS:
171-
return PUT;
172-
}
173-
}
174-
case BYPASS: {
175-
switch ( retrieveMode ) {
176-
case USE:
177-
return GET;
178-
case BYPASS:
179-
return IGNORE;
180-
}
181-
}
182-
case REFRESH: {
183-
// technically should combo CacheStoreMode#REFRESH and CacheRetrieveMode#USE be illegal?
184-
return REFRESH;
185-
}
186-
default: {
187-
throw new AssertionFailure( "Unrecognized CacheStoreMode: " + storeMode );
188-
}
189-
}
165+
return switch (storeMode) {
166+
case USE -> switch (retrieveMode) {
167+
case USE -> NORMAL;
168+
case BYPASS -> PUT;
169+
};
170+
case BYPASS -> switch (retrieveMode) {
171+
case USE -> GET;
172+
case BYPASS -> IGNORE;
173+
};
174+
// technically should combo CacheStoreMode#REFRESH and CacheRetrieveMode#USE be illegal?
175+
case REFRESH -> REFRESH;
176+
};
190177
}
191178
}

hibernate-core/src/main/java/org/hibernate/ConnectionAcquisitionMode.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
package org.hibernate;
88

9-
import org.hibernate.internal.util.StringHelper;
9+
import static org.hibernate.internal.util.StringHelper.isEmpty;
1010

1111
/**
1212
* Indicates the manner in which JDBC {@linkplain java.sql.Connection connections}
@@ -32,24 +32,22 @@ public enum ConnectionAcquisitionMode {
3232
AS_NEEDED;
3333

3434
public static ConnectionAcquisitionMode interpret(String value) {
35-
if ( "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value ) ) {
36-
return IMMEDIATELY;
37-
}
38-
39-
return AS_NEEDED;
35+
return "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value )
36+
? IMMEDIATELY
37+
: AS_NEEDED;
4038
}
4139

4240
public static ConnectionAcquisitionMode interpret(Object setting) {
4341
if ( setting == null ) {
4442
return null;
4543
}
4644

47-
if ( setting instanceof ConnectionAcquisitionMode ) {
48-
return (ConnectionAcquisitionMode) setting;
45+
if ( setting instanceof ConnectionAcquisitionMode mode ) {
46+
return mode;
4947
}
5048

5149
final String value = setting.toString();
52-
if ( StringHelper.isEmpty( value ) ) {
50+
if ( isEmpty( value ) ) {
5351
return null;
5452
}
5553

hibernate-core/src/main/java/org/hibernate/ConnectionReleaseMode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.util.Locale;
1010

11-
import org.hibernate.internal.util.StringHelper;
11+
import static org.hibernate.internal.util.StringHelper.isEmpty;
1212

1313
/**
1414
* Enumerates various policies for releasing JDBC {@linkplain java.sql.Connection
@@ -74,12 +74,12 @@ public static ConnectionReleaseMode interpret(Object setting) {
7474
return null;
7575
}
7676

77-
if ( setting instanceof ConnectionReleaseMode ) {
78-
return (ConnectionReleaseMode) setting;
77+
if ( setting instanceof ConnectionReleaseMode mode ) {
78+
return mode;
7979
}
8080

8181
final String value = setting.toString();
82-
if ( StringHelper.isEmpty( value ) ) {
82+
if ( isEmpty( value ) ) {
8383
return null;
8484
}
8585

hibernate-core/src/main/java/org/hibernate/FlushMode.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,12 @@ public static FlushMode interpretExternalSetting(String externalName) {
8383
}
8484

8585
private int level() {
86-
switch (this) {
87-
case ALWAYS:
88-
return 20;
89-
case AUTO:
90-
return 10;
91-
case COMMIT:
92-
return 5;
93-
case MANUAL:
94-
return 0;
95-
default:
96-
throw new AssertionFailure("Impossible FlushMode");
97-
}
86+
return switch (this) {
87+
case ALWAYS -> 20;
88+
case AUTO -> 10;
89+
case COMMIT -> 5;
90+
case MANUAL -> 0;
91+
};
9892
}
9993

10094
public static FlushMode fromJpaFlushMode(FlushModeType flushModeType) {

hibernate-core/src/main/java/org/hibernate/LockMode.java

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -245,27 +245,15 @@ public String toExternalForm() {
245245
* forced version increment will occur.
246246
*/
247247
private int level() {
248-
switch (this) {
249-
case NONE:
250-
return 0;
251-
case READ:
252-
return 1;
253-
case OPTIMISTIC:
254-
return 2;
255-
case OPTIMISTIC_FORCE_INCREMENT:
256-
return 3;
257-
case PESSIMISTIC_READ:
258-
return 4;
259-
case UPGRADE_NOWAIT:
260-
case UPGRADE_SKIPLOCKED:
261-
case PESSIMISTIC_WRITE:
262-
return 5;
263-
case PESSIMISTIC_FORCE_INCREMENT:
264-
case WRITE:
265-
return 6;
266-
default:
267-
throw new AssertionFailure( "Unrecognized LockMode: " + this );
268-
}
248+
return switch (this) {
249+
case NONE -> 0;
250+
case READ -> 1;
251+
case OPTIMISTIC -> 2;
252+
case OPTIMISTIC_FORCE_INCREMENT -> 3;
253+
case PESSIMISTIC_READ -> 4;
254+
case UPGRADE_NOWAIT, UPGRADE_SKIPLOCKED, PESSIMISTIC_WRITE -> 5;
255+
case PESSIMISTIC_FORCE_INCREMENT, WRITE -> 6;
256+
};
269257
}
270258

271259
public static LockMode fromExternalForm(String externalForm) {
@@ -291,29 +279,17 @@ public static LockMode fromExternalForm(String externalForm) {
291279
* all other settings defaulted.
292280
*/
293281
public LockOptions toLockOptions() {
294-
switch (this) {
295-
case NONE:
296-
return LockOptions.NONE;
297-
case READ:
298-
return LockOptions.READ;
299-
case OPTIMISTIC:
300-
return LockOptions.OPTIMISTIC;
301-
case OPTIMISTIC_FORCE_INCREMENT:
302-
return LockOptions.OPTIMISTIC_FORCE_INCREMENT;
303-
case UPGRADE_NOWAIT:
304-
return LockOptions.UPGRADE_NOWAIT;
305-
case UPGRADE_SKIPLOCKED:
306-
return LockOptions.UPGRADE_SKIPLOCKED;
307-
case PESSIMISTIC_READ:
308-
return LockOptions.PESSIMISTIC_READ;
309-
case PESSIMISTIC_WRITE:
310-
return LockOptions.PESSIMISTIC_WRITE;
311-
case PESSIMISTIC_FORCE_INCREMENT:
312-
return LockOptions.PESSIMISTIC_FORCE_INCREMENT;
313-
case WRITE:
314-
throw new UnsupportedOperationException("WRITE is not a valid LockMode as an argument");
315-
default:
316-
throw new AssertionFailure( "Unrecognized LockMode: " + this );
317-
}
282+
return switch (this) {
283+
case NONE -> LockOptions.NONE;
284+
case READ -> LockOptions.READ;
285+
case OPTIMISTIC -> LockOptions.OPTIMISTIC;
286+
case OPTIMISTIC_FORCE_INCREMENT -> LockOptions.OPTIMISTIC_FORCE_INCREMENT;
287+
case UPGRADE_NOWAIT -> LockOptions.UPGRADE_NOWAIT;
288+
case UPGRADE_SKIPLOCKED -> LockOptions.UPGRADE_SKIPLOCKED;
289+
case PESSIMISTIC_READ -> LockOptions.PESSIMISTIC_READ;
290+
case PESSIMISTIC_WRITE -> LockOptions.PESSIMISTIC_WRITE;
291+
case PESSIMISTIC_FORCE_INCREMENT -> LockOptions.PESSIMISTIC_FORCE_INCREMENT;
292+
case WRITE -> throw new UnsupportedOperationException( "WRITE is not a valid LockMode as an argument" );
293+
};
318294
}
319295
}

hibernate-core/src/main/java/org/hibernate/ScrollMode.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,39 @@ public enum ScrollMode {
2020
*
2121
* @see ResultSet#TYPE_FORWARD_ONLY
2222
*/
23-
FORWARD_ONLY( ResultSet.TYPE_FORWARD_ONLY ),
23+
FORWARD_ONLY,
2424

2525
/**
2626
* Requests a scrollable result which is sensitive to changes
2727
* in the underlying data.
2828
*
2929
* @see ResultSet#TYPE_SCROLL_SENSITIVE
3030
*/
31-
SCROLL_SENSITIVE( ResultSet.TYPE_SCROLL_SENSITIVE ),
31+
SCROLL_SENSITIVE,
3232

3333
/**
3434
* Requests a scrollable result which is insensitive to changes
3535
* in the underlying data.
36-
*
36+
* <p>
3737
* Note that since the Hibernate session acts as a cache, you
3838
* might need to explicitly evict objects, if you need to see
3939
* changes made by other transactions.
4040
*
4141
* @see ResultSet#TYPE_SCROLL_INSENSITIVE
4242
*/
43-
SCROLL_INSENSITIVE( ResultSet.TYPE_SCROLL_INSENSITIVE );
44-
45-
private final int resultSetType;
46-
47-
ScrollMode(int level) {
48-
this.resultSetType = level;
49-
}
43+
SCROLL_INSENSITIVE;
5044

5145
/**
5246
* Get the corresponding JDBC scroll type code constant value.
5347
*
5448
* @return the JDBC result set type code
5549
*/
5650
public int toResultSetType() {
57-
return resultSetType;
51+
return switch (this) {
52+
case FORWARD_ONLY -> ResultSet.TYPE_FORWARD_ONLY;
53+
case SCROLL_SENSITIVE -> ResultSet.TYPE_SCROLL_SENSITIVE;
54+
case SCROLL_INSENSITIVE -> ResultSet.TYPE_SCROLL_INSENSITIVE;
55+
};
5856
}
5957

6058
/**
@@ -65,7 +63,6 @@ public int toResultSetType() {
6563
* @return {@code true} if this mode is less than the other.
6664
*/
6765
public boolean lessThan(ScrollMode other) {
68-
return this.resultSetType < other.resultSetType;
66+
return this.toResultSetType() < other.toResultSetType();
6967
}
70-
7168
}

hibernate-core/src/main/java/org/hibernate/TimeZoneStorageStrategy.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public enum TimeZoneStorageStrategy {
3131
* Does not store the time zone, and instead:
3232
* <ul>
3333
* <li>when persisting to the database, normalizes JDBC timestamps to the
34-
* {@linkplain org.hibernate.cfg.AvailableSettings#JDBC_TIME_ZONE}
35-
* or to the JVM default time zone otherwise.
34+
* {@linkplain org.hibernate.cfg.AvailableSettings#JDBC_TIME_ZONE
35+
* configured JDBC time zone}, or to the JVM default time zone
36+
* id no JDBC time zone is configured, or
3637
* <li>when reading back from the database, sets the offset or zone
37-
* of {@code OffsetDateTime}/{@code ZonedDateTime} properties
38-
* to the JVM default time zone.
38+
* of {@code OffsetDateTime}/{@code ZonedDateTime} properties
39+
* to the JVM default time zone.
3940
* </ul>
4041
* <p>
4142
* Provided partly for backward compatibility with older

hibernate-core/src/main/java/org/hibernate/annotations/CacheConcurrencyStrategy.java

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package org.hibernate.annotations;
88

9-
import org.hibernate.AssertionFailure;
109
import org.hibernate.cache.spi.access.AccessType;
1110

1211
/**
@@ -140,20 +139,13 @@ public enum CacheConcurrencyStrategy {
140139
* return {@code null} for {@link #NONE}.
141140
*/
142141
public AccessType toAccessType() {
143-
switch ( this ) {
144-
case NONE:
145-
return null;
146-
case READ_ONLY:
147-
return AccessType.READ_ONLY;
148-
case NONSTRICT_READ_WRITE:
149-
return AccessType.NONSTRICT_READ_WRITE;
150-
case READ_WRITE:
151-
return AccessType.READ_WRITE;
152-
case TRANSACTIONAL:
153-
return AccessType.TRANSACTIONAL;
154-
default:
155-
throw new AssertionFailure( "unknown CacheConcurrencyStrategy" );
156-
}
142+
return switch (this) {
143+
case NONE -> null;
144+
case READ_ONLY -> AccessType.READ_ONLY;
145+
case NONSTRICT_READ_WRITE -> AccessType.NONSTRICT_READ_WRITE;
146+
case READ_WRITE -> AccessType.READ_WRITE;
147+
case TRANSACTIONAL -> AccessType.TRANSACTIONAL;
148+
};
157149
}
158150

159151
/**
@@ -170,18 +162,12 @@ public static CacheConcurrencyStrategy fromAccessType(AccessType accessType) {
170162
return NONE;
171163
}
172164
else {
173-
switch ( accessType ) {
174-
case READ_ONLY:
175-
return READ_ONLY;
176-
case READ_WRITE:
177-
return READ_WRITE;
178-
case NONSTRICT_READ_WRITE:
179-
return NONSTRICT_READ_WRITE;
180-
case TRANSACTIONAL:
181-
return TRANSACTIONAL;
182-
default:
183-
return NONE;
184-
}
165+
return switch (accessType) {
166+
case READ_ONLY -> READ_ONLY;
167+
case READ_WRITE -> READ_WRITE;
168+
case NONSTRICT_READ_WRITE -> NONSTRICT_READ_WRITE;
169+
case TRANSACTIONAL -> TRANSACTIONAL;
170+
};
185171
}
186172
}
187173

hibernate-core/src/main/java/org/hibernate/annotations/CacheLayout.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ public enum CacheLayout {
4242
* Stores the full state into the query cache.
4343
* This is useful when the chances for the object being part of the second level cache are very low.
4444
*/
45-
FULL;
45+
FULL
4646
}

hibernate-core/src/main/java/org/hibernate/annotations/OnDeleteAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public static OnDeleteAction fromExternalForm(Object value) {
6666
return null;
6767
}
6868

69-
if ( value instanceof OnDeleteAction ) {
70-
return (OnDeleteAction) value;
69+
if ( value instanceof OnDeleteAction onDeleteAction ) {
70+
return onDeleteAction;
7171
}
7272

7373
final String valueString = value.toString();

0 commit comments

Comments
 (0)