From 0ba7aec32d6304774c9adbd53f92c225a87055cd Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 18 Oct 2024 22:24:43 +0200 Subject: [PATCH] modernize code in GraphSemantic and ExecuteUpdateResultCheckStyle --- .../spi/ExecuteUpdateResultCheckStyle.java | 62 +++++++----------- .../org/hibernate/graph/GraphSemantic.java | 63 +++++++------------ 2 files changed, 45 insertions(+), 80 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/ExecuteUpdateResultCheckStyle.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/ExecuteUpdateResultCheckStyle.java index 8eee2159b3..99c16e0cce 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/ExecuteUpdateResultCheckStyle.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/ExecuteUpdateResultCheckStyle.java @@ -52,29 +52,19 @@ public enum ExecuteUpdateResultCheckStyle { PARAM; public String externalName() { - switch (this) { - case NONE: - return "none"; - case COUNT: - return "rowcount"; - case PARAM: - return "param"; - default: - throw new AssertionFailure("Unrecognized ExecuteUpdateResultCheckStyle"); - } + return switch ( this ) { + case NONE -> "none"; + case COUNT -> "rowcount"; + case PARAM -> "param"; + }; } - public static @Nullable ExecuteUpdateResultCheckStyle fromResultCheckStyle(ResultCheckStyle style) { - switch (style) { - case NONE: - return NONE; - case COUNT: - return COUNT; - case PARAM: - return PARAM; - default: - return null; - } + public static ExecuteUpdateResultCheckStyle fromResultCheckStyle(ResultCheckStyle style) { + return switch ( style ) { + case NONE -> NONE; + case COUNT -> COUNT; + case PARAM -> PARAM; + }; } public static @Nullable ExecuteUpdateResultCheckStyle fromExternalName(String name) { @@ -92,28 +82,18 @@ public enum ExecuteUpdateResultCheckStyle { } public Supplier expectationConstructor() { - switch (this) { - case NONE: - return Expectation.None::new; - case COUNT: - return Expectation.RowCount::new; - case PARAM: - return Expectation.OutParameter::new; - default: - throw new AssertionFailure( "Unrecognized ExecuteUpdateResultCheckStyle"); - } + return switch ( this ) { + case NONE -> Expectation.None::new; + case COUNT -> Expectation.RowCount::new; + case PARAM -> Expectation.OutParameter::new; + }; } public Class expectationClass() { - switch (this) { - case NONE: - return Expectation.None.class; - case COUNT: - return Expectation.RowCount.class; - case PARAM: - return Expectation.OutParameter.class; - default: - throw new AssertionFailure( "Unrecognized ExecuteUpdateResultCheckStyle"); - } + return switch ( this ) { + case NONE -> Expectation.None.class; + case COUNT -> Expectation.RowCount.class; + case PARAM -> Expectation.OutParameter.class; + }; } } diff --git a/hibernate-core/src/main/java/org/hibernate/graph/GraphSemantic.java b/hibernate-core/src/main/java/org/hibernate/graph/GraphSemantic.java index da113e2e66..64cee5075b 100644 --- a/hibernate-core/src/main/java/org/hibernate/graph/GraphSemantic.java +++ b/hibernate-core/src/main/java/org/hibernate/graph/GraphSemantic.java @@ -6,8 +6,6 @@ package org.hibernate.graph; import java.util.Locale; -import org.hibernate.AssertionFailure; - import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_FETCH_GRAPH; import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_LOAD_GRAPH; import static org.hibernate.jpa.SpecHints.HINT_SPEC_FETCH_GRAPH; @@ -49,14 +47,10 @@ public enum GraphSemantic { * @see org.hibernate.jpa.SpecHints#HINT_SPEC_LOAD_GRAPH */ public String getJakartaHintName() { - switch ( this ) { - case FETCH: - return HINT_SPEC_FETCH_GRAPH; - case LOAD: - return HINT_SPEC_LOAD_GRAPH; - default: - throw new AssertionFailure( "unknown GraphSemantic" ); - } + return switch ( this ) { + case FETCH -> HINT_SPEC_FETCH_GRAPH; + case LOAD -> HINT_SPEC_LOAD_GRAPH; + }; } /** @@ -69,38 +63,29 @@ public enum GraphSemantic { */ @Deprecated(since = "6.0") public String getJpaHintName() { - switch ( this ) { - case FETCH: - return HINT_JAVAEE_FETCH_GRAPH; - case LOAD: - return HINT_JAVAEE_LOAD_GRAPH; - default: - throw new AssertionFailure( "unknown GraphSemantic" ); - } + return switch ( this ) { + case FETCH -> HINT_JAVAEE_FETCH_GRAPH; + case LOAD -> HINT_JAVAEE_LOAD_GRAPH; + }; } public static GraphSemantic fromHintName(String hintName) { - switch ( hintName ) { - case HINT_SPEC_FETCH_GRAPH: - case HINT_JAVAEE_FETCH_GRAPH: - return FETCH; - case HINT_SPEC_LOAD_GRAPH: - case HINT_JAVAEE_LOAD_GRAPH: - return LOAD; - default: - throw new IllegalArgumentException( - String.format( - Locale.ROOT, - "Unknown EntityGraph hint name - `%s`. " + - "Expecting `%s` or `%s` (or `%s` and `%s`).", - hintName, - HINT_SPEC_FETCH_GRAPH, - HINT_SPEC_LOAD_GRAPH, - HINT_JAVAEE_FETCH_GRAPH, - HINT_JAVAEE_LOAD_GRAPH - ) - ); - } + return switch ( hintName ) { + case HINT_SPEC_FETCH_GRAPH, HINT_JAVAEE_FETCH_GRAPH -> FETCH; + case HINT_SPEC_LOAD_GRAPH, HINT_JAVAEE_LOAD_GRAPH -> LOAD; + default -> throw new IllegalArgumentException( + String.format( + Locale.ROOT, + "Unknown EntityGraph hint name - `%s`. " + + "Expecting `%s` or `%s` (or `%s` and `%s`).", + hintName, + HINT_SPEC_FETCH_GRAPH, + HINT_SPEC_LOAD_GRAPH, + HINT_JAVAEE_FETCH_GRAPH, + HINT_JAVAEE_LOAD_GRAPH + ) + ); + }; } /**