Skip to content

Implement equals,hashCode on ExecutionAttributes #2457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ public ExecutionAttributes copy() {
return toBuilder().build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

ExecutionAttributes that = (ExecutionAttributes) o;

return attributes != null ? attributes.equals(that.attributes) : that.attributes == null;
}

@Override
public int hashCode() {
return attributes != null ? attributes.hashCode() : 0;
}

public static ExecutionAttributes unmodifiableExecutionAttributes(ExecutionAttributes attributes) {
return new UnmodifiableExecutionAttributes(attributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public void testConfigurationEquals() {
.build();

assertThat(request1Override).isEqualTo(request1Override);
assertThat(request1Override).isNotEqualTo(request2Override);
assertThat(request1Override).isEqualTo(request2Override);
assertThat(request1Override).isNotEqualTo(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.core.interceptor;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;

public class ExecutionAttributesTest {
private static final ExecutionAttribute<String> ATTR_1 = new ExecutionAttribute<>("Attr1");
private static final ExecutionAttribute<String> ATTR_2 = new ExecutionAttribute<>("Attr2");

@Test
public void equals_identity_returnsTrue() {
ExecutionAttributes executionAttributes = ExecutionAttributes.builder()
.put(ATTR_1, "hello")
.put(ATTR_2, "world")
.build();

assertThat(executionAttributes.equals(executionAttributes)).isTrue();
}

@Test
public void equals_sameAttributes_returnsTrue() {
ExecutionAttributes executionAttributes1 = ExecutionAttributes.builder()
.put(ATTR_1, "hello")
.put(ATTR_2, "world")
.build();

ExecutionAttributes executionAttributes2 = ExecutionAttributes.builder()
.put(ATTR_1, "hello")
.put(ATTR_2, "world")
.build();

assertThat(executionAttributes1).isEqualTo(executionAttributes2);
assertThat(executionAttributes2).isEqualTo(executionAttributes1);
}

@Test
public void equals_differentAttributes_returnsFalse() {
ExecutionAttributes executionAttributes1 = ExecutionAttributes.builder()
.put(ATTR_1, "HELLO")
.put(ATTR_2, "WORLD")
.build();

ExecutionAttributes executionAttributes2 = ExecutionAttributes.builder()
.put(ATTR_1, "hello")
.put(ATTR_2, "world")
.build();

assertThat(executionAttributes1).isNotEqualTo(executionAttributes2);
assertThat(executionAttributes2).isNotEqualTo(executionAttributes1);
}


@Test
public void hashCode_objectsEqual_valuesEqual() {
ExecutionAttributes executionAttributes1 = ExecutionAttributes.builder()
.put(ATTR_1, "hello")
.put(ATTR_2, "world")
.build();

ExecutionAttributes executionAttributes2 = ExecutionAttributes.builder()
.put(ATTR_1, "hello")
.put(ATTR_2, "world")
.build();

assertThat(executionAttributes1.hashCode()).isEqualTo(executionAttributes2.hashCode());
}
}