All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.reactivex.core.ExpectationTransformer Maven / Gradle / Ivy

The newest version!
package io.vertx.reactivex.core;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.SingleTransformer;
import io.vertx.core.Expectation;

import java.util.function.Function;

public final class ExpectationTransformer implements SingleTransformer {

  private final Expectation expectation;
  private final Function unwrapper;

  public ExpectationTransformer(Expectation expectation, Function unwrapper) {
    this.expectation = expectation;
    this.unwrapper = unwrapper;
  }

  @Override
  public SingleSource apply(Single upstream) {
    return upstream.flatMap(t -> {
      U u = unwrapper.apply(t);
      if (expectation.test(u)) {
        return Single.just(t);
      } else {
        Throwable desc = expectation.describe(u);
        return Single.error(desc);
      }
    });
  }
}