Skip to content

Commit 281b1de

Browse files
committed
Implement equals,hashCode on ExecutionAttributes
1 parent 69e15bf commit 281b1de

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/interceptor/ExecutionAttributes.java

+20
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ public ExecutionAttributes copy() {
9797
return toBuilder().build();
9898
}
9999

100+
@Override
101+
public boolean equals(Object o) {
102+
if (this == o) {
103+
return true;
104+
}
105+
106+
if (o == null || getClass() != o.getClass()) {
107+
return false;
108+
}
109+
110+
ExecutionAttributes that = (ExecutionAttributes) o;
111+
112+
return attributes != null ? attributes.equals(that.attributes) : that.attributes == null;
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
return attributes != null ? attributes.hashCode() : 0;
118+
}
119+
100120
public static ExecutionAttributes unmodifiableExecutionAttributes(ExecutionAttributes attributes) {
101121
return new UnmodifiableExecutionAttributes(attributes);
102122
}

core/sdk-core/src/test/java/software/amazon/awssdk/core/RequestOverrideConfigurationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public void testConfigurationEquals() {
281281
.build();
282282

283283
assertThat(request1Override).isEqualTo(request1Override);
284-
assertThat(request1Override).isNotEqualTo(request2Override);
284+
assertThat(request1Override).isEqualTo(request2Override);
285285
assertThat(request1Override).isNotEqualTo(null);
286286
}
287287
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.core.interceptor;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import org.junit.Test;
20+
21+
public class ExecutionAttributesTest {
22+
private static final ExecutionAttribute<String> ATTR_1 = new ExecutionAttribute<>("Attr1");
23+
private static final ExecutionAttribute<String> ATTR_2 = new ExecutionAttribute<>("Attr2");
24+
25+
@Test
26+
public void equals_identity_returnsTrue() {
27+
ExecutionAttributes executionAttributes = ExecutionAttributes.builder()
28+
.put(ATTR_1, "hello")
29+
.put(ATTR_2, "world")
30+
.build();
31+
32+
assertThat(executionAttributes.equals(executionAttributes)).isTrue();
33+
}
34+
35+
@Test
36+
public void equals_sameAttributes_returnsTrue() {
37+
ExecutionAttributes executionAttributes1 = ExecutionAttributes.builder()
38+
.put(ATTR_1, "hello")
39+
.put(ATTR_2, "world")
40+
.build();
41+
42+
ExecutionAttributes executionAttributes2 = ExecutionAttributes.builder()
43+
.put(ATTR_1, "hello")
44+
.put(ATTR_2, "world")
45+
.build();
46+
47+
assertThat(executionAttributes1).isEqualTo(executionAttributes2);
48+
assertThat(executionAttributes2).isEqualTo(executionAttributes1);
49+
}
50+
51+
@Test
52+
public void equals_differentAttributes_returnsFalse() {
53+
ExecutionAttributes executionAttributes1 = ExecutionAttributes.builder()
54+
.put(ATTR_1, "HELLO")
55+
.put(ATTR_2, "WORLD")
56+
.build();
57+
58+
ExecutionAttributes executionAttributes2 = ExecutionAttributes.builder()
59+
.put(ATTR_1, "hello")
60+
.put(ATTR_2, "world")
61+
.build();
62+
63+
assertThat(executionAttributes1).isNotEqualTo(executionAttributes2);
64+
assertThat(executionAttributes2).isNotEqualTo(executionAttributes1);
65+
}
66+
67+
68+
@Test
69+
public void hashCode_objectsEqual_valuesEqual() {
70+
ExecutionAttributes executionAttributes1 = ExecutionAttributes.builder()
71+
.put(ATTR_1, "hello")
72+
.put(ATTR_2, "world")
73+
.build();
74+
75+
ExecutionAttributes executionAttributes2 = ExecutionAttributes.builder()
76+
.put(ATTR_1, "hello")
77+
.put(ATTR_2, "world")
78+
.build();
79+
80+
assertThat(executionAttributes1.hashCode()).isEqualTo(executionAttributes2.hashCode());
81+
}
82+
}

0 commit comments

Comments
 (0)