Skip to content

Commit

Permalink
Improve conditions for more code readability
Browse files Browse the repository at this point in the history
* Reduce `else` condition
* Improve condition for returning type and avoiding NPE
* Polish diamond operator and pattern matching usage
  • Loading branch information
ngocnhan-tran1996 authored Oct 16, 2024
1 parent 347c96a commit d87f268
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.amqp.core;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -39,6 +40,7 @@
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
* @author Ngoc Nhan
*/
public class Address {

Expand Down Expand Up @@ -111,21 +113,9 @@ public String getRoutingKey() {

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Address address = (Address) o;

return !(this.exchangeName != null
? !this.exchangeName.equals(address.exchangeName)
: address.exchangeName != null)
&& !(this.routingKey != null
? !this.routingKey.equals(address.routingKey)
: address.routingKey != null);
return o instanceof Address address
&& Objects.equals(this.exchangeName, address.exchangeName)
&& Objects.equals(this.routingKey, address.routingKey);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -30,6 +30,7 @@
* @author Mark Fisher
* @author Dave Syer
* @author Gary Russell
* @author Ngoc Nhan
*
* @see AmqpAdmin
*/
Expand Down Expand Up @@ -74,7 +75,7 @@ public Binding(@Nullable Queue lazyQueue, @Nullable String destination, Destinat
String exchange, @Nullable String routingKey, @Nullable Map<String, Object> arguments) {

super(arguments);
Assert.isTrue(lazyQueue == null || destinationType.equals(DestinationType.QUEUE),
Assert.isTrue(lazyQueue == null || destinationType == DestinationType.QUEUE,
"'lazyQueue' must be null for destination type " + destinationType);
Assert.isTrue(lazyQueue != null || destination != null, "`destination` cannot be null");
this.lazyQueue = lazyQueue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ public static final class TopicExchangeRoutingKeyConfigurer extends AbstractRout

public Binding with(String routingKey) {
return new Binding(destination.queue, destination.name, destination.type, exchange, routingKey,
Collections.<String, Object>emptyMap());
Collections.emptyMap());
}

public Binding with(Enum<?> routingKeyEnum) {
return new Binding(destination.queue, destination.name, destination.type, exchange,
routingKeyEnum.toString(), Collections.<String, Object>emptyMap());
routingKeyEnum.toString(), Collections.emptyMap());
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ public Binding and(Map<String, Object> map) {
public Binding noargs() {
return new Binding(this.configurer.destination.queue,
this.configurer.destination.name, this.configurer.destination.type, this.configurer.exchange,
this.routingKey, Collections.<String, Object>emptyMap());
this.routingKey, Collections.emptyMap());
}

}
Expand All @@ -298,17 +298,17 @@ public static final class DirectExchangeRoutingKeyConfigurer extends AbstractRou

public Binding with(String routingKey) {
return new Binding(destination.queue, destination.name, destination.type, exchange, routingKey,
Collections.<String, Object>emptyMap());
Collections.emptyMap());
}

public Binding with(Enum<?> routingKeyEnum) {
return new Binding(destination.queue, destination.name, destination.type, exchange,
routingKeyEnum.toString(), Collections.<String, Object>emptyMap());
routingKeyEnum.toString(), Collections.emptyMap());
}

public Binding withQueueName() {
return new Binding(destination.queue, destination.name, destination.type, exchange, destination.name,
Collections.<String, Object>emptyMap());
Collections.emptyMap());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,9 @@ protected String retrieveHeader(MessageProperties properties, String headerName)
protected String retrieveHeaderAsString(MessageProperties properties, String headerName) {
Map<String, Object> headers = properties.getHeaders();
Object classIdFieldNameValue = headers.get(headerName);
String classId = null;
if (classIdFieldNameValue != null) {
classId = classIdFieldNameValue.toString();
}
return classId;
return classIdFieldNameValue != null
? classIdFieldNameValue.toString()
: null;
}

private void createReverseMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ protected MessageConverter getConverterForContentType(String contentType) {
if (delegate == null) {
throw new MessageConversionException("No delegate converter is specified for content type " + contentType);
}
else {
return delegate;
}

return delegate;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
Expand Down Expand Up @@ -40,6 +40,7 @@
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author James Carr
* @author Ngoc Nhan
* @see org.springframework.amqp.core.AmqpTemplate#convertAndSend(Object)
* @see org.springframework.amqp.core.AmqpTemplate#receiveAndConvert()
*/
Expand Down Expand Up @@ -77,10 +78,9 @@ public MarshallingMessageConverter(Marshaller marshaller) {
"interface. Please set an Unmarshaller explicitly by using the " +
"MarshallingMessageConverter(Marshaller, Unmarshaller) constructor.");
}
else {
this.marshaller = marshaller;
this.unmarshaller = (Unmarshaller) marshaller;
}

this.marshaller = marshaller;
this.unmarshaller = (Unmarshaller) marshaller;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2020 the original author or authors.
* Copyright 2014-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.
Expand Down Expand Up @@ -40,6 +40,7 @@
* is considered to be a request).
*
* @author Stephane Nicoll
* @author Ngoc Nhan
* @since 1.4
*/
public class MessagingMessageConverter implements MessageConverter, InitializingBean {
Expand Down Expand Up @@ -104,11 +105,10 @@ public void afterPropertiesSet() {
public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties)
throws MessageConversionException {

if (!(object instanceof Message)) {
if (!(object instanceof Message<?> input)) {
throw new IllegalArgumentException("Could not convert [" + object + "] - only [" +
Message.class.getName() + "] is handled by this converter");
}
Message<?> input = (Message<?>) object;
this.headerMapper.fromHeaders(input.getHeaders(), messageProperties);
org.springframework.amqp.core.Message amqpMessage = this.payloadConverter.toMessage(
input.getPayload(), messageProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Juergen Hoeller
* @author Gary Russell
* @author Ngoc Nhan
* @since 3.0
*/
public class RemoteInvocationResult implements Serializable {
Expand Down Expand Up @@ -142,16 +143,13 @@ public boolean hasInvocationTargetException() {
@Nullable
public Object recreate() throws Throwable {
if (this.exception != null) {
Throwable exToThrow = this.exception;
if (this.exception instanceof InvocationTargetException invocationTargetException) {
exToThrow = invocationTargetException.getTargetException();
}
Throwable exToThrow = this.exception instanceof InvocationTargetException invocationTargetException
? invocationTargetException.getTargetException()
: this.exception;
RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow);
throw exToThrow;
}
else {
return this.value;
}
return this.value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* the final content encoding of the decompressed message.
*
* @author Gary Russell
* @author Ngoc Nhan
* @since 1.4.2
*/
public abstract class AbstractDecompressingPostProcessor implements MessagePostProcessor, Ordered {
Expand Down Expand Up @@ -115,9 +116,8 @@ public Message postProcessMessage(Message message) throws AmqpException {
throw new AmqpIOException(e);
}
}
else {
return message;
}

return message;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,20 @@ public Message postProcessMessage(Message message) throws AmqpException {
if (encoding == null) {
return message;
}
else {
int delimAt = encoding.indexOf(':');
if (delimAt < 0) {
delimAt = encoding.indexOf(',');
}
if (delimAt > 0) {
encoding = encoding.substring(0, delimAt);
}
MessagePostProcessor decompressor = this.decompressors.get(encoding);
if (decompressor != null) {
return decompressor.postProcessMessage(message);
}
else {
return message;
}

int delimAt = encoding.indexOf(':');
if (delimAt < 0) {
delimAt = encoding.indexOf(',');
}
if (delimAt > 0) {
encoding = encoding.substring(0, delimAt);
}
MessagePostProcessor decompressor = this.decompressors.get(encoding);
if (decompressor != null) {
return decompressor.postProcessMessage(message);
}

return message;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-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.
Expand All @@ -25,6 +25,7 @@
* @author Iwein Fuld
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Ngoc Nhan
* @since 1.2
*/
public final class TestUtils {
Expand All @@ -47,13 +48,14 @@ public static Object getPropertyValue(Object root, String propertyPath) {
value = accessor.getPropertyValue(tokens[i]);
if (value != null) {
accessor = new DirectFieldAccessor(value);
continue;
}
else if (i == tokens.length - 1) {

if (i == tokens.length - 1) {
return null;
}
else {
throw new IllegalArgumentException("intermediate property '" + tokens[i] + "' is null");
}

throw new IllegalArgumentException("intermediate property '" + tokens[i] + "' is null");
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
Expand All @@ -25,6 +25,7 @@
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
* @author Ngoc Nhan
*/
public class AddressTests {

Expand Down Expand Up @@ -100,6 +101,9 @@ public void testDirectReplyTo() {
@Test
public void testEquals() {
assertThat(new Address("foo/bar")).isEqualTo(new Address("foo/bar"));
assertThat(new Address("foo", null)).isEqualTo(new Address("foo", null));
assertThat(new Address(null, "bar")).isEqualTo(new Address(null, "bar"));
assertThat(new Address(null, null)).isEqualTo(new Address(null, null));
}

}

0 comments on commit d87f268

Please sign in to comment.