Custom subjects: Add wildcards for generic types
Prior to I9a3b7a877, we had those wildcards in OptionalSubject. As the
new approach applied in that change tied us even more to the generic
type bounds of Subject and of factories for standard Subject types, we
had to remove those wildcards. With the generic types of Subject gone
(I75f3d1510), we can now add the wildcards again and even do so for the
similarly structured ListSubject.
The used wildcards follow the PECS principle.
Change-Id: I4500e702520d24a8cfde54d26c8760772d3ec22e
diff --git a/java/com/google/gerrit/truth/ListSubject.java b/java/com/google/gerrit/truth/ListSubject.java
index bee7408..9f93964 100644
--- a/java/com/google/gerrit/truth/ListSubject.java
+++ b/java/com/google/gerrit/truth/ListSubject.java
@@ -30,10 +30,10 @@
public class ListSubject<S extends Subject, E> extends IterableSubject {
private final List<E> list;
- private final BiFunction<StandardSubjectBuilder, E, S> elementSubjectCreator;
+ private final BiFunction<StandardSubjectBuilder, ? super E, ? extends S> elementSubjectCreator;
public static <S extends Subject, E> ListSubject<S, E> assertThat(
- List<E> list, Subject.Factory<S, E> subjectFactory) {
+ List<E> list, Subject.Factory<? extends S, ? super E> subjectFactory) {
return assertAbout(elements()).thatCustom(list, subjectFactory);
}
@@ -44,7 +44,7 @@
private ListSubject(
FailureMetadata failureMetadata,
List<E> list,
- BiFunction<StandardSubjectBuilder, E, S> elementSubjectCreator) {
+ BiFunction<StandardSubjectBuilder, ? super E, ? extends S> elementSubjectCreator) {
super(failureMetadata, list);
this.list = list;
this.elementSubjectCreator = elementSubjectCreator;
@@ -78,12 +78,13 @@
}
public <S extends Subject, E> ListSubject<S, E> thatCustom(
- List<E> list, Subject.Factory<S, E> subjectFactory) {
+ List<E> list, Subject.Factory<? extends S, ? super E> subjectFactory) {
return that(list, (builder, element) -> builder.about(subjectFactory).that(element));
}
public <S extends Subject, E> ListSubject<S, E> that(
- List<E> list, BiFunction<StandardSubjectBuilder, E, S> elementSubjectCreator) {
+ List<E> list,
+ BiFunction<StandardSubjectBuilder, ? super E, ? extends S> elementSubjectCreator) {
return new ListSubject<>(metadata(), list, elementSubjectCreator);
}
}
diff --git a/java/com/google/gerrit/truth/OptionalSubject.java b/java/com/google/gerrit/truth/OptionalSubject.java
index a87dff1..1c288be 100644
--- a/java/com/google/gerrit/truth/OptionalSubject.java
+++ b/java/com/google/gerrit/truth/OptionalSubject.java
@@ -39,7 +39,7 @@
}
public static <S extends Subject, T> OptionalSubject<S, T> assertThat(
- Optional<T> optional, Subject.Factory<S, T> valueSubjectFactory) {
+ Optional<T> optional, Subject.Factory<? extends S, ? super T> valueSubjectFactory) {
return assertAbout(optionals()).thatCustom(optional, valueSubjectFactory);
}
@@ -91,7 +91,7 @@
}
public <S extends Subject, T> OptionalSubject<S, T> thatCustom(
- Optional<T> optional, Subject.Factory<S, T> valueSubjectFactory) {
+ Optional<T> optional, Subject.Factory<? extends S, ? super T> valueSubjectFactory) {
return that(optional, (builder, value) -> builder.about(valueSubjectFactory).that(value));
}