Skip to content

Commit

Permalink
Adds test where elements from 2 generic types are cast to their type
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 5, 2024
1 parent 35de694 commit b2e191e
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public void succeed_whenToStringLooksAtNonCollectionGenericContent() {
EqualsVerifier.forClass(SparseArrayToStringContainer.class).verify();
}

@Test
public void succeed_whenEqualsLooksAtGenericContent_givenTwoGenericFields() {
EqualsVerifier.forClass(TwoGenericsContainerWithIntrospection.class).verify();
}

@Test
public void succeed_whenClassHasTypeVariableThatExtendsSomething() {
EqualsVerifier.forClass(TypeVariableExtendsContainer.class).verify();
Expand Down Expand Up @@ -745,6 +750,38 @@ public String toString() {
}
}

public static final class TwoGenericsContainerWithIntrospection {

private final List<String> stringList = new ArrayList<>();
private final List<Integer> intList = new ArrayList<>();

@SuppressWarnings("unused")
@Override
public boolean equals(Object obj) {
if (stringList != null && stringList.size() > 0) {
String key = stringList.get(0); // force a cast
}
if (intList != null && intList.size() > 0) {
Integer key = intList.get(0); // force a cast
}

if (!(obj instanceof TwoGenericsContainerWithIntrospection)) {
return false;
}
TwoGenericsContainerWithIntrospection other =
(TwoGenericsContainerWithIntrospection) obj;
return (
Objects.equals(stringList, other.stringList) &&
Objects.equals(intList, other.intList)
);
}

@Override
public int hashCode() {
return Objects.hash(stringList, intList);
}
}

@SuppressWarnings("unused")
static final class TypeVariableExtendsContainer<I extends Comparable<I>> {

Expand Down

0 comments on commit b2e191e

Please sign in to comment.