Modernize code for diamond, isEmpty & pattern matching

This commit is contained in:
Tran Ngoc Nhan
2024-09-24 01:42:38 +07:00
committed by GitHub
parent 40faaa0c15
commit fc377126de
107 changed files with 550 additions and 454 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -45,6 +45,7 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
* @author Ngoc Nhan
*
* @since 2.1
*/
@@ -94,8 +95,8 @@ public abstract class AbstractAmqpChannel extends AbstractMessageChannel impleme
AbstractAmqpChannel(AmqpTemplate amqpTemplate, AmqpHeaderMapper outboundMapper, AmqpHeaderMapper inboundMapper) {
Assert.notNull(amqpTemplate, "amqpTemplate must not be null");
this.amqpTemplate = amqpTemplate;
if (amqpTemplate instanceof RabbitTemplate) {
this.rabbitTemplate = (RabbitTemplate) amqpTemplate;
if (amqpTemplate instanceof RabbitTemplate castRabbitTemplate) {
this.rabbitTemplate = castRabbitTemplate;
MessageConverter converter = this.rabbitTemplate.getMessageConverter();
if (converter instanceof AllowedListDeserializingMessageConverter allowedListMessageConverter) {
allowedListMessageConverter.addAllowedListPatterns(

View File

@@ -47,6 +47,7 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
* @author Ngoc Nhan
*
* @since 2.1
*/
@@ -167,7 +168,7 @@ public class PollableAmqpChannel extends AbstractAmqpChannel
if (traceEnabled) {
logger.trace("preReceive on channel '" + this + "'");
}
if (interceptorList.getInterceptors().size() > 0) {
if (!interceptorList.getInterceptors().isEmpty()) {
interceptorStack = new ArrayDeque<>();
if (!interceptorList.preReceive(this, interceptorStack)) {
return null;

View File

@@ -64,6 +64,7 @@ import org.springframework.util.ErrorHandler;
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
* @author Ngoc Nhan
*
* @since 2.1
*/
@@ -197,8 +198,8 @@ public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChan
*/
public void setEncoding(String encoding) {
if (this.amqpTemplate instanceof RabbitTemplate) {
((RabbitTemplate) this.amqpTemplate).setEncoding(encoding);
if (this.amqpTemplate instanceof RabbitTemplate rabbitTemplate) {
rabbitTemplate.setEncoding(encoding);
}
else if (logger.isInfoEnabled()) {
logger.info("AmqpTemplate is not a RabbitTemplate, so configured 'encoding' value will be ignored.");
@@ -206,8 +207,8 @@ public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChan
}
public void setMessageConverter(MessageConverter messageConverter) {
if (this.amqpTemplate instanceof RabbitTemplate) {
((RabbitTemplate) this.amqpTemplate).setMessageConverter(messageConverter);
if (this.amqpTemplate instanceof RabbitTemplate rabbitTemplate) {
rabbitTemplate.setMessageConverter(messageConverter);
}
else if (logger.isInfoEnabled()) {
logger.info("AmqpTemplate is not a RabbitTemplate, so configured MessageConverter will be ignored.");
@@ -215,8 +216,8 @@ public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChan
}
public void setTemplateChannelTransacted(boolean channelTransacted) {
if (this.amqpTemplate instanceof RabbitTemplate) {
((RabbitTemplate) this.amqpTemplate).setChannelTransacted(channelTransacted);
if (this.amqpTemplate instanceof RabbitTemplate rabbitTemplate) {
rabbitTemplate.setChannelTransacted(channelTransacted);
}
else if (logger.isInfoEnabled()) {
logger.info("AmqpTemplate is not a RabbitTemplate, so configured 'channelTransacted' will be ignored.");
@@ -233,15 +234,15 @@ public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChan
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
if (this.amqpTemplate instanceof RabbitTemplate) {
((RabbitTemplate) this.amqpTemplate).setConnectionFactory(this.connectionFactory);
if (this.amqpTemplate instanceof RabbitTemplate rabbitTemplate) {
rabbitTemplate.setConnectionFactory(this.connectionFactory);
}
}
public void setMessagePropertiesConverter(MessagePropertiesConverter messagePropertiesConverter) {
this.messagePropertiesConverter = messagePropertiesConverter;
if (this.amqpTemplate instanceof RabbitTemplate) {
((RabbitTemplate) this.amqpTemplate).setMessagePropertiesConverter(messagePropertiesConverter);
if (this.amqpTemplate instanceof RabbitTemplate rabbitTemplate) {
rabbitTemplate.setMessagePropertiesConverter(messagePropertiesConverter);
}
}
@@ -354,8 +355,8 @@ public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChan
protected AbstractAmqpChannel createInstance() {
if (this.messageDriven) {
AbstractMessageListenerContainer container = this.createContainer();
if (this.amqpTemplate instanceof RabbitAccessor) {
((RabbitAccessor) this.amqpTemplate).afterPropertiesSet();
if (this.amqpTemplate instanceof RabbitAccessor rabbitAccessor) {
rabbitAccessor.afterPropertiesSet();
}
if (this.isPubSub) {
PublishSubscribeAmqpChannel pubsub = new PublishSubscribeAmqpChannel(
@@ -440,38 +441,38 @@ public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChan
@Override
public boolean isAutoStartup() {
return (this.channel instanceof SmartLifecycle) && ((SmartLifecycle) this.channel).isAutoStartup();
return (this.channel instanceof SmartLifecycle smartLifecycle) && smartLifecycle.isAutoStartup();
}
@Override
public int getPhase() {
return (this.channel instanceof SmartLifecycle) ?
((SmartLifecycle) this.channel).getPhase() : 0;
return (this.channel instanceof SmartLifecycle smartLifecycle) ?
smartLifecycle.getPhase() : 0;
}
@Override
public boolean isRunning() {
return (this.channel instanceof Lifecycle) && ((Lifecycle) this.channel).isRunning();
return (this.channel instanceof Lifecycle lifecycle) && lifecycle.isRunning();
}
@Override
public void start() {
if (this.channel instanceof Lifecycle) {
((Lifecycle) this.channel).start();
if (this.channel instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
@Override
public void stop() {
if (this.channel instanceof Lifecycle) {
((Lifecycle) this.channel).stop();
if (this.channel instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
@Override
public void stop(Runnable callback) {
if (this.channel instanceof SmartLifecycle) {
((SmartLifecycle) this.channel).stop(callback);
if (this.channel instanceof SmartLifecycle smartLifecycle) {
smartLifecycle.stop(callback);
}
else {
callback.run();

View File

@@ -63,6 +63,7 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
* @author Ngoc Nhan
*
* @since 2.1
*/
@@ -136,8 +137,8 @@ public class AmqpInboundChannelAdapter extends MessageProducerSupport implements
this.messageListenerContainer = listenerContainer;
this.messageListenerContainer.setAutoStartup(false);
setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
this.abstractListenerContainer = listenerContainer instanceof AbstractMessageListenerContainer
? (AbstractMessageListenerContainer) listenerContainer
this.abstractListenerContainer = listenerContainer instanceof AbstractMessageListenerContainer abstractMessageListenerContainer
? abstractMessageListenerContainer
: null;
}

View File

@@ -62,6 +62,7 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
* @author Ngoc Nhan
*
* @since 2.1
*/
@@ -123,12 +124,12 @@ public class AmqpInboundGateway extends MessagingGatewaySupport {
this.messageListenerContainer.setAutoStartup(false);
this.amqpTemplate = amqpTemplate;
this.amqpTemplateExplicitlySet = amqpTemplateExplicitlySet;
if (this.amqpTemplateExplicitlySet && this.amqpTemplate instanceof RabbitTemplate) {
this.templateMessageConverter = ((RabbitTemplate) this.amqpTemplate).getMessageConverter();
if (this.amqpTemplateExplicitlySet && this.amqpTemplate instanceof RabbitTemplate rabbitTemplate) {
this.templateMessageConverter = rabbitTemplate.getMessageConverter();
}
setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
this.abstractListenerContainer = listenerContainer instanceof AbstractMessageListenerContainer
? (AbstractMessageListenerContainer) listenerContainer
this.abstractListenerContainer = listenerContainer instanceof AbstractMessageListenerContainer abstractMessageListenerContainer
? abstractMessageListenerContainer
: null;
}

View File

@@ -61,6 +61,7 @@ import org.springframework.util.StringUtils;
* @author Gary Russell
* @author Artem Bilan
* @author Christian Tzolov
* @author Ngoc Nhan
*
* @since 4.3
*
@@ -594,8 +595,8 @@ public abstract class AbstractAmqpOutboundEndpoint extends AbstractReplyProducin
}
if (correlationData == null) {
Object correlation = requestMessage.getHeaders().get(AmqpHeaders.PUBLISH_CONFIRM_CORRELATION);
if (correlation instanceof CorrelationData) {
correlationData = (CorrelationData) correlation;
if (correlation instanceof CorrelationData castCorrelationData) {
correlationData = castCorrelationData;
}
if (correlationData != null) {
correlationData = new CorrelationDataWrapper(messageId, correlationData, requestMessage);
@@ -728,8 +729,8 @@ public abstract class AbstractAmqpOutboundEndpoint extends AbstractReplyProducin
@Override
public CompletableFuture<Confirm> getFuture() {
if (this.userData instanceof CorrelationData) {
return ((CorrelationData) this.userData).getFuture();
if (this.userData instanceof CorrelationData correlationData) {
return correlationData.getFuture();
}
else {
return super.getFuture();
@@ -738,8 +739,8 @@ public abstract class AbstractAmqpOutboundEndpoint extends AbstractReplyProducin
@Override
public void setReturned(ReturnedMessage returned) {
if (this.userData instanceof CorrelationData) {
((CorrelationData) this.userData).setReturned(returned);
if (this.userData instanceof CorrelationData correlationData) {
correlationData.setReturned(returned);
}
super.setReturned(returned);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
@@ -44,6 +44,7 @@ import org.springframework.util.Assert;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
* @author Ngoc Nhan
*
* @since 2.1
*/
@@ -67,9 +68,9 @@ public class AmqpOutboundEndpoint extends AbstractAmqpOutboundEndpoint
public AmqpOutboundEndpoint(AmqpTemplate amqpTemplate) {
Assert.notNull(amqpTemplate, "amqpTemplate must not be null");
this.amqpTemplate = amqpTemplate;
if (amqpTemplate instanceof RabbitTemplate) {
setConnectionFactory(((RabbitTemplate) amqpTemplate).getConnectionFactory());
this.rabbitTemplate = (RabbitTemplate) amqpTemplate;
if (amqpTemplate instanceof RabbitTemplate castRabbitTemplate) {
setConnectionFactory(castRabbitTemplate.getConnectionFactory());
this.rabbitTemplate = castRabbitTemplate;
}
else {
this.rabbitTemplate = null;
@@ -159,8 +160,8 @@ public class AmqpOutboundEndpoint extends AbstractAmqpOutboundEndpoint
@Override
protected void doStop() {
if (this.amqpTemplate instanceof Lifecycle) {
((Lifecycle) this.amqpTemplate).stop();
if (this.amqpTemplate instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}