Skip to content

Commit d87f268

Browse files
Improve conditions for more code readability
* Reduce `else` condition * Improve condition for returning type and avoiding NPE * Polish diamond operator and pattern matching usage
1 parent 347c96a commit d87f268

File tree

12 files changed

+61
-71
lines changed

12 files changed

+61
-71
lines changed

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

Lines changed: 6 additions & 16 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.amqp.core;
1818

19+
import java.util.Objects;
1920
import java.util.regex.Matcher;
2021
import java.util.regex.Pattern;
2122

@@ -39,6 +40,7 @@
3940
* @author Dave Syer
4041
* @author Artem Bilan
4142
* @author Gary Russell
43+
* @author Ngoc Nhan
4244
*/
4345
public class Address {
4446

@@ -111,21 +113,9 @@ public String getRoutingKey() {
111113

112114
@Override
113115
public boolean equals(Object o) {
114-
if (this == o) {
115-
return true;
116-
}
117-
if (o == null || getClass() != o.getClass()) {
118-
return false;
119-
}
120-
121-
Address address = (Address) o;
122-
123-
return !(this.exchangeName != null
124-
? !this.exchangeName.equals(address.exchangeName)
125-
: address.exchangeName != null)
126-
&& !(this.routingKey != null
127-
? !this.routingKey.equals(address.routingKey)
128-
: address.routingKey != null);
116+
return o instanceof Address address
117+
&& Objects.equals(this.exchangeName, address.exchangeName)
118+
&& Objects.equals(this.routingKey, address.routingKey);
129119
}
130120

131121
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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 Mark Fisher
3131
* @author Dave Syer
3232
* @author Gary Russell
33+
* @author Ngoc Nhan
3334
*
3435
* @see AmqpAdmin
3536
*/
@@ -74,7 +75,7 @@ public Binding(@Nullable Queue lazyQueue, @Nullable String destination, Destinat
7475
String exchange, @Nullable String routingKey, @Nullable Map<String, Object> arguments) {
7576

7677
super(arguments);
77-
Assert.isTrue(lazyQueue == null || destinationType.equals(DestinationType.QUEUE),
78+
Assert.isTrue(lazyQueue == null || destinationType == DestinationType.QUEUE,
7879
"'lazyQueue' must be null for destination type " + destinationType);
7980
Assert.isTrue(lazyQueue != null || destination != null, "`destination` cannot be null");
8081
this.lazyQueue = lazyQueue;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ public static final class TopicExchangeRoutingKeyConfigurer extends AbstractRout
231231

232232
public Binding with(String routingKey) {
233233
return new Binding(destination.queue, destination.name, destination.type, exchange, routingKey,
234-
Collections.<String, Object>emptyMap());
234+
Collections.emptyMap());
235235
}
236236

237237
public Binding with(Enum<?> routingKeyEnum) {
238238
return new Binding(destination.queue, destination.name, destination.type, exchange,
239-
routingKeyEnum.toString(), Collections.<String, Object>emptyMap());
239+
routingKeyEnum.toString(), Collections.emptyMap());
240240
}
241241
}
242242

@@ -282,7 +282,7 @@ public Binding and(Map<String, Object> map) {
282282
public Binding noargs() {
283283
return new Binding(this.configurer.destination.queue,
284284
this.configurer.destination.name, this.configurer.destination.type, this.configurer.exchange,
285-
this.routingKey, Collections.<String, Object>emptyMap());
285+
this.routingKey, Collections.emptyMap());
286286
}
287287

288288
}
@@ -298,17 +298,17 @@ public static final class DirectExchangeRoutingKeyConfigurer extends AbstractRou
298298

299299
public Binding with(String routingKey) {
300300
return new Binding(destination.queue, destination.name, destination.type, exchange, routingKey,
301-
Collections.<String, Object>emptyMap());
301+
Collections.emptyMap());
302302
}
303303

304304
public Binding with(Enum<?> routingKeyEnum) {
305305
return new Binding(destination.queue, destination.name, destination.type, exchange,
306-
routingKeyEnum.toString(), Collections.<String, Object>emptyMap());
306+
routingKeyEnum.toString(), Collections.emptyMap());
307307
}
308308

309309
public Binding withQueueName() {
310310
return new Binding(destination.queue, destination.name, destination.type, exchange, destination.name,
311-
Collections.<String, Object>emptyMap());
311+
Collections.emptyMap());
312312
}
313313

314314
}

spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractJavaTypeMapper.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,9 @@ protected String retrieveHeader(MessageProperties properties, String headerName)
9999
protected String retrieveHeaderAsString(MessageProperties properties, String headerName) {
100100
Map<String, Object> headers = properties.getHeaders();
101101
Object classIdFieldNameValue = headers.get(headerName);
102-
String classId = null;
103-
if (classIdFieldNameValue != null) {
104-
classId = classIdFieldNameValue.toString();
105-
}
106-
return classId;
102+
return classIdFieldNameValue != null
103+
? classIdFieldNameValue.toString()
104+
: null;
107105
}
108106

109107
private void createReverseMap() {

spring-amqp/src/main/java/org/springframework/amqp/support/converter/ContentTypeDelegatingMessageConverter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ protected MessageConverter getConverterForContentType(String contentType) {
109109
if (delegate == null) {
110110
throw new MessageConversionException("No delegate converter is specified for content type " + contentType);
111111
}
112-
else {
113-
return delegate;
114-
}
112+
113+
return delegate;
115114
}
116115

117116
}

spring-amqp/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java

Lines changed: 5 additions & 5 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.
@@ -40,6 +40,7 @@
4040
* @author Arjen Poutsma
4141
* @author Juergen Hoeller
4242
* @author James Carr
43+
* @author Ngoc Nhan
4344
* @see org.springframework.amqp.core.AmqpTemplate#convertAndSend(Object)
4445
* @see org.springframework.amqp.core.AmqpTemplate#receiveAndConvert()
4546
*/
@@ -77,10 +78,9 @@ public MarshallingMessageConverter(Marshaller marshaller) {
7778
"interface. Please set an Unmarshaller explicitly by using the " +
7879
"MarshallingMessageConverter(Marshaller, Unmarshaller) constructor.");
7980
}
80-
else {
81-
this.marshaller = marshaller;
82-
this.unmarshaller = (Unmarshaller) marshaller;
83-
}
81+
82+
this.marshaller = marshaller;
83+
this.unmarshaller = (Unmarshaller) marshaller;
8484
}
8585

8686
/**

spring-amqp/src/main/java/org/springframework/amqp/support/converter/MessagingMessageConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-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.
@@ -40,6 +40,7 @@
4040
* is considered to be a request).
4141
*
4242
* @author Stephane Nicoll
43+
* @author Ngoc Nhan
4344
* @since 1.4
4445
*/
4546
public class MessagingMessageConverter implements MessageConverter, InitializingBean {
@@ -104,11 +105,10 @@ public void afterPropertiesSet() {
104105
public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties)
105106
throws MessageConversionException {
106107

107-
if (!(object instanceof Message)) {
108+
if (!(object instanceof Message<?> input)) {
108109
throw new IllegalArgumentException("Could not convert [" + object + "] - only [" +
109110
Message.class.getName() + "] is handled by this converter");
110111
}
111-
Message<?> input = (Message<?>) object;
112112
this.headerMapper.fromHeaders(input.getHeaders(), messageProperties);
113113
org.springframework.amqp.core.Message amqpMessage = this.payloadConverter.toMessage(
114114
input.getPayload(), messageProperties);

spring-amqp/src/main/java/org/springframework/amqp/support/converter/RemoteInvocationResult.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Juergen Hoeller
2828
* @author Gary Russell
29+
* @author Ngoc Nhan
2930
* @since 3.0
3031
*/
3132
public class RemoteInvocationResult implements Serializable {
@@ -142,16 +143,13 @@ public boolean hasInvocationTargetException() {
142143
@Nullable
143144
public Object recreate() throws Throwable {
144145
if (this.exception != null) {
145-
Throwable exToThrow = this.exception;
146-
if (this.exception instanceof InvocationTargetException invocationTargetException) {
147-
exToThrow = invocationTargetException.getTargetException();
148-
}
146+
Throwable exToThrow = this.exception instanceof InvocationTargetException invocationTargetException
147+
? invocationTargetException.getTargetException()
148+
: this.exception;
149149
RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow);
150150
throw exToThrow;
151151
}
152-
else {
153-
return this.value;
154-
}
152+
return this.value;
155153
}
156154

157155
}

spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/AbstractDecompressingPostProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* the final content encoding of the decompressed message.
3939
*
4040
* @author Gary Russell
41+
* @author Ngoc Nhan
4142
* @since 1.4.2
4243
*/
4344
public abstract class AbstractDecompressingPostProcessor implements MessagePostProcessor, Ordered {
@@ -115,9 +116,8 @@ public Message postProcessMessage(Message message) throws AmqpException {
115116
throw new AmqpIOException(e);
116117
}
117118
}
118-
else {
119-
return message;
120-
}
119+
120+
return message;
121121
}
122122

123123
/**

spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/DelegatingDecompressingPostProcessor.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,20 @@ public Message postProcessMessage(Message message) throws AmqpException {
9898
if (encoding == null) {
9999
return message;
100100
}
101-
else {
102-
int delimAt = encoding.indexOf(':');
103-
if (delimAt < 0) {
104-
delimAt = encoding.indexOf(',');
105-
}
106-
if (delimAt > 0) {
107-
encoding = encoding.substring(0, delimAt);
108-
}
109-
MessagePostProcessor decompressor = this.decompressors.get(encoding);
110-
if (decompressor != null) {
111-
return decompressor.postProcessMessage(message);
112-
}
113-
else {
114-
return message;
115-
}
101+
102+
int delimAt = encoding.indexOf(':');
103+
if (delimAt < 0) {
104+
delimAt = encoding.indexOf(',');
105+
}
106+
if (delimAt > 0) {
107+
encoding = encoding.substring(0, delimAt);
116108
}
109+
MessagePostProcessor decompressor = this.decompressors.get(encoding);
110+
if (decompressor != null) {
111+
return decompressor.postProcessMessage(message);
112+
}
113+
114+
return message;
117115
}
118116

119117
}

0 commit comments

Comments
 (0)