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
20 changes: 13 additions & 7 deletions api/src/main/java/org/apache/iceberg/util/CharSequenceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -168,22 +167,29 @@ public void clear() {
}

@Override
public boolean equals(Object o) {
if (this == o) {
public boolean equals(Object other) {
Copy link
Contributor Author

@aokolnychyi aokolnychyi Dec 8, 2023

Choose a reason for hiding this comment

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

See Set#equals() and AbstractSet#equals() for background.

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 Set API. Therefore, I am respecting that to be safe.

return false;
}
}

@Override
public int hashCode() {
return Objects.hashCode(wrapperSet);
return wrapperSet.stream().mapToInt(CharSequenceWrapper::hashCode).sum();
Copy link
Contributor Author

@aokolnychyi aokolnychyi Dec 8, 2023

Choose a reason for hiding this comment

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

The Set API requires the hash code to be a sum of hash codes of all elements. See Set#hashCode().

}

@Override
Expand Down
33 changes: 33 additions & 0 deletions api/src/test/java/org/apache/iceberg/util/TestCharSequenceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am primarily interested in wrapping CharSequenceSet into UnmodifiableSet in CharSequenceMap.


Set<CharSequenceWrapper> set4 =
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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());
}
}