I have the following static function to log some info before another flux is called.
public static Flux<ServerSentEvent<MyObject>> logBefore(Consumer<ServerSentEvent<MyObject>> consumer) {
return Flux.<ServerSentEvent<MyObject>>empty()
.doOnEach(logOnComplete(consumer));
}
I call the function as follows:
public Flux<ServerSentEvent<MyObject>> myMethod(@Valid @RequestBody MyRequest request) {
return logBefore(r -> log.info("[Enter] myMethod() called."))
.switchIfEmpty(client.retrieveData(request));
}
How to generify this function? I have tried something as follows:
public static <T> Flux<T> logBefore(Consumer<T> consumer) {
return Flux.<T>empty()
.doOnEach(logOnComplete(consumer));
}
However, the compiler is complaining.
Required type:Flux<ServerSentEvent<MyObject>>
Provided:Flux<Object>
Type inference unfortunately does not go deeper than 1 level. That means that type inference tries to get the actual type of T
when calling logBefore(r -> log.info("[Enter] myMethod() called."))
from its argument. That's so generic that no proper type can be inferred, and therefore object is used.
Two ways to get around this:
return ClassWhereLogBeforeIsDefined.<ServerSentEvent<MyObject>>logBefore(r -> log.info("[Enter] myMethod() called."))
return logBefore((ServerSentEvent<MyObject> r) -> log.info("[Enter] myMethod() called."))