Skip to content

Commit 48b9a9b

Browse files
committed
#1858: restrict collection.Immutable equals to fellow Immutable wrappers
Delegating equals() to the inner collection produced asymmetric equality when compared with a plain list: 'wrapper.equals(list)' was true (via delegation) but 'list.equals(wrapper)' was false because Immutable is a Collection, not a List. The Collection interface adds no stipulations to Object.equals, so restricting equals() to other Immutable instances keeps the wrapper symmetric with any plain List/Set while still allowing two wrappers of equal content to compare equal. hashCode() keeps delegating to the inner collection — equal objects still have equal hashCodes; unequal objects may share a hash, which is permitted.
1 parent 2a86f62 commit 48b9a9b

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

src/main/java/org/cactoos/collection/Immutable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public int hashCode() {
120120

121121
@Override
122122
public boolean equals(final Object obj) {
123-
return this.col.equals(obj);
123+
return this == obj
124+
|| obj instanceof Immutable
125+
&& this.col.equals(((Immutable<?>) obj).col);
124126
}
125127
}

0 commit comments

Comments
 (0)