Skip to content

Commit e5d7640

Browse files
committed
use a switch expression
1 parent f87c27e commit e5d7640

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/IdentifierGeneratorDefinition.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.hibernate.boot.model.internal.GeneratorStrategies.generatorStrategy;
2828
import static org.hibernate.boot.models.JpaAnnotations.SEQUENCE_GENERATOR;
2929
import static org.hibernate.boot.models.JpaAnnotations.TABLE_GENERATOR;
30+
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
3031
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
3132

3233
/**
@@ -49,7 +50,9 @@ public IdentifierGeneratorDefinition(
4950
final Map<String, String> parameters) {
5051
this.name = name;
5152
this.strategy = strategy;
52-
this.parameters = isEmpty( parameters ) ? emptyMap() : unmodifiableMap( parameters );
53+
this.parameters = isEmpty( parameters )
54+
? emptyMap()
55+
: unmodifiableMap( parameters );
5356
}
5457

5558
public IdentifierGeneratorDefinition(
@@ -98,52 +101,37 @@ public static IdentifierGeneratorDefinition createImplicit(
98101
// If we were unable to locate an actual matching named generator assume
99102
// a sequence/table of the given name, make one based on GenerationType.
100103

101-
if ( generationType == null ) {
102-
generationType = GenerationType.SEQUENCE;
103-
}
104-
105-
switch ( generationType ) {
106-
case SEQUENCE:
107-
return buildSequenceGeneratorDefinition( name );
108-
case TABLE:
109-
return buildTableGeneratorDefinition( name );
110-
case IDENTITY:
111-
throw new AnnotationException(
112-
"@GeneratedValue annotation specified 'strategy=IDENTITY' and 'generator'"
113-
+ " but the generator name is unnecessary"
114-
);
115-
case UUID:
116-
throw new AnnotationException(
117-
"@GeneratedValue annotation specified 'strategy=UUID' and 'generator'"
118-
+ " but the generator name is unnecessary"
119-
);
120-
case AUTO:
121-
return new IdentifierGeneratorDefinition(
122-
name,
123-
generatorStrategy( generationType, generatorName, idType ),
124-
singletonMap( IdentifierGenerator.GENERATOR_NAME, name )
125-
);
126-
default:
127-
throw new AssertionFailure( "unknown generator type: " + generationType );
128-
}
104+
return switch ( generationType == null ? GenerationType.SEQUENCE : generationType ) {
105+
case SEQUENCE -> buildSequenceGeneratorDefinition( name );
106+
case TABLE -> buildTableGeneratorDefinition( name );
107+
case AUTO -> new IdentifierGeneratorDefinition(
108+
name,
109+
generatorStrategy( generationType, generatorName, idType ),
110+
singletonMap( IdentifierGenerator.GENERATOR_NAME, name )
111+
);
112+
case IDENTITY, UUID -> throw new AnnotationException(
113+
"@GeneratedValue annotation specified 'strategy=" + generationType
114+
+ "' and 'generator' but the generator name is unnecessary"
115+
);
116+
};
129117
}
130118

131119
private static IdentifierGeneratorDefinition buildTableGeneratorDefinition(String name) {
132-
final Builder builder = new Builder();
133120
final TableGeneratorJpaAnnotation tableGeneratorUsage = TABLE_GENERATOR.createUsage( null );
134-
if ( StringHelper.isNotEmpty( name ) ) {
121+
if ( isNotEmpty( name ) ) {
135122
tableGeneratorUsage.name( name );
136123
}
124+
final Builder builder = new Builder();
137125
interpretTableGenerator( tableGeneratorUsage, builder );
138126
return builder.build();
139127
}
140128

141129
private static IdentifierGeneratorDefinition buildSequenceGeneratorDefinition(String name) {
142-
final Builder builder = new Builder();
143130
final SequenceGeneratorJpaAnnotation sequenceGeneratorUsage = SEQUENCE_GENERATOR.createUsage( null );
144-
if ( StringHelper.isNotEmpty( name ) ) {
131+
if ( isNotEmpty( name ) ) {
145132
sequenceGeneratorUsage.name( name );
146133
}
134+
final Builder builder = new Builder();
147135
interpretSequenceGenerator( sequenceGeneratorUsage, builder );
148136
return builder.build();
149137
}

0 commit comments

Comments
 (0)