Replace if statements with switch statements where appropriate

This commit is contained in:
Mahmoud Ben Hassine
2023-06-12 15:39:09 +02:00
parent f0787454f8
commit cb1b68d165
3 changed files with 99 additions and 99 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.
@@ -42,6 +42,7 @@ import org.springframework.util.xml.DomUtils;
* @author Dave Syer
* @author Michael Minella
* @author Chris Schaefer
* @author Mahmoud Ben Hassine
*
*/
public abstract class AbstractFlowParser extends AbstractSingleBeanDefinitionParser {
@@ -174,21 +175,22 @@ public abstract class AbstractFlowParser extends AbstractSingleBeanDefinitionPar
if (node instanceof Element) {
String nodeName = node.getLocalName();
Element child = (Element) node;
if (nodeName.equals(STEP_ELE)) {
stateTransitions.addAll(stepParser.parse(child, parserContext, jobFactoryRef));
stepExists = true;
}
else if (nodeName.equals(DECISION_ELE)) {
stateTransitions.addAll(decisionParser.parse(child, parserContext));
}
else if (nodeName.equals(FLOW_ELE)) {
stateTransitions.addAll(flowParser.parse(child, parserContext));
stepExists = true;
}
else if (nodeName.equals(SPLIT_ELE)) {
stateTransitions.addAll(splitParser.parse(child, new ParserContext(parserContext.getReaderContext(),
parserContext.getDelegate(), builder.getBeanDefinition())));
stepExists = true;
switch (nodeName) {
case STEP_ELE -> {
stateTransitions.addAll(stepParser.parse(child, parserContext, jobFactoryRef));
stepExists = true;
}
case DECISION_ELE -> stateTransitions.addAll(decisionParser.parse(child, parserContext));
case FLOW_ELE -> {
stateTransitions.addAll(flowParser.parse(child, parserContext));
stepExists = true;
}
case SPLIT_ELE -> {
stateTransitions
.addAll(splitParser.parse(child, new ParserContext(parserContext.getReaderContext(),
parserContext.getDelegate(), builder.getBeanDefinition())));
stepExists = true;
}
}
if (Arrays.asList(STEP_ELE, DECISION_ELE, SPLIT_ELE, FLOW_ELE).contains(nodeName)) {
@@ -439,18 +441,12 @@ public abstract class AbstractFlowParser extends AbstractSingleBeanDefinitionPar
*/
protected static FlowExecutionStatus getBatchStatusFromEndTransitionName(String elementName) {
elementName = stripNamespace(elementName);
if (STOP_ELE.equals(elementName)) {
return FlowExecutionStatus.STOPPED;
}
else if (END_ELE.equals(elementName)) {
return FlowExecutionStatus.COMPLETED;
}
else if (FAIL_ELE.equals(elementName)) {
return FlowExecutionStatus.FAILED;
}
else {
return FlowExecutionStatus.UNKNOWN;
}
return switch (elementName) {
case STOP_ELE -> FlowExecutionStatus.STOPPED;
case END_ELE -> FlowExecutionStatus.COMPLETED;
case FAIL_ELE -> FlowExecutionStatus.FAILED;
default -> FlowExecutionStatus.UNKNOWN;
};
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 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.
@@ -33,6 +33,7 @@ import org.springframework.lang.Nullable;
/**
* @author peter.zozom
* @author Mahmoud Ben Hassine
*
*/
public class OrderItemReader implements ItemReader<Order> {
@@ -89,65 +90,67 @@ public class OrderItemReader implements ItemReader<Order> {
String lineId = fieldSet.readString(0);
if (Order.LINE_ID_HEADER.equals(lineId)) {
log.debug("STARTING NEW RECORD");
order = headerMapper.mapFieldSet(fieldSet);
}
else if (Order.LINE_ID_FOOTER.equals(lineId)) {
log.debug("END OF RECORD");
switch (lineId) {
case Order.LINE_ID_HEADER -> {
log.debug("STARTING NEW RECORD");
order = headerMapper.mapFieldSet(fieldSet);
}
case Order.LINE_ID_FOOTER -> {
log.debug("END OF RECORD");
// Do mapping for footer here, because mapper does not allow to pass
// an Order object as input.
// Mapper always creates new object
order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE"));
order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS"));
order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS"));
// Do mapping for footer here, because mapper does not allow to pass
// an Order object as input.
// Mapper always creates new object
order.setTotalPrice(fieldSet.readBigDecimal("TOTAL_PRICE"));
order.setTotalLines(fieldSet.readInt("TOTAL_LINE_ITEMS"));
order.setTotalItems(fieldSet.readInt("TOTAL_ITEMS"));
// mark we are finished with current Order
recordFinished = true;
}
else if (Customer.LINE_ID_BUSINESS_CUST.equals(lineId)) {
log.debug("MAPPING CUSTOMER");
if (order.getCustomer() == null) {
Customer customer = customerMapper.mapFieldSet(fieldSet);
customer.setBusinessCustomer(true);
order.setCustomer(customer);
// mark we are finished with current Order
recordFinished = true;
}
}
else if (Customer.LINE_ID_NON_BUSINESS_CUST.equals(lineId)) {
log.debug("MAPPING CUSTOMER");
if (order.getCustomer() == null) {
Customer customer = customerMapper.mapFieldSet(fieldSet);
customer.setBusinessCustomer(false);
order.setCustomer(customer);
case Customer.LINE_ID_BUSINESS_CUST -> {
log.debug("MAPPING CUSTOMER");
if (order.getCustomer() == null) {
Customer customer = customerMapper.mapFieldSet(fieldSet);
customer.setBusinessCustomer(true);
order.setCustomer(customer);
}
}
}
else if (Address.LINE_ID_BILLING_ADDR.equals(lineId)) {
log.debug("MAPPING BILLING ADDRESS");
order.setBillingAddress(addressMapper.mapFieldSet(fieldSet));
}
else if (Address.LINE_ID_SHIPPING_ADDR.equals(lineId)) {
log.debug("MAPPING SHIPPING ADDRESS");
order.setShippingAddress(addressMapper.mapFieldSet(fieldSet));
}
else if (BillingInfo.LINE_ID_BILLING_INFO.equals(lineId)) {
log.debug("MAPPING BILLING INFO");
order.setBilling(billingMapper.mapFieldSet(fieldSet));
}
else if (ShippingInfo.LINE_ID_SHIPPING_INFO.equals(lineId)) {
log.debug("MAPPING SHIPPING INFO");
order.setShipping(shippingMapper.mapFieldSet(fieldSet));
}
else if (LineItem.LINE_ID_ITEM.equals(lineId)) {
log.debug("MAPPING LINE ITEM");
if (order.getLineItems() == null) {
order.setLineItems(new ArrayList<>());
case Customer.LINE_ID_NON_BUSINESS_CUST -> {
log.debug("MAPPING CUSTOMER");
if (order.getCustomer() == null) {
Customer customer = customerMapper.mapFieldSet(fieldSet);
customer.setBusinessCustomer(false);
order.setCustomer(customer);
}
}
order.getLineItems().add(itemMapper.mapFieldSet(fieldSet));
}
else {
if (log.isDebugEnabled()) {
log.debug("Could not map LINE_ID=" + lineId);
case Address.LINE_ID_BILLING_ADDR -> {
log.debug("MAPPING BILLING ADDRESS");
order.setBillingAddress(addressMapper.mapFieldSet(fieldSet));
}
case Address.LINE_ID_SHIPPING_ADDR -> {
log.debug("MAPPING SHIPPING ADDRESS");
order.setShippingAddress(addressMapper.mapFieldSet(fieldSet));
}
case BillingInfo.LINE_ID_BILLING_INFO -> {
log.debug("MAPPING BILLING INFO");
order.setBilling(billingMapper.mapFieldSet(fieldSet));
}
case ShippingInfo.LINE_ID_SHIPPING_INFO -> {
log.debug("MAPPING SHIPPING INFO");
order.setShipping(shippingMapper.mapFieldSet(fieldSet));
}
case LineItem.LINE_ID_ITEM -> {
log.debug("MAPPING LINE ITEM");
if (order.getLineItems() == null) {
order.setLineItems(new ArrayList<>());
}
order.getLineItems().add(itemMapper.mapFieldSet(fieldSet));
}
default -> {
if (log.isDebugEnabled()) {
log.debug("Could not map LINE_ID=" + lineId);
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 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.
@@ -28,6 +28,7 @@ import org.springframework.util.Assert;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
public class MultiLineTradeItemReader implements ItemReader<Trade>, ItemStream {
@@ -44,21 +45,21 @@ public class MultiLineTradeItemReader implements ItemReader<Trade>, ItemStream {
for (FieldSet line; (line = this.delegate.read()) != null;) {
String prefix = line.readString(0);
if (prefix.equals("BEGIN")) {
t = new Trade(); // Record must start with 'BEGIN'
}
else if (prefix.equals("INFO")) {
Assert.notNull(t, "No 'BEGIN' was found.");
t.setIsin(line.readString(1));
t.setCustomer(line.readString(2));
}
else if (prefix.equals("AMNT")) {
Assert.notNull(t, "No 'BEGIN' was found.");
t.setQuantity(line.readInt(1));
t.setPrice(line.readBigDecimal(2));
}
else if (prefix.equals("END")) {
return t; // Record must end with 'END'
switch (prefix) {
case "BEGIN" -> t = new Trade(); // Record must start with 'BEGIN'
case "INFO" -> {
Assert.notNull(t, "No 'BEGIN' was found.");
t.setIsin(line.readString(1));
t.setCustomer(line.readString(2));
}
case "AMNT" -> {
Assert.notNull(t, "No 'BEGIN' was found.");
t.setQuantity(line.readInt(1));
t.setPrice(line.readBigDecimal(2));
}
case "END" -> {
return t; // Record must end with 'END'
}
}
}
Assert.isNull(t, "No 'END' was found.");