-
Notifications
You must be signed in to change notification settings - Fork 3k
API: Fix equals and hashCode in CharSequenceSet #9245
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,6 @@ | |
| import java.io.Serializable; | ||
| import java.util.Collection; | ||
| import java.util.Iterator; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
|
|
@@ -168,22 +167,29 @@ public void clear() { | |
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| public boolean equals(Object other) { | ||
| if (this == other) { | ||
| return true; | ||
| } else if (!(other instanceof Set)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (o == null || getClass() != o.getClass()) { | ||
| Set<?> that = (Set<?>) other; | ||
|
|
||
| if (size() != that.size()) { | ||
| return false; | ||
| } | ||
|
|
||
| CharSequenceSet that = (CharSequenceSet) o; | ||
| return wrapperSet.equals(that.wrapperSet); | ||
| try { | ||
| return containsAll(that); | ||
| } catch (ClassCastException | NullPointerException unused) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this class does not throw these exceptions now, it may in the future. These exceptions are valid outcomes defined by the |
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(wrapperSet); | ||
| return wrapperSet.stream().mapToInt(CharSequenceWrapper::hashCode).sum(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,10 @@ | |
| package org.apache.iceberg.util; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
@@ -79,4 +81,35 @@ public void testRemoveAll() { | |
|
|
||
| Assertions.assertThat(set).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEqualsAndHashCode() { | ||
| CharSequenceSet set1 = CharSequenceSet.empty(); | ||
| CharSequenceSet set2 = CharSequenceSet.empty(); | ||
|
|
||
| Assertions.assertThat(set1).isEqualTo(set2); | ||
| Assertions.assertThat(set1.hashCode()).isEqualTo(set2.hashCode()); | ||
|
|
||
| set1.add("v1"); | ||
| set1.add("v2"); | ||
| set1.add("v3"); | ||
|
|
||
| set2.add(new StringBuilder("v1")); | ||
| set2.add(new StringBuffer("v2")); | ||
| set2.add("v3"); | ||
|
|
||
| Set<CharSequence> set3 = Collections.unmodifiableSet(set2); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am primarily interested in wrapping |
||
|
|
||
| Set<CharSequenceWrapper> set4 = | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one works too now. |
||
| ImmutableSet.of( | ||
| CharSequenceWrapper.wrap("v1"), | ||
| CharSequenceWrapper.wrap(new StringBuffer("v2")), | ||
| CharSequenceWrapper.wrap(new StringBuffer("v3"))); | ||
|
|
||
| Assertions.assertThat(set1).isEqualTo(set2).isEqualTo(set3).isEqualTo(set4); | ||
| Assertions.assertThat(set1.hashCode()) | ||
| .isEqualTo(set2.hashCode()) | ||
| .isEqualTo(set3.hashCode()) | ||
| .isEqualTo(set4.hashCode()); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See
Set#equals()andAbstractSet#equals()for background.