Enhance switch statements

Issue #4365
This commit is contained in:
Minsoo Kim
2023-05-03 14:05:03 +09:00
committed by Mahmoud Ben Hassine
parent 7460c5f9a1
commit 4a971a0764
5 changed files with 53 additions and 119 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -169,6 +169,7 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
* @author Lucas Ward
* @author Mahmoud Ben Hassine
* @author Minsoo Kim
* @since 1.0
*/
public class CommandLineJobRunner {
@@ -538,15 +539,9 @@ public class CommandLineJobRunner {
}
else {
switch (count) {
case 0:
jobPath = arg;
break;
case 1:
jobIdentifier = arg;
break;
default:
params.add(arg);
break;
case 0 -> jobPath = arg;
case 1 -> jobIdentifier = arg;
default -> params.add(arg);
}
count++;
}

View File

@@ -62,6 +62,7 @@ import org.springframework.util.Assert;
* @author David Turanski
* @author Mahmoud Ben Hassine
* @author Baris Cubukcuoglu
* @author Minsoo Kim
* @see StepExecutionDao
*/
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -183,21 +184,12 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
Integer[] parameterTypes = (Integer[]) parameters.get(1);
for (int indx = 0; indx < parameterValues.length; indx++) {
switch (parameterTypes[indx]) {
case Types.INTEGER:
ps.setInt(indx + 1, (Integer) parameterValues[indx]);
break;
case Types.VARCHAR:
ps.setString(indx + 1, (String) parameterValues[indx]);
break;
case Types.TIMESTAMP:
ps.setTimestamp(indx + 1, (Timestamp) parameterValues[indx]);
break;
case Types.BIGINT:
ps.setLong(indx + 1, (Long) parameterValues[indx]);
break;
default:
throw new IllegalArgumentException(
"unsupported SQL parameter type for step execution field index " + i);
case Types.INTEGER -> ps.setInt(indx + 1, (Integer) parameterValues[indx]);
case Types.VARCHAR -> ps.setString(indx + 1, (String) parameterValues[indx]);
case Types.TIMESTAMP -> ps.setTimestamp(indx + 1, (Timestamp) parameterValues[indx]);
case Types.BIGINT -> ps.setLong(indx + 1, (Long) parameterValues[indx]);
default -> throw new IllegalArgumentException(
"unsupported SQL parameter type for step execution field index " + i);
}
}
}

View File

@@ -51,6 +51,7 @@ import org.springframework.util.Assert;
* @author Glenn Renfro
* @author Drummond Dawson
* @author Mahmoud Ben Hassine
* @author Minsoo Kim
* @since 4.0
* @see JdbcPagingItemReader
*/
@@ -334,53 +335,22 @@ public class JdbcPagingItemReaderBuilder<T> {
try {
DatabaseType databaseType = DatabaseType.fromMetaData(dataSource);
AbstractSqlPagingQueryProvider provider;
switch (databaseType) {
case DERBY:
provider = new DerbyPagingQueryProvider();
break;
case DB2:
case DB2VSE:
case DB2ZOS:
case DB2AS400:
provider = new Db2PagingQueryProvider();
break;
case H2:
provider = new H2PagingQueryProvider();
break;
case HANA:
provider = new HanaPagingQueryProvider();
break;
case HSQL:
provider = new HsqlPagingQueryProvider();
break;
case SQLSERVER:
provider = new SqlServerPagingQueryProvider();
break;
case MYSQL:
provider = new MySqlPagingQueryProvider();
break;
case MARIADB:
provider = new MariaDBPagingQueryProvider();
break;
case ORACLE:
provider = new OraclePagingQueryProvider();
break;
case POSTGRES:
provider = new PostgresPagingQueryProvider();
break;
case SYBASE:
provider = new SybasePagingQueryProvider();
break;
case SQLITE:
provider = new SqlitePagingQueryProvider();
break;
default:
throw new IllegalArgumentException(
"Unable to determine PagingQueryProvider type " + "from database type: " + databaseType);
}
AbstractSqlPagingQueryProvider provider = switch (databaseType) {
case DERBY -> new DerbyPagingQueryProvider();
case DB2, DB2VSE, DB2ZOS, DB2AS400 -> new Db2PagingQueryProvider();
case H2 -> new H2PagingQueryProvider();
case HANA -> new HanaPagingQueryProvider();
case HSQL -> new HsqlPagingQueryProvider();
case SQLSERVER -> new SqlServerPagingQueryProvider();
case MYSQL -> new MySqlPagingQueryProvider();
case MARIADB -> new MariaDBPagingQueryProvider();
case ORACLE -> new OraclePagingQueryProvider();
case POSTGRES -> new PostgresPagingQueryProvider();
case SYBASE -> new SybasePagingQueryProvider();
case SQLITE -> new SqlitePagingQueryProvider();
default -> throw new IllegalArgumentException(
"Unable to determine PagingQueryProvider type " + "from database type: " + databaseType);
};
provider.setSelectClause(this.selectClause);
provider.setFromClause(this.fromClause);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -129,33 +129,22 @@ public class User extends org.apache.avro.specific.SpecificRecordBase
// Used by DatumWriter. Applications should not call.
public Object get(int field$) {
switch (field$) {
case 0:
return name;
case 1:
return favorite_number;
case 2:
return favorite_color;
default:
throw new org.apache.avro.AvroRuntimeException("Bad index");
}
return switch (field$) {
case 0 -> name;
case 1 -> favorite_number;
case 2 -> favorite_color;
default -> throw new org.apache.avro.AvroRuntimeException("Bad index");
};
}
// Used by DatumReader. Applications should not call.
@SuppressWarnings(value = "unchecked")
public void put(int field$, Object value$) {
switch (field$) {
case 0:
name = (CharSequence) value$;
break;
case 1:
favorite_number = (Integer) value$;
break;
case 2:
favorite_color = (CharSequence) value$;
break;
default:
throw new org.apache.avro.AvroRuntimeException("Bad index");
case 0 -> name = (CharSequence) value$;
case 1 -> favorite_number = (Integer) value$;
case 2 -> favorite_color = (CharSequence) value$;
default -> throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
@@ -510,11 +499,8 @@ public class User extends org.apache.avro.specific.SpecificRecordBase
else {
for (int i = 0; i < 3; i++) {
switch (fieldOrder[i].pos()) {
case 0:
this.name = in.readString(this.name instanceof Utf8 ? (Utf8) this.name : null);
break;
case 1:
case 0 -> this.name = in.readString(this.name instanceof Utf8 ? (Utf8) this.name : null);
case 1 -> {
if (in.readIndex() != 0) {
in.readNull();
this.favorite_number = null;
@@ -522,9 +508,8 @@ public class User extends org.apache.avro.specific.SpecificRecordBase
else {
this.favorite_number = in.readInt();
}
break;
case 2:
}
case 2 -> {
if (in.readIndex() != 0) {
in.readNull();
this.favorite_color = null;
@@ -533,10 +518,8 @@ public class User extends org.apache.avro.specific.SpecificRecordBase
this.favorite_color = in.readString(
this.favorite_color instanceof Utf8 ? (Utf8) this.favorite_color : null);
}
break;
default:
throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
default -> throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2022 the original author or authors.
* Copyright 2008-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,18 +39,12 @@ class AggregateItemReaderTests {
@Nullable
@Override
public AggregateItem<String> read() {
switch (count++) {
case 0:
return AggregateItem.getHeader();
case 1:
case 2:
case 3:
return new AggregateItem<>("line");
case 4:
return AggregateItem.getFooter();
default:
return null;
}
return switch (count++) {
case 0 -> AggregateItem.getHeader();
case 1, 2, 3 -> new AggregateItem<>("line");
case 4 -> AggregateItem.getFooter();
default -> null;
};
}
};