Skip to content

Commit c1ccb07

Browse files
authored
Merge pull request #1879 from davvd/fix-1858-equals-hashcode
#1858: fix asymmetric equals in collection.Immutable
2 parents 603eb9e + 48b9a9b commit c1ccb07

2 files changed

Lines changed: 17 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
}

src/test/java/org/cactoos/collection/ImmutableTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.cactoos.collection;
66

7+
import java.util.Collection;
78
import java.util.List;
89
import org.cactoos.list.ListOf;
910
import org.hamcrest.MatcherAssert;
@@ -234,4 +235,17 @@ void testEquals() {
234235
)
235236
);
236237
}
238+
239+
@Test
240+
void equalsIsSymmetricWithPlainList() {
241+
final List<Integer> plain = new ListOf<>(1, 2, 3);
242+
final Collection<Integer> wrapper = new Immutable<>(
243+
new ListOf<>(1, 2, 3)
244+
);
245+
MatcherAssert.assertThat(
246+
"equals() must be symmetric between wrapper and plain list",
247+
wrapper.equals(plain),
248+
new IsEqual<>(plain.equals(wrapper))
249+
);
250+
}
237251
}

0 commit comments

Comments
 (0)