Skip to content

Commit 2e4c2ff

Browse files
committed
clean up two more enums
1 parent d0d6f08 commit 2e4c2ff

File tree

4 files changed

+67
-103
lines changed

4 files changed

+67
-103
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/QuerySettings.java

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

99
import org.hibernate.boot.spi.SessionFactoryOptions;
10-
import org.hibernate.query.NullPrecedence;
1110
import org.hibernate.query.spi.QueryPlan;
1211

1312
import jakarta.persistence.criteria.CriteriaDelete;
@@ -98,13 +97,14 @@ public interface QuerySettings {
9897
String CRITERIA_VALUE_HANDLING_MODE = "hibernate.criteria.value_handling_mode";
9998

10099
/**
101-
* Specifies the default {@linkplain NullPrecedence precedence of null values} in the
102-
* HQL {@code ORDER BY} clause, either {@code none}, {@code first}, or {@code last},
103-
* or an instance of {@link NullPrecedence}.
100+
* Specifies the default {@linkplain jakarta.persistence.criteria.Nulls precedence
101+
* of null values} sorted via the HQL {@code ORDER BY} clause, either {@code none},
102+
* {@code first}, or {@code last}, or an instance of the enumeration
103+
* {@link jakarta.persistence.criteria.Nulls}.
104104
* <p>
105105
* The default is {@code none}.
106106
*
107-
* @see NullPrecedence
107+
* @see jakarta.persistence.criteria.Nulls
108108
* @see org.hibernate.boot.SessionFactoryBuilder#applyDefaultNullPrecedence(jakarta.persistence.criteria.Nulls)
109109
*/
110110
String DEFAULT_NULL_ORDERING = "hibernate.order_by.default_null_ordering";
@@ -245,7 +245,7 @@ public interface QuerySettings {
245245

246246
/**
247247
* For database supporting name parameters this setting allows to use named parameter is the procedure call.
248-
*
248+
* <p>
249249
* By default, this is set to false
250250
*/
251251
String QUERY_PASS_PROCEDURE_PARAMETER_NAMES = "hibernate.query.pass_procedure_paramater_names";

hibernate-core/src/main/java/org/hibernate/query/ImmutableEntityUpdateQueryHandlingMode.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
package org.hibernate.query;
88

99
import org.hibernate.HibernateException;
10+
import org.hibernate.cfg.AvailableSettings;
1011

1112
/**
12-
* This enum defines how {@link org.hibernate.annotations.Immutable} entities are handled when
13-
* executing a bulk update statement.
13+
* Controls for how {@linkplain org.hibernate.annotations.Immutable immutable} entities
14+
* are handled when executing a bulk update statement.
1415
* <ul>
15-
* <li>By default, the {@link ImmutableEntityUpdateQueryHandlingMode#WARNING} mode is used,
16-
* and a warning log message is issued when an {@link org.hibernate.annotations.Immutable}
17-
* entity is to be updated via a bulk update statement.
18-
* <li>If the {@link ImmutableEntityUpdateQueryHandlingMode#EXCEPTION} mode is used, then a
19-
* {@link HibernateException} is thrown instead.
16+
* <li>By default, the {@link #WARNING} mode is used, and a warning log message is issued
17+
* when an immutable entity is updated via a bulk update statement.
18+
* <li>If the {@link #EXCEPTION} mode is configured, then a {@link HibernateException} is
19+
* thrown instead.
2020
* </ul>
2121
*
2222
* @see org.hibernate.cfg.AvailableSettings#IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE
@@ -29,31 +29,32 @@ public enum ImmutableEntityUpdateQueryHandlingMode {
2929
EXCEPTION;
3030

3131
/**
32-
* Interpret the configured {@link ImmutableEntityUpdateQueryHandlingMode} value.
33-
* Valid values are either a {@link ImmutableEntityUpdateQueryHandlingMode} object or
34-
* its string representation. For string values, the matching is case-insensitive,
35-
* so you can use either {@code warning} or {@code exception}.
32+
* Interpret the setting specified via
33+
* {@value AvailableSettings#IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE}.
34+
* <p>
35+
* Valid values are an instance of {@link ImmutableEntityUpdateQueryHandlingMode}
36+
* or its string representation. For string values, the matching is case-insensitive,
37+
* so {@code warning} or {@code exception} are legal values.
3638
*
37-
* @param mode configured {@link ImmutableEntityUpdateQueryHandlingMode} representation
38-
* @return associated {@link ImmutableEntityUpdateQueryHandlingMode} object
39+
* @param setting the configuration setting.
40+
* @return the associated {@link ImmutableEntityUpdateQueryHandlingMode} object
3941
*/
40-
public static ImmutableEntityUpdateQueryHandlingMode interpret(Object mode) {
41-
if ( mode == null ) {
42+
public static ImmutableEntityUpdateQueryHandlingMode interpret(Object setting) {
43+
if ( setting == null ) {
4244
return WARNING;
4345
}
44-
else if ( mode instanceof ImmutableEntityUpdateQueryHandlingMode ) {
45-
return (ImmutableEntityUpdateQueryHandlingMode) mode;
46+
else if ( setting instanceof ImmutableEntityUpdateQueryHandlingMode mode ) {
47+
return mode;
4648
}
47-
else if ( mode instanceof String ) {
49+
else if ( setting instanceof String string ) {
4850
for ( ImmutableEntityUpdateQueryHandlingMode value : values() ) {
49-
if ( value.name().equalsIgnoreCase( (String) mode ) ) {
51+
if ( value.name().equalsIgnoreCase( string ) ) {
5052
return value;
5153
}
5254
}
5355
}
54-
throw new HibernateException(
55-
"Unrecognized immutable_entity_update_query_handling_mode value : " + mode
56-
+ ". Supported values include 'warning' and 'exception'."
57-
);
56+
throw new HibernateException( "Unrecognized value '" + setting
57+
+ "' specified via '" + AvailableSettings.IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE
58+
+ "' (should be 'warning' or 'exception')" );
5859
}
5960
}

hibernate-core/src/main/java/org/hibernate/query/NullPrecedence.java

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

9-
import org.hibernate.AssertionFailure;
109
import org.hibernate.dialect.NullOrdering;
1110

1211
import jakarta.persistence.criteria.Nulls;
@@ -24,21 +23,15 @@ public enum NullPrecedence {
2423
/**
2524
* Null precedence not specified. Relies on the RDBMS implementation.
2625
*/
27-
NONE( Nulls.NONE ),
26+
NONE,
2827
/**
2928
* Null values appear at the beginning of the sorted collection.
3029
*/
31-
FIRST( Nulls.FIRST ),
30+
FIRST,
3231
/**
3332
* Null values appear at the end of the sorted collection.
3433
*/
35-
LAST( Nulls.LAST );
36-
37-
private final Nulls jpaValue;
38-
39-
NullPrecedence(Nulls jpaValue) {
40-
this.jpaValue = jpaValue;
41-
}
34+
LAST;
4235

4336
/**
4437
* Is this null precedence the default for the given sort order and null ordering.
@@ -47,38 +40,21 @@ public enum NullPrecedence {
4740
*/
4841
@Deprecated(since = "7.0", forRemoval = true)
4942
public boolean isDefaultOrdering(SortDirection sortOrder, NullOrdering nullOrdering) {
50-
switch (this) {
51-
case NONE:
52-
return true;
53-
case FIRST:
54-
switch ( nullOrdering ) {
55-
case FIRST:
56-
return true;
57-
case LAST:
58-
return false;
59-
case SMALLEST:
60-
return sortOrder == SortDirection.ASCENDING;
61-
case GREATEST:
62-
return sortOrder == SortDirection.DESCENDING;
63-
default:
64-
throw new AssertionFailure("Unrecognized NullOrdering");
65-
}
66-
case LAST:
67-
switch ( nullOrdering ) {
68-
case LAST:
69-
return true;
70-
case FIRST:
71-
return false;
72-
case SMALLEST:
73-
return sortOrder == SortDirection.DESCENDING;
74-
case GREATEST:
75-
return sortOrder == SortDirection.ASCENDING;
76-
default:
77-
throw new AssertionFailure("Unrecognized NullOrdering");
78-
}
79-
default:
80-
throw new AssertionFailure("Unrecognized NullPrecedence");
81-
}
43+
return switch (this) {
44+
case NONE -> true;
45+
case FIRST -> switch (nullOrdering) {
46+
case FIRST -> true;
47+
case LAST -> false;
48+
case SMALLEST -> sortOrder == SortDirection.ASCENDING;
49+
case GREATEST -> sortOrder == SortDirection.DESCENDING;
50+
};
51+
case LAST -> switch (nullOrdering) {
52+
case LAST -> true;
53+
case FIRST -> false;
54+
case SMALLEST -> sortOrder == SortDirection.DESCENDING;
55+
case GREATEST -> sortOrder == SortDirection.ASCENDING;
56+
};
57+
};
8258
}
8359

8460
/**
@@ -112,22 +88,19 @@ public static NullPrecedence parse(String name, NullPrecedence defaultValue) {
11288
}
11389

11490
public Nulls getJpaValue() {
115-
return jpaValue;
91+
return switch (this) {
92+
case NONE -> Nulls.NONE;
93+
case FIRST -> Nulls.FIRST;
94+
case LAST -> Nulls.LAST;
95+
};
11696
}
11797

11898
public static NullPrecedence fromJpaValue(Nulls jpaValue) {
119-
switch ( jpaValue ) {
120-
case NONE: {
121-
return NullPrecedence.NONE;
122-
}
123-
case FIRST: {
124-
return NullPrecedence.FIRST;
125-
}
126-
case LAST: {
127-
return NullPrecedence.LAST;
128-
}
129-
}
99+
return switch (jpaValue) {
100+
case NONE -> NullPrecedence.NONE;
101+
case FIRST -> NullPrecedence.FIRST;
102+
case LAST -> NullPrecedence.LAST;
103+
};
130104

131-
throw new IllegalArgumentException( "Unexpected JPA Nulls - " + jpaValue );
132105
}
133106
}

hibernate-core/src/main/java/org/hibernate/query/SortDirection.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,20 @@ public enum SortDirection {
2020
DESCENDING;
2121

2222
public SortDirection reverse() {
23-
switch (this) {
24-
case ASCENDING:
25-
return DESCENDING;
26-
case DESCENDING:
27-
return ASCENDING;
28-
default:
29-
return this;
30-
}
23+
return switch (this) {
24+
case ASCENDING -> DESCENDING;
25+
case DESCENDING -> ASCENDING;
26+
};
3127
}
3228

3329
public static SortDirection interpret(String value) {
3430
if ( value == null ) {
3531
return null;
3632
}
37-
38-
switch ( value.toLowerCase(Locale.ROOT) ) {
39-
case "asc":
40-
case "ascending":
41-
return ASCENDING;
42-
case "desc":
43-
case "descending":
44-
return DESCENDING;
45-
default:
46-
throw new IllegalArgumentException( "Unknown sort order: " + value );
47-
}
33+
else return switch ( value.toLowerCase(Locale.ROOT) ) {
34+
case "asc", "ascending" -> ASCENDING;
35+
case "desc", "descending" -> DESCENDING;
36+
default -> throw new IllegalArgumentException( "Unknown sort order: " + value );
37+
};
4838
}
4939
}

0 commit comments

Comments
 (0)