Skip to content

Commit c2a7261

Browse files
Improve condition for more readability
1 parent 09b1479 commit c2a7261

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import java.util.concurrent.locks.Lock;
2626
import java.util.concurrent.locks.ReentrantLock;
2727

28-
2928
import org.springframework.lang.Nullable;
3029
import org.springframework.util.Assert;
30+
import org.springframework.util.ObjectUtils;
3131

3232
/**
3333
* Base class for {@link Declarable} classes.
@@ -40,7 +40,7 @@
4040
*/
4141
public abstract class AbstractDeclarable implements Declarable {
4242

43-
private final Lock lock = new ReentrantLock();
43+
private final Lock lock = new ReentrantLock();
4444

4545
private boolean shouldDeclare = true;
4646

@@ -103,16 +103,18 @@ public void setIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions)
103103

104104
@Override
105105
public void setAdminsThatShouldDeclare(Object... adminArgs) {
106-
Collection<Object> admins = new ArrayList<>();
107-
if (adminArgs != null) {
108-
if (adminArgs.length > 1) {
109-
Assert.noNullElements(adminArgs, "'admins' cannot contain null elements");
110-
}
111-
if (adminArgs.length > 0 && !(adminArgs.length == 1 && adminArgs[0] == null)) {
112-
admins.addAll(Arrays.asList(adminArgs));
113-
}
106+
this.declaringAdmins = new ArrayList<>();
107+
if (ObjectUtils.isEmpty(adminArgs)) {
108+
return;
109+
}
110+
111+
if (adminArgs.length > 1) {
112+
Assert.noNullElements(adminArgs, "'admins' cannot contain null elements");
113+
this.declaringAdmins.addAll(Arrays.asList(adminArgs));
114+
}
115+
else if (adminArgs[0] != null) {
116+
this.declaringAdmins.add(adminArgs[0]);
114117
}
115-
this.declaringAdmins = admins;
116118
}
117119

118120
@Override

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/test/java/org/springframework/amqp/core/AddressTests.java

Lines changed: 5 additions & 1 deletion
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.
@@ -25,6 +25,7 @@
2525
* @author Mark Fisher
2626
* @author Artem Bilan
2727
* @author Gary Russell
28+
* @author Ngoc Nhan
2829
*/
2930
public class AddressTests {
3031

@@ -100,6 +101,9 @@ public void testDirectReplyTo() {
100101
@Test
101102
public void testEquals() {
102103
assertThat(new Address("foo/bar")).isEqualTo(new Address("foo/bar"));
104+
assertThat(new Address("foo", null)).isEqualTo(new Address("foo", null));
105+
assertThat(new Address(null, "bar")).isEqualTo(new Address(null, "bar"));
106+
assertThat(new Address(null, null)).isEqualTo(new Address(null, null));
103107
}
104108

105109
}

0 commit comments

Comments
 (0)