Skip to content

Commit add7744

Browse files
Reduce else condition and modernize switch pattern
1 parent 366f9da commit add7744

15 files changed

+117
-141
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/annotation/RabbitListenerAnnotationBeanPostProcessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.springframework.context.expression.StandardBeanExpressionResolver;
7777
import org.springframework.core.Ordered;
7878
import org.springframework.core.annotation.AnnotationUtils;
79+
import org.springframework.core.annotation.MergedAnnotation;
7980
import org.springframework.core.annotation.MergedAnnotations;
8081
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
8182
import org.springframework.core.convert.ConversionService;
@@ -357,7 +358,7 @@ else if (source instanceof Method method) {
357358
}
358359
return !name.contains("$MockitoMock$");
359360
})
360-
.map(ann -> ann.synthesize())
361+
.map(MergedAnnotation::synthesize)
361362
.collect(Collectors.toList());
362363
}
363364

@@ -893,7 +894,7 @@ private void addToMap(Map<String, Object> map, String key, Object value, Class<?
893894
}
894895
}
895896
else {
896-
if (value instanceof String && !StringUtils.hasText((String) value)) {
897+
if (value instanceof String string && !StringUtils.hasText(string)) {
897898
putEmpty(map, key);
898899
}
899900
else {

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/batch/SimpleBatchingStrategy.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,12 @@ public Date nextRelease() {
107107
if (this.messages.isEmpty() || this.timeout <= 0) {
108108
return null;
109109
}
110-
else if (this.currentSize >= this.bufferLimit) {
110+
if (this.currentSize >= this.bufferLimit) {
111111
// release immediately, we're already over the limit
112112
return new Date();
113113
}
114-
else {
115-
return new Date(System.currentTimeMillis() + this.timeout);
116-
}
114+
115+
return new Date(System.currentTimeMillis() + this.timeout);
117116
}
118117

119118
@Override
@@ -122,9 +121,8 @@ public Collection<MessageBatch> releaseBatches() {
122121
if (batch == null) {
123122
return Collections.emptyList();
124123
}
125-
else {
126-
return Collections.singletonList(batch);
127-
}
124+
125+
return Collections.singletonList(batch);
128126
}
129127

130128
private MessageBatch doReleaseBatch() {

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/ListenerContainerFactoryBean.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -575,14 +575,13 @@ private AbstractMessageListenerContainer createContainer() {
575575
.acceptIfNotNull(this.retryDeclarationInterval, container::setRetryDeclarationInterval);
576576
return container;
577577
}
578-
else {
579-
DirectMessageListenerContainer container = new DirectMessageListenerContainer(this.connectionFactory);
580-
JavaUtils.INSTANCE
581-
.acceptIfNotNull(this.consumersPerQueue, container::setConsumersPerQueue)
582-
.acceptIfNotNull(this.taskScheduler, container::setTaskScheduler)
583-
.acceptIfNotNull(this.monitorInterval, container::setMonitorInterval);
584-
return container;
585-
}
578+
579+
DirectMessageListenerContainer container = new DirectMessageListenerContainer(this.connectionFactory);
580+
JavaUtils.INSTANCE
581+
.acceptIfNotNull(this.consumersPerQueue, container::setConsumersPerQueue)
582+
.acceptIfNotNull(this.taskScheduler, container::setTaskScheduler)
583+
.acceptIfNotNull(this.monitorInterval, container::setMonitorInterval);
584+
return container;
586585
}
587586

588587
@Override

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/ListenerContainerParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
101101
}
102102

103103
List<Element> childElements = DomUtils.getChildElementsByTagName(element, LISTENER_ELEMENT);
104-
for (int i = 0; i < childElements.size(); i++) {
105-
parseListener(childElements.get(i), element, parserContext, containerList);
104+
for (Element childElement : childElements) {
105+
parseListener(childElement, element, parserContext, containerList);
106106
}
107107

108108
parserContext.popAndRegisterContainingComponent();
@@ -190,8 +190,8 @@ private void parseListener(Element listenerEle, Element containerEle, ParserCont
190190
else {
191191
String[] names = StringUtils.commaDelimitedListToStringArray(queues);
192192
List<RuntimeBeanReference> values = new ManagedList<>();
193-
for (int i = 0; i < names.length; i++) {
194-
values.add(new RuntimeBeanReference(names[i].trim()));
193+
for (String name : names) {
194+
values.add(new RuntimeBeanReference(name.trim()));
195195
}
196196
containerDef.getPropertyValues().add("queues", values);
197197
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/StatefulRetryOperationsInterceptorFactoryBean.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -47,6 +47,7 @@
4747
*
4848
* @author Dave Syer
4949
* @author Gary Russell
50+
* @author Ngoc Nhan
5051
*
5152
* @see RetryOperations#execute(org.springframework.retry.RetryCallback, org.springframework.retry.RecoveryCallback,
5253
* org.springframework.retry.RetryState)
@@ -90,9 +91,8 @@ private NewMethodArgumentsIdentifier createNewItemIdentifier() {
9091
if (StatefulRetryOperationsInterceptorFactoryBean.this.newMessageIdentifier == null) {
9192
return !message.getMessageProperties().isRedelivered();
9293
}
93-
else {
94-
return StatefulRetryOperationsInterceptorFactoryBean.this.newMessageIdentifier.isNew(message);
95-
}
94+
95+
return StatefulRetryOperationsInterceptorFactoryBean.this.newMessageIdentifier.isNew(message);
9696
};
9797
}
9898

@@ -127,23 +127,19 @@ private MethodArgumentsKeyGenerator createKeyGenerator() {
127127
}
128128
return messageId;
129129
}
130-
else {
131-
return StatefulRetryOperationsInterceptorFactoryBean.this.messageKeyGenerator.getKey(message);
132-
}
130+
return StatefulRetryOperationsInterceptorFactoryBean.this.messageKeyGenerator.getKey(message);
133131
};
134132
}
135133

136-
@SuppressWarnings("unchecked")
137134
private Message argToMessage(Object[] args) {
138135
Object arg = args[1];
139-
Message message = null;
140136
if (arg instanceof Message msg) {
141-
message = msg;
137+
return msg;
142138
}
143-
else if (arg instanceof List) {
144-
message = ((List<Message>) arg).get(0);
139+
if (arg instanceof List<?> list) {
140+
return (Message) list.get(0);
145141
}
146-
return message;
142+
return null;
147143
}
148144

149145
@Override

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractRoutingConnectionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setTargetConnectionFactories(Map<Object, ConnectionFactory> targetCo
6969
Assert.noNullElements(targetConnectionFactories.values().toArray(),
7070
"'targetConnectionFactories' cannot have null values.");
7171
this.targetConnectionFactories.putAll(targetConnectionFactories);
72-
targetConnectionFactories.values().stream().forEach(cf -> checkConfirmsAndReturns(cf));
72+
targetConnectionFactories.values().forEach(this::checkConfirmsAndReturns);
7373
}
7474

7575
/**
@@ -293,7 +293,7 @@ public void destroy() {
293293

294294
@Override
295295
public void resetConnection() {
296-
this.targetConnectionFactories.values().forEach(factory -> factory.resetConnection());
296+
this.targetConnectionFactories.values().forEach(ConnectionFactory::resetConnection);
297297
this.defaultTargetConnectionFactory.resetConnection();
298298
}
299299

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ConsumerChannelRegistry.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ public static void unRegisterConsumerChannel() {
8484
@Nullable
8585
public static Channel getConsumerChannel() {
8686
ChannelHolder channelHolder = consumerChannel.get();
87-
Channel channel = null;
88-
if (channelHolder != null) {
89-
channel = channelHolder.getChannel();
90-
}
91-
return channel;
87+
return channelHolder != null
88+
? channelHolder.getChannel()
89+
: null;
9290
}
9391

9492
/**

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/PendingConfirm.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -27,6 +27,7 @@
2727
* expired. It also holds {@link CorrelationData} for
2828
* the client to correlate a confirm with a sent message.
2929
* @author Gary Russell
30+
* @author Ngoc Nhan
3031
* @since 1.0.1
3132
*
3233
*/
@@ -115,7 +116,7 @@ public void setReturned(boolean isReturned) {
115116
* @since 2.2.10
116117
*/
117118
public boolean waitForReturnIfNeeded() throws InterruptedException {
118-
return this.returned ? this.latch.await(RETURN_CALLBACK_TIMEOUT, TimeUnit.SECONDS) : true;
119+
return !this.returned || this.latch.await(RETURN_CALLBACK_TIMEOUT, TimeUnit.SECONDS);
119120
}
120121

121122
/**

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/PooledChannelConnectionFactory.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* @author Gary Russell
5656
* @author Leonardo Ferreira
5757
* @author Christian Tzolov
58+
* @author Ngoc Nhan
5859
* @since 2.3
5960
*
6061
*/
@@ -255,23 +256,21 @@ private Channel createProxy(Channel channel, boolean transacted) {
255256
Advice advice =
256257
(MethodInterceptor) invocation -> {
257258
String method = invocation.getMethod().getName();
258-
switch (method) {
259-
case "close":
260-
handleClose(channel, transacted, proxy);
261-
return null;
262-
case "getTargetChannel":
263-
return channel;
264-
case "isTransactional":
265-
return transacted;
266-
case "confirmSelect":
267-
confirmSelected.set(true);
268-
return channel.confirmSelect();
269-
case "isConfirmSelected":
270-
return confirmSelected.get();
271-
case "isPublisherConfirms":
272-
return false;
273-
}
274-
return null;
259+
return switch (method) {
260+
case "close" -> {
261+
handleClose(channel, transacted, proxy);
262+
yield null;
263+
}
264+
case "getTargetChannel" -> channel;
265+
case "isTransactional" -> transacted;
266+
case "confirmSelect" -> {
267+
confirmSelected.set(true);
268+
yield channel.confirmSelect();
269+
}
270+
case "isConfirmSelected" -> confirmSelected.get();
271+
case "isPublisherConfirms" -> false;
272+
default -> null;
273+
};
275274
};
276275
NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(advice);
277276
advisor.addMethodName("close");

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/PublisherCallbackChannelImpl.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -922,27 +922,26 @@ public Collection<PendingConfirm> expire(Listener listener, long cutoffTime) {
922922
try {
923923
SortedMap<Long, PendingConfirm> pendingConfirmsForListener = this.pendingConfirms.get(listener);
924924
if (pendingConfirmsForListener == null) {
925-
return Collections.<PendingConfirm>emptyList();
925+
return Collections.emptyList();
926926
}
927-
else {
928-
List<PendingConfirm> expired = new ArrayList<>();
929-
Iterator<Entry<Long, PendingConfirm>> iterator = pendingConfirmsForListener.entrySet().iterator();
930-
while (iterator.hasNext()) {
931-
PendingConfirm pendingConfirm = iterator.next().getValue();
932-
if (pendingConfirm.getTimestamp() < cutoffTime) {
933-
expired.add(pendingConfirm);
934-
iterator.remove();
935-
CorrelationData correlationData = pendingConfirm.getCorrelationData();
936-
if (correlationData != null && StringUtils.hasText(correlationData.getId())) {
937-
this.pendingReturns.remove(correlationData.getId()); // NOSONAR never null
938-
}
939-
}
940-
else {
941-
break;
927+
928+
List<PendingConfirm> expired = new ArrayList<>();
929+
Iterator<Entry<Long, PendingConfirm>> iterator = pendingConfirmsForListener.entrySet().iterator();
930+
while (iterator.hasNext()) {
931+
PendingConfirm pendingConfirm = iterator.next().getValue();
932+
if (pendingConfirm.getTimestamp() < cutoffTime) {
933+
expired.add(pendingConfirm);
934+
iterator.remove();
935+
CorrelationData correlationData = pendingConfirm.getCorrelationData();
936+
if (correlationData != null && StringUtils.hasText(correlationData.getId())) {
937+
this.pendingReturns.remove(correlationData.getId()); // NOSONAR never null
942938
}
943939
}
944-
return expired;
940+
else {
941+
break;
942+
}
945943
}
944+
return expired;
946945
}
947946
finally {
948947
this.lock.unlock();

0 commit comments

Comments
 (0)