Skip to content
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
13 changes: 12 additions & 1 deletion core/src/main/java/io/grpc/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,18 @@ public boolean equals(Object o) {
return false;
}
Attributes that = (Attributes) o;
return Objects.equal(data, that.data);
if (data.size() != that.data.size()) {
return false;
}
for (Entry<Key<?>, Object> e : data.entrySet()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only handles one direction. that.data may contain an extra element, but this would incorrectly say they were equal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sizes are checked above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right. kk

if (!that.data.containsKey(e.getKey())) {
return false;
}
if (!Objects.equal(e.getValue(), that.data.get(e.getKey()))) {
return false;
}
}
return true;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/io/grpc/AttributesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

import org.junit.Test;
Expand Down Expand Up @@ -50,4 +51,20 @@ public void duplicates() {
public void empty() {
assertEquals(0, Attributes.EMPTY.keys().size());
}

@Test
@SuppressWarnings("BoxedPrimitiveConstructor")
public void valueEquality() {
Attributes.Key<Integer> key = Attributes.Key.of("ints");
Integer v1 = new Integer(100000);
Integer v2 = new Integer(100000);

assertNotSame(v1, v2);
assertEquals(v1, v2);

Attributes attr1 = Attributes.newBuilder().set(key, v1).build();
Attributes attr2 = Attributes.newBuilder().set(key, v2).build();

assertEquals(attr1, attr2);
}
}