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

io.vertx.rxjava3.impl.AsyncResultMaybe Maven / Gradle / Ivy

package io.vertx.rxjava3.impl;

import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.MaybeObserver;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author Julien Viet
 */
public class AsyncResultMaybe extends Maybe {

  public static  Maybe toMaybe(Consumer>> subscriptionConsumer) {
    return RxJavaPlugins.onAssembly(new AsyncResultMaybe<>(subscriptionConsumer, Function.identity()));
  }

  public static  Maybe toMaybe(Future future, Function mapping) {
    if (future == null) {
      return Maybe.error(new NullPointerException());
    }
    return RxJavaPlugins.onAssembly(new AsyncResultMaybe<>(future::onComplete, mapping));
  }

  public static  Maybe toMaybe(Supplier> future, Function mapping) {
    if (future == null) {
      return Maybe.error(new NullPointerException());
    }
    return RxJavaPlugins.onAssembly(new AsyncResultMaybe<>(h -> future.get().onComplete(h), mapping));
  }

  private final Consumer>> subscriptionConsumer;
  private final Function mapping;

  private AsyncResultMaybe(Consumer>> subscriptionConsumer, Function mapping) {
    this.subscriptionConsumer = subscriptionConsumer;
    this.mapping = mapping;
  }

  @Override
  protected void subscribeActual(MaybeObserver observer) {
    AtomicBoolean disposed = new AtomicBoolean();
    observer.onSubscribe(new Disposable() {
      @Override
      public void dispose() {
        disposed.set(true);
      }
      @Override
      public boolean isDisposed() {
        return disposed.get();
      }
    });
    if (!disposed.get()) {
      try {
        subscriptionConsumer.accept(ar -> {
          if (!disposed.getAndSet(true)) {
            if (ar.succeeded()) {
              try {
                T val = mapping.apply(ar.result());
                if (val != null) {
                  observer.onSuccess(val);
                } else {
                  observer.onComplete();
                }
              } catch (Throwable t) {
                Exceptions.throwIfFatal(t);
                RxJavaPlugins.onError(t);
              }
            } else if (ar.failed()) {
              try {
                observer.onError(ar.cause());
              } catch (Throwable t) {
                Exceptions.throwIfFatal(t);
                RxJavaPlugins.onError(t);
              }
            }
          }
        });
      } catch (Exception e) {
        if (!disposed.getAndSet(true)) {
          try {
            observer.onError(e);
          } catch (Throwable t) {
            Exceptions.throwIfFatal(t);
            RxJavaPlugins.onError(t);
          }
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy