Use pattern matching variables where appropriate

This commit is contained in:
Mahmoud Ben Hassine
2023-06-12 15:47:57 +02:00
parent a2be6ba0c3
commit 0f44e2bad1
23 changed files with 52 additions and 67 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.
@@ -27,6 +27,7 @@ import org.springframework.util.ClassUtils;
*
* @author Lucas Ward
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
@SuppressWarnings("serial")
@@ -122,10 +123,9 @@ public class Entity implements Serializable {
if (other == null) {
return false;
}
if (!(other instanceof Entity)) {
if (!(other instanceof Entity entity)) {
return false;
}
Entity entity = (Entity) other;
if (id == null || entity.getId() == null) {
return false;
}

View File

@@ -90,7 +90,7 @@ public class JobParameter<T> implements Serializable {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof JobParameter)) {
if (!(obj instanceof JobParameter rhs)) {
return false;
}
@@ -98,7 +98,6 @@ public class JobParameter<T> implements Serializable {
return true;
}
JobParameter rhs = (JobParameter) obj;
return type == rhs.type && value.equals(rhs.value);
}

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.
@@ -212,10 +212,9 @@ public class StepContribution implements Serializable {
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof StepContribution)) {
if (!(obj instanceof StepContribution other)) {
return false;
}
StepContribution other = (StepContribution) obj;
return toString().equals(other.toString());
}

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.
@@ -495,10 +495,9 @@ public class StepExecution extends Entity {
public boolean equals(Object obj) {
Object jobExecutionId = getJobExecutionId();
if (jobExecutionId == null || !(obj instanceof StepExecution) || getId() == null) {
if (jobExecutionId == null || !(obj instanceof StepExecution other) || getId() == null) {
return super.equals(obj);
}
StepExecution other = (StepExecution) obj;
return stepName.equals(other.getStepName()) && (jobExecutionId.equals(other.getJobExecutionId()))
&& getId().equals(other.getId());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-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,8 +42,7 @@ class AutomaticJobRegistrarBeanPostProcessor implements BeanFactoryPostProcessor
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof AutomaticJobRegistrar) {
AutomaticJobRegistrar automaticJobRegistrar = (AutomaticJobRegistrar) bean;
if (bean instanceof AutomaticJobRegistrar automaticJobRegistrar) {
automaticJobRegistrar.setJobLoader(new DefaultJobLoader(this.beanFactory.getBean(JobRegistry.class)));
for (ApplicationContextFactory factory : this.beanFactory.getBeansOfType(ApplicationContextFactory.class)
.values()) {

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.Assert;
* {@link Job} to launch.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class JobRegistryBeanPostProcessor
@@ -126,8 +127,7 @@ public class JobRegistryBeanPostProcessor
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Job) {
Job job = (Job) bean;
if (bean instanceof Job job) {
try {
String groupName = this.groupName;
if (beanFactory != null && beanFactory.containsBean(beanName)) {

View File

@@ -172,9 +172,8 @@ public abstract class AbstractFlowParser extends AbstractSingleBeanDefinitionPar
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node instanceof Element) {
if (node instanceof Element child) {
String nodeName = node.getLocalName();
Element child = (Element) node;
switch (nodeName) {
case STEP_ELE -> {
stateTransitions.addAll(stepParser.parse(child, parserContext, jobFactoryRef));

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 Thomas Risberg
* @author Josh Long
* @author Mahmoud Ben Hassine
* @see JobParser
* @since 2.0
*/
@@ -109,8 +110,7 @@ public abstract class AbstractStepParser {
for (int i = 0; i < children.getLength(); i++) {
Node nd = children.item(i);
if (nd instanceof Element) {
Element nestedElement = (Element) nd;
if (nd instanceof Element nestedElement) {
String name = nestedElement.getLocalName();
if (TASKLET_ELE.equals(name)) {

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.
@@ -800,12 +800,10 @@ public class StepParserStepFactoryBean<I, O> implements FactoryBean<Step>, BeanN
SkipListener<I, O> skipListener = (SkipListener<I, O>) listener;
skipListeners.add(skipListener);
}
if (listener instanceof StepExecutionListener) {
StepExecutionListener stepExecutionListener = (StepExecutionListener) listener;
if (listener instanceof StepExecutionListener stepExecutionListener) {
stepExecutionListeners.add(stepExecutionListener);
}
if (listener instanceof ChunkListener) {
ChunkListener chunkListener = (ChunkListener) listener;
if (listener instanceof ChunkListener chunkListener) {
chunkListeners.add(chunkListener);
}
if (listener instanceof ItemReadListener) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 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.
@@ -20,6 +20,7 @@ package org.springframework.batch.core.job.flow;
*
* @author Dan Garrette
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @since 2.0
*/
public class FlowExecutionStatus implements Comparable<FlowExecutionStatus> {
@@ -127,10 +128,9 @@ public class FlowExecutionStatus implements Comparable<FlowExecutionStatus> {
if (object == this) {
return true;
}
if (!(object instanceof FlowExecutionStatus)) {
if (!(object instanceof FlowExecutionStatus other)) {
return false;
}
FlowExecutionStatus other = (FlowExecutionStatus) object;
return name.equals(other.name);
}

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.
@@ -95,8 +95,7 @@ public class FlowJob extends AbstractJob {
private void findSteps(Flow flow, Map<String, Step> map) {
for (State state : flow.getStates()) {
if (state instanceof StepLocator) {
StepLocator locator = (StepLocator) state;
if (state instanceof StepLocator locator) {
for (String name : locator.getStepNames()) {
map.put(name, locator.getStep(name));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-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.batch.support.MethodInvoker;
* which returns ExitStatus.
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
* @since 2.0
* @see MethodInvoker
*/
@@ -86,10 +87,9 @@ public class MethodInvokerMethodInterceptor implements MethodInterceptor {
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MethodInvokerMethodInterceptor)) {
if (!(obj instanceof MethodInvokerMethodInterceptor other)) {
return false;
}
MethodInvokerMethodInterceptor other = (MethodInvokerMethodInterceptor) obj;
return invokerMap.equals(other.invokerMap);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-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.
@@ -34,6 +34,7 @@ import org.springframework.util.StringValueResolver;
* ScopeSupport.
*
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @since 3.0
*/
public abstract class BatchScopeSupport implements Scope, BeanFactoryPostProcessor, Ordered {
@@ -194,8 +195,7 @@ public abstract class BatchScopeSupport implements Scope, BeanFactoryPostProcess
definition = (BeanDefinition) value;
beanName = BeanDefinitionReaderUtils.generateBeanName(definition, registry);
}
else if (value instanceof BeanDefinitionHolder) {
BeanDefinitionHolder holder = (BeanDefinitionHolder) value;
else if (value instanceof BeanDefinitionHolder holder) {
definition = holder.getBeanDefinition();
beanName = holder.getBeanName();
}

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.
@@ -201,13 +201,12 @@ public class JobContext extends SynchronizedAttributeAccessor {
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof JobContext)) {
if (!(other instanceof JobContext context)) {
return false;
}
if (other == this) {
return true;
}
JobContext context = (JobContext) other;
if (context.jobExecution == jobExecution) {
return true;
}

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.
@@ -243,13 +243,12 @@ public class StepContext extends SynchronizedAttributeAccessor {
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof StepContext)) {
if (!(other instanceof StepContext context)) {
return false;
}
if (other == this) {
return true;
}
StepContext context = (StepContext) other;
if (context.stepExecution == stepExecution) {
return true;
}

View File

@@ -216,10 +216,9 @@ public class Chunk<W> implements Iterable<W>, Serializable {
if (obj == this) {
return true;
}
if (!(obj instanceof Chunk)) {
if (!(obj instanceof Chunk<?> other)) {
return false;
}
Chunk<?> other = (Chunk<?>) obj;
return Objects.equals(this.items, other.items) && Objects.equals(this.skips, other.skips)
&& Objects.equals(this.errors, other.errors) && Objects.equals(this.userData, other.userData)
&& this.end == other.end && this.busy == other.busy;

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.
@@ -38,6 +38,7 @@ import org.springframework.util.StringUtils;
*
* @author Rob Harrop
* @author Dave Syer
* @author Mahmoud Ben Hassine
*/
public class DefaultFieldSet implements FieldSet {
@@ -669,8 +670,7 @@ public class DefaultFieldSet implements FieldSet {
*/
@Override
public boolean equals(Object object) {
if (object instanceof DefaultFieldSet) {
DefaultFieldSet fs = (DefaultFieldSet) object;
if (object instanceof DefaultFieldSet fs) {
if (this.tokens == null) {
return fs.tokens == null;

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.
@@ -53,8 +53,7 @@ public class JmsItemReader<T> implements ItemReader<T>, InitializingBean {
*/
public void setJmsTemplate(JmsOperations jmsTemplate) {
this.jmsTemplate = jmsTemplate;
if (jmsTemplate instanceof JmsTemplate) {
JmsTemplate template = (JmsTemplate) jmsTemplate;
if (jmsTemplate instanceof JmsTemplate template) {
Assert.isTrue(template.getReceiveTimeout() != JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT,
"JmsTemplate must have a receive timeout!");
Assert.isTrue(template.getDefaultDestination() != null || template.getDefaultDestinationName() != null,

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.
@@ -49,8 +49,7 @@ public class JmsItemWriter<T> implements ItemWriter<T> {
*/
public void setJmsTemplate(JmsOperations jmsTemplate) {
this.jmsTemplate = jmsTemplate;
if (jmsTemplate instanceof JmsTemplate) {
JmsTemplate template = (JmsTemplate) jmsTemplate;
if (jmsTemplate instanceof JmsTemplate template) {
Assert.isTrue(template.getDefaultDestination() != null || template.getDefaultDestinationName() != null,
"JmsTemplate must have a defaultDestination or defaultDestinationName!");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 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.util.ClassUtils;
* in a standalone application with no threads).
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar {
@@ -67,8 +68,7 @@ public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar {
if (key instanceof Class<?>) {
requiredType = (Class<?>) key;
}
else if (key instanceof String) {
String className = (String) key;
else if (key instanceof String className) {
requiredType = ClassUtils.resolveClassName(className, getClass().getClassLoader());
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-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.
@@ -46,6 +46,7 @@ import org.springframework.util.ClassUtils;
* then an exception is thrown.
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
* @since 2.0
*/
public class SimpleMethodInvoker implements MethodInvoker {
@@ -133,14 +134,13 @@ public class SimpleMethodInvoker implements MethodInvoker {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof SimpleMethodInvoker)) {
if (!(obj instanceof SimpleMethodInvoker rhs)) {
return false;
}
if (obj == this) {
return true;
}
SimpleMethodInvoker rhs = (SimpleMethodInvoker) obj;
return (rhs.method.equals(this.method)) && (rhs.object.equals(this.object));
}

View File

@@ -204,10 +204,9 @@ public class JdbcBatchItemWriterNamedParameterTests {
@Override
public boolean matches(Object actual) {
if (!(actual instanceof SqlParameterSource[])) {
if (!(actual instanceof SqlParameterSource[] actualArray)) {
return false;
}
SqlParameterSource[] actualArray = (SqlParameterSource[]) actual;
if (expected.length != actualArray.length) {
return false;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010-2012 the original author or authors.
* Copyright 2010-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,8 +39,7 @@ public class DerbyShutdownBean implements DisposableBean {
@Override
public void destroy() throws Exception {
logger.info("Attempting Derby database shut down on: " + dataSource);
if (!isShutdown && dataSource != null && dataSource instanceof EmbeddedDataSource) {
EmbeddedDataSource ds = (EmbeddedDataSource) dataSource;
if (!isShutdown && dataSource != null && dataSource instanceof EmbeddedDataSource ds) {
try {
ds.setShutdownDatabase("shutdown");
ds.getConnection();