I'm trying to figure out why the compiler throws
test(capture<? extends Serializable>) in Predicate cannot be applied to (Serializable)
for test(e.getValue().getData()) and how I could solve the issue.
The code throwing the compiler error is the following
private Map<Predicate<? extends Serializable>, TaggedData<? extends Serializable>> scheduledData = new HashMap<>();
public synchronized <T extends Serializable> void conditionalSend(String tag, T data, Predicate<T> condition) {
scheduledData.put(condition, new TaggedData<T>(tag, data));
scheduledData.entrySet()
.stream()
.filter(e -> e.getKey().test(e.getValue().getData()))
.forEach(e -> send(e.getValue()));
}
My TaggedData class is defined like this
class TaggedData<T extends Serializable> implements Serializable {
private static final long serialVersionUID = 1469827769491692358L;
private final String tag;
private final T data;
TaggedData(String tag, T data) {
this.tag = tag;
this.data = data;
}
String getTag() {
return tag;
}
T getData() {
return data;
}
}
Imagine, that you pass Integer (which is Serializable) to Predicate<String> (which extends Serializable)? This is senseless.
Predicate#test() serves as a consumer, so you need to use Predicate<? super Serializable> instead.
Read more: What is PECS (Producer Extends Consumer Super)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With