Skip to content

Commit cb1b68d

Browse files
committed
Replace if statements with switch statements where appropriate
1 parent f078745 commit cb1b68d

File tree

3 files changed

+101
-101
lines changed

3 files changed

+101
-101
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/AbstractFlowParser.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
* @author Dave Syer
4343
* @author Michael Minella
4444
* @author Chris Schaefer
45+
* @author Mahmoud Ben Hassine
4546
*
4647
*/
4748
public abstract class AbstractFlowParser extends AbstractSingleBeanDefinitionParser {
@@ -174,21 +175,22 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
174175
if (node instanceof Element) {
175176
String nodeName = node.getLocalName();
176177
Element child = (Element) node;
177-
if (nodeName.equals(STEP_ELE)) {
178-
stateTransitions.addAll(stepParser.parse(child, parserContext, jobFactoryRef));
179-
stepExists = true;
180-
}
181-
else if (nodeName.equals(DECISION_ELE)) {
182-
stateTransitions.addAll(decisionParser.parse(child, parserContext));
183-
}
184-
else if (nodeName.equals(FLOW_ELE)) {
185-
stateTransitions.addAll(flowParser.parse(child, parserContext));
186-
stepExists = true;
187-
}
188-
else if (nodeName.equals(SPLIT_ELE)) {
189-
stateTransitions.addAll(splitParser.parse(child, new ParserContext(parserContext.getReaderContext(),
190-
parserContext.getDelegate(), builder.getBeanDefinition())));
191-
stepExists = true;
178+
switch (nodeName) {
179+
case STEP_ELE -> {
180+
stateTransitions.addAll(stepParser.parse(child, parserContext, jobFactoryRef));
181+
stepExists = true;
182+
}
183+
case DECISION_ELE -> stateTransitions.addAll(decisionParser.parse(child, parserContext));
184+
case FLOW_ELE -> {
185+
stateTransitions.addAll(flowParser.parse(child, parserContext));
186+
stepExists = true;
187+
}
188+
case SPLIT_ELE -> {
189+
stateTransitions
190+
.addAll(splitParser.parse(child, new ParserContext(parserContext.getReaderContext(),
191+
parserContext.getDelegate(), builder.getBeanDefinition())));
192+
stepExists = true;
193+
}
192194
}
193195

194196
if (Arrays.asList(STEP_ELE, DECISION_ELE, SPLIT_ELE, FLOW_ELE).contains(nodeName)) {
@@ -439,18 +441,12 @@ protected static Collection<BeanDefinition> createTransition(FlowExecutionStatus
439441
*/
440442
protected static FlowExecutionStatus getBatchStatusFromEndTransitionName(String elementName) {
441443
elementName = stripNamespace(elementName);
442-
if (STOP_ELE.equals(elementName)) {
443-
return FlowExecutionStatus.STOPPED;
444-
}
445-
else if (END_ELE.equals(elementName)) {
446-
return FlowExecutionStatus.COMPLETED;
447-
}
448-
else if (FAIL_ELE.equals(elementName)) {
449-
return FlowExecutionStatus.FAILED;
450-
}
451-
else {
452-
return FlowExecutionStatus.UNKNOWN;
453-
}
444+
return switch (elementName) {
445+
case STOP_ELE -> FlowExecutionStatus.STOPPED;
446+
case END_ELE -> FlowExecutionStatus.COMPLETED;
447+
case FAIL_ELE -> FlowExecutionStatus.FAILED;
448+
default -> FlowExecutionStatus.UNKNOWN;
449+
};
454450
}
455451

456452
/**

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/order/internal/OrderItemReader.java

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333

3434
/**
3535
* @author peter.zozom
36+
* @author Mahmoud Ben Hassine
3637
*
3738
*/
3839
public class OrderItemReader implements ItemReader<Order> {
@@ -89,65 +90,67 @@ private void process(FieldSet fieldSet) throws Exception {
8990

9091
String lineId = fieldSet.readString(0);
9192

92-
if (Order.LINE_ID_HEADER.equals(lineId)) {
93-
log.debug("STARTING NEW RECORD");
94-
order = headerMapper.mapFieldSet(fieldSet);
95-
}
96-
else if (Order.LINE_ID_FOOTER.equals(lineId)) {
97-
log.debug("END OF RECORD");
98-
99-
// Do mapping for footer here, because mapper does not allow to pass
100-
// an Order object as input.
101-
// Mapper always creates new object
102-
order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE"));
103-
order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS"));
104-
order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS"));
105-
106-
// mark we are finished with current Order
107-
recordFinished = true;
108-
}
109-
else if (Customer.LINE_ID_BUSINESS_CUST.equals(lineId)) {
110-
log.debug("MAPPING CUSTOMER");
111-
if (order.getCustomer() == null) {
112-
Customer customer = customerMapper.mapFieldSet(fieldSet);
113-
customer.setBusinessCustomer(true);
114-
order.setCustomer(customer);
93+
switch (lineId) {
94+
case Order.LINE_ID_HEADER -> {
95+
log.debug("STARTING NEW RECORD");
96+
order = headerMapper.mapFieldSet(fieldSet);
11597
}
116-
}
117-
else if (Customer.LINE_ID_NON_BUSINESS_CUST.equals(lineId)) {
118-
log.debug("MAPPING CUSTOMER");
119-
if (order.getCustomer() == null) {
120-
Customer customer = customerMapper.mapFieldSet(fieldSet);
121-
customer.setBusinessCustomer(false);
122-
order.setCustomer(customer);
98+
case Order.LINE_ID_FOOTER -> {
99+
log.debug("END OF RECORD");
100+
101+
// Do mapping for footer here, because mapper does not allow to pass
102+
// an Order object as input.
103+
// Mapper always creates new object
104+
order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE"));
105+
order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS"));
106+
order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS"));
107+
108+
// mark we are finished with current Order
109+
recordFinished = true;
123110
}
124-
}
125-
else if (Address.LINE_ID_BILLING_ADDR.equals(lineId)) {
126-
log.debug("MAPPING BILLING ADDRESS");
127-
order.setBillingAddress(addressMapper.mapFieldSet(fieldSet));
128-
}
129-
else if (Address.LINE_ID_SHIPPING_ADDR.equals(lineId)) {
130-
log.debug("MAPPING SHIPPING ADDRESS");
131-
order.setShippingAddress(addressMapper.mapFieldSet(fieldSet));
132-
}
133-
else if (BillingInfo.LINE_ID_BILLING_INFO.equals(lineId)) {
134-
log.debug("MAPPING BILLING INFO");
135-
order.setBilling(billingMapper.mapFieldSet(fieldSet));
136-
}
137-
else if (ShippingInfo.LINE_ID_SHIPPING_INFO.equals(lineId)) {
138-
log.debug("MAPPING SHIPPING INFO");
139-
order.setShipping(shippingMapper.mapFieldSet(fieldSet));
140-
}
141-
else if (LineItem.LINE_ID_ITEM.equals(lineId)) {
142-
log.debug("MAPPING LINE ITEM");
143-
if (order.getLineItems() == null) {
144-
order.setLineItems(new ArrayList<>());
111+
case Customer.LINE_ID_BUSINESS_CUST -> {
112+
log.debug("MAPPING CUSTOMER");
113+
if (order.getCustomer() == null) {
114+
Customer customer = customerMapper.mapFieldSet(fieldSet);
115+
customer.setBusinessCustomer(true);
116+
order.setCustomer(customer);
117+
}
145118
}
146-
order.getLineItems().add(itemMapper.mapFieldSet(fieldSet));
147-
}
148-
else {
149-
if (log.isDebugEnabled()) {
150-
log.debug("Could not map LINE_ID=" + lineId);
119+
case Customer.LINE_ID_NON_BUSINESS_CUST -> {
120+
log.debug("MAPPING CUSTOMER");
121+
if (order.getCustomer() == null) {
122+
Customer customer = customerMapper.mapFieldSet(fieldSet);
123+
customer.setBusinessCustomer(false);
124+
order.setCustomer(customer);
125+
}
126+
}
127+
case Address.LINE_ID_BILLING_ADDR -> {
128+
log.debug("MAPPING BILLING ADDRESS");
129+
order.setBillingAddress(addressMapper.mapFieldSet(fieldSet));
130+
}
131+
case Address.LINE_ID_SHIPPING_ADDR -> {
132+
log.debug("MAPPING SHIPPING ADDRESS");
133+
order.setShippingAddress(addressMapper.mapFieldSet(fieldSet));
134+
}
135+
case BillingInfo.LINE_ID_BILLING_INFO -> {
136+
log.debug("MAPPING BILLING INFO");
137+
order.setBilling(billingMapper.mapFieldSet(fieldSet));
138+
}
139+
case ShippingInfo.LINE_ID_SHIPPING_INFO -> {
140+
log.debug("MAPPING SHIPPING INFO");
141+
order.setShipping(shippingMapper.mapFieldSet(fieldSet));
142+
}
143+
case LineItem.LINE_ID_ITEM -> {
144+
log.debug("MAPPING LINE ITEM");
145+
if (order.getLineItems() == null) {
146+
order.setLineItems(new ArrayList<>());
147+
}
148+
order.getLineItems().add(itemMapper.mapFieldSet(fieldSet));
149+
}
150+
default -> {
151+
if (log.isDebugEnabled()) {
152+
log.debug("Could not map LINE_ID=" + lineId);
153+
}
151154
}
152155
}
153156
}

spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/internal/MultiLineTradeItemReader.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828

2929
/**
3030
* @author Dan Garrette
31+
* @author Mahmoud Ben Hassine
3132
* @since 2.0
3233
*/
3334
public class MultiLineTradeItemReader implements ItemReader<Trade>, ItemStream {
@@ -44,21 +45,21 @@ public Trade read() throws Exception {
4445

4546
for (FieldSet line; (line = this.delegate.read()) != null;) {
4647
String prefix = line.readString(0);
47-
if (prefix.equals("BEGIN")) {
48-
t = new Trade(); // Record must start with 'BEGIN'
49-
}
50-
else if (prefix.equals("INFO")) {
51-
Assert.notNull(t, "No 'BEGIN' was found.");
52-
t.setIsin(line.readString(1));
53-
t.setCustomer(line.readString(2));
54-
}
55-
else if (prefix.equals("AMNT")) {
56-
Assert.notNull(t, "No 'BEGIN' was found.");
57-
t.setQuantity(line.readInt(1));
58-
t.setPrice(line.readBigDecimal(2));
59-
}
60-
else if (prefix.equals("END")) {
61-
return t; // Record must end with 'END'
48+
switch (prefix) {
49+
case "BEGIN" -> t = new Trade(); // Record must start with 'BEGIN'
50+
case "INFO" -> {
51+
Assert.notNull(t, "No 'BEGIN' was found.");
52+
t.setIsin(line.readString(1));
53+
t.setCustomer(line.readString(2));
54+
}
55+
case "AMNT" -> {
56+
Assert.notNull(t, "No 'BEGIN' was found.");
57+
t.setQuantity(line.readInt(1));
58+
t.setPrice(line.readBigDecimal(2));
59+
}
60+
case "END" -> {
61+
return t; // Record must end with 'END'
62+
}
6263
}
6364
}
6465
Assert.isNull(t, "No 'END' was found.");

0 commit comments

Comments
 (0)