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

io.smallrye.mutiny.converters.uni.ToMaybe Maven / Gradle / Ivy

There is a newer version: 1.10.0
Show newest version
package io.smallrye.mutiny.converters.uni;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import io.reactivex.Maybe;
import io.smallrye.mutiny.Uni;

public class ToMaybe implements Function, Maybe> {
    @SuppressWarnings("rawtypes")
    public static final ToMaybe INSTANCE = new ToMaybe();

    private ToMaybe() {
        // Avoid direct instantiation
    }

    @Override
    public Maybe apply(Uni uni) {
        return Maybe.create(emitter -> {
            CompletableFuture future = uni.subscribe().asCompletionStage();
            emitter.setCancellable(() -> future.cancel(false));
            future.whenComplete((res, fail) -> {
                if (future.isCancelled()) {
                    return;
                }

                if (fail != null) {
                    emitter.onError(fail);
                } else if (res != null) {
                    emitter.onSuccess(res);
                    emitter.onComplete();
                } else {
                    emitter.onComplete();
                }

            });
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy