Skip to content

Commit a597c15

Browse files
Modernize more code for diamond, isEmpty & pattern matching
1 parent ce9c67c commit a597c15

File tree

18 files changed

+56
-54
lines changed

18 files changed

+56
-54
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/BindingBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static final class DestinationConfigurer {
8282
}
8383

8484
public Binding to(FanoutExchange exchange) {
85-
return new Binding(this.queue, this.name, this.type, exchange.getName(), "", new HashMap<String, Object>());
85+
return new Binding(this.queue, this.name, this.type, exchange.getName(), "", new HashMap<>());
8686
}
8787

8888
public HeadersExchangeMapConfigurer to(HeadersExchange exchange) {

spring-rabbit-stream/src/main/java/org/springframework/rabbit/stream/listener/StreamListenerContainer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 the original author or authors.
2+
* Copyright 2021-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.
@@ -56,6 +56,7 @@
5656
*
5757
* @author Gary Russell
5858
* @author Christian Tzolov
59+
* @author Ngoc Nhan
5960
* @since 2.4
6061
*
6162
*/
@@ -251,7 +252,7 @@ public void afterPropertiesSet() {
251252
public boolean isRunning() {
252253
this.lock.lock();
253254
try {
254-
return this.consumers.size() > 0;
255+
return !this.consumers.isEmpty();
255256
}
256257
finally {
257258
this.lock.unlock();
@@ -262,7 +263,7 @@ public boolean isRunning() {
262263
public void start() {
263264
this.lock.lock();
264265
try {
265-
if (this.consumers.size() == 0) {
266+
if (this.consumers.isEmpty()) {
266267
this.consumerCustomizer.accept(getListenerId(), this.builder);
267268
if (this.simpleStream) {
268269
this.consumers.add(this.builder.build());

spring-rabbit-stream/src/main/java/org/springframework/rabbit/stream/support/converter/DefaultStreamMessageConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* Default {@link StreamMessageConverter}.
4242
*
4343
* @author Gary Russell
44+
* @author Ngoc Nhan
4445
* @since 2.4
4546
*
4647
*/
@@ -105,7 +106,7 @@ public com.rabbitmq.stream.Message fromMessage(Message message) throws MessageCo
105106
.acceptIfNotNull(mProps.getGroupSequence(), propsBuilder::groupSequence)
106107
.acceptIfNotNull(mProps.getReplyToGroupId(), propsBuilder::replyToGroupId);
107108
ApplicationPropertiesBuilder appPropsBuilder = builder.applicationProperties();
108-
if (mProps.getHeaders().size() > 0) {
109+
if (!mProps.getHeaders().isEmpty()) {
109110
mProps.getHeaders().forEach((key, val) -> {
110111
mapProp(key, val, appPropsBuilder);
111112
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,12 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
317317

318318
private TypeMetadata buildMetadata(Class<?> targetClass) {
319319
List<RabbitListener> classLevelListeners = findListenerAnnotations(targetClass);
320-
final boolean hasClassLevelListeners = classLevelListeners.size() > 0;
320+
final boolean hasClassLevelListeners = !classLevelListeners.isEmpty();
321321
final List<ListenerMethod> methods = new ArrayList<>();
322322
final List<Method> multiMethods = new ArrayList<>();
323323
ReflectionUtils.doWithMethods(targetClass, method -> {
324324
List<RabbitListener> listenerAnnotations = findListenerAnnotations(method);
325-
if (listenerAnnotations.size() > 0) {
325+
if (!listenerAnnotations.isEmpty()) {
326326
methods.add(new ListenerMethod(method,
327327
listenerAnnotations.toArray(new RabbitListener[listenerAnnotations.size()])));
328328
}
@@ -880,7 +880,7 @@ private Map<String, Object> resolveArguments(Argument[] arguments) {
880880
}
881881
}
882882
}
883-
return map.size() < 1 ? null : map;
883+
return map.isEmpty() ? null : map;
884884
}
885885

886886
private void addToMap(Map<String, Object> map, String key, Object value, Class<?> typeClass, String typeName) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public MessageBatch addToBatch(String exch, String routKey, Message message) {
8888
}
8989
int bufferUse = Integer.BYTES + message.getBody().length;
9090
MessageBatch batch = null;
91-
if (this.messages.size() > 0 && this.currentSize + bufferUse > this.bufferLimit) {
91+
if (!this.messages.isEmpty() && this.currentSize + bufferUse > this.bufferLimit) {
9292
batch = doReleaseBatch();
9393
this.exchange = exch;
9494
this.routingKey = routKey;
@@ -104,7 +104,7 @@ public MessageBatch addToBatch(String exch, String routKey, Message message) {
104104

105105
@Override
106106
public Date nextRelease() {
107-
if (this.messages.size() == 0 || this.timeout <= 0) {
107+
if (this.messages.isEmpty() || this.timeout <= 0) {
108108
return null;
109109
}
110110
else if (this.currentSize >= this.bufferLimit) {
@@ -128,7 +128,7 @@ public Collection<MessageBatch> releaseBatches() {
128128
}
129129

130130
private MessageBatch doReleaseBatch() {
131-
if (this.messages.size() < 1) {
131+
if (this.messages.isEmpty()) {
132132
return null;
133133
}
134134
Message message = assembleMessage();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -30,6 +30,7 @@
3030
* @author Dave Syer
3131
* @author Gary Russell
3232
* @author Artem Bilan
33+
* @author Ngoc Nhan
3334
*
3435
*/
3536
public class HeadersExchangeParser extends AbstractExchangeParser {
@@ -63,7 +64,7 @@ protected BeanDefinitionBuilder parseBinding(String exchangeName, Element bindin
6364
parserContext.getReaderContext()
6465
.error("At least one of 'binding-arguments' sub-element or 'key/value' attributes pair have to be declared.", binding);
6566
}
66-
ManagedMap<TypedStringValue, TypedStringValue> map = new ManagedMap<TypedStringValue, TypedStringValue>();
67+
ManagedMap<TypedStringValue, TypedStringValue> map = new ManagedMap<>();
6768
map.put(new TypedStringValue(key), new TypedStringValue(value));
6869
builder.addPropertyValue("arguments", map);
6970
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* @author Artem Bilan
5757
* @author Johno Crawford
5858
* @author Jeonggi Kim
59+
* @author Ngoc Nhan
5960
*
6061
* @since 2.0
6162
*
@@ -542,7 +543,7 @@ protected AbstractMessageListenerContainer createInstance() { // NOSONAR complex
542543
.acceptIfNotNull(this.exclusiveConsumerExceptionLogger,
543544
container::setExclusiveConsumerExceptionLogger)
544545
.acceptIfNotNull(this.micrometerEnabled, container::setMicrometerEnabled)
545-
.acceptIfCondition(this.micrometerTags.size() > 0, this.micrometerTags,
546+
.acceptIfCondition(!this.micrometerTags.isEmpty(), this.micrometerTags,
546547
container::setMicrometerTags);
547548
if (this.smlcCustomizer != null && this.type.equals(Type.simple)) {
548549
this.smlcCustomizer.configure((SimpleMessageListenerContainer) container);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 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.
@@ -42,6 +42,7 @@
4242
/**
4343
* @author Mark Fisher
4444
* @author Gary Russell
45+
* @author Ngoc Nhan
4546
* @since 1.0
4647
*/
4748
class ListenerContainerParser implements BeanDefinitionParser {
@@ -188,22 +189,22 @@ private void parseListener(Element listenerEle, Element containerEle, ParserCont
188189
}
189190
else {
190191
String[] names = StringUtils.commaDelimitedListToStringArray(queues);
191-
List<RuntimeBeanReference> values = new ManagedList<RuntimeBeanReference>();
192+
List<RuntimeBeanReference> values = new ManagedList<>();
192193
for (int i = 0; i < names.length; i++) {
193194
values.add(new RuntimeBeanReference(names[i].trim()));
194195
}
195196
containerDef.getPropertyValues().add("queues", values);
196197
}
197198
}
198199

199-
ManagedMap<String, TypedStringValue> args = new ManagedMap<String, TypedStringValue>();
200+
ManagedMap<String, TypedStringValue> args = new ManagedMap<>();
200201

201202
String priority = listenerEle.getAttribute("priority");
202203
if (StringUtils.hasText(priority)) {
203204
args.put("x-priority", new TypedStringValue(priority, Integer.class));
204205
}
205206

206-
if (args.size() > 0) {
207+
if (!args.isEmpty()) {
207208
containerDef.getPropertyValues().add("consumerArguments", args);
208209
}
209210

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -33,6 +33,7 @@
3333
* @author Gary Russell
3434
* @author Felipe Gutierrez
3535
* @author Artem Bilan
36+
* @author Ngoc Nhan
3637
*
3738
*/
3839
public class QueueParser extends AbstractSingleBeanDefinitionParser {
@@ -134,7 +135,7 @@ private void parseArguments(Element element, ParserContext parserContext, BeanDe
134135
Map<?, ?> map = parserContext.getDelegate().parseMapElement(argumentsElement,
135136
builder.getRawBeanDefinition());
136137
if (StringUtils.hasText(ref)) {
137-
if (map != null && map.size() > 0) {
138+
if (map != null && !map.isEmpty()) {
138139
parserContext.getReaderContext()
139140
.error("You cannot have both a 'ref' and a nested map", element);
140141
}

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 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.
@@ -358,28 +358,22 @@ public static BeanDefinition parseContainer(Element containerEle, ParserContext
358358
}
359359

360360
private static AcknowledgeMode parseAcknowledgeMode(Element ele, ParserContext parserContext) {
361-
AcknowledgeMode acknowledgeMode = null;
362361
String acknowledge = ele.getAttribute(ACKNOWLEDGE_ATTRIBUTE);
363362
if (StringUtils.hasText(acknowledge)) {
364-
if (ACKNOWLEDGE_AUTO.equals(acknowledge)) {
365-
acknowledgeMode = AcknowledgeMode.AUTO;
366-
}
367-
else if (ACKNOWLEDGE_MANUAL.equals(acknowledge)) {
368-
acknowledgeMode = AcknowledgeMode.MANUAL;
369-
}
370-
else if (ACKNOWLEDGE_NONE.equals(acknowledge)) {
371-
acknowledgeMode = AcknowledgeMode.NONE;
372-
}
373-
else {
374-
parserContext.getReaderContext().error(
363+
return switch (acknowledge) {
364+
case ACKNOWLEDGE_AUTO -> AcknowledgeMode.AUTO;
365+
case ACKNOWLEDGE_MANUAL -> AcknowledgeMode.MANUAL;
366+
case ACKNOWLEDGE_NONE -> AcknowledgeMode.NONE;
367+
default -> {
368+
parserContext.getReaderContext().error(
375369
"Invalid listener container 'acknowledge' setting [" + acknowledge
376-
+ "]: only \"auto\", \"manual\", and \"none\" supported.", ele);
377-
}
378-
return acknowledgeMode;
379-
}
380-
else {
381-
return null;
370+
+ "]: only \"auto\", \"manual\", and \"none\" supported.", ele);
371+
yield null;
372+
}
373+
};
382374
}
375+
376+
return null;
383377
}
384378

385379
}

0 commit comments

Comments
 (0)