Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Last active March 23, 2021 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akarnokd/739882c96a9a1a8f4153351fbfe33833 to your computer and use it in GitHub Desktop.
Save akarnokd/739882c96a9a1a8f4153351fbfe33833 to your computer and use it in GitHub Desktop.
static <T, R> Publisher<R> map(Publisher<T> upstream, Function<T, R> mapper) {
return downstream -> upstream.subscribe(new Mapper<>(downstream, mapper));
}
class Mapper<T, R> implements Subscriber<T> {
final Subscriber<? super R> downstream;
final Function<T, R> mapper;
Mapper(Subscriber<? super R> downstream, Function<T, R> mapper) {
this.downstream = downstream;
this.mapper = mapper;
}
@Override
public void onSubscribe(Subscription s) {
downstream.onSubscribe(s);
}
@Override
public void onNext(T item) {
downstream.onNext(mapper.apply(item));
}
@Override
public void onError(Throwable throwable) {
downstream.onError(throwable);
}
@Override
public void onComplete() {
downstream.onComplete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment