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

com.github.ljtfreitas.julian.reactor.MonoPromise Maven / Gradle / Ivy

/*
 * Copyright (C) 2021 Tiago de Freitas Lima
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.github.ljtfreitas.julian.reactor;

import com.github.ljtfreitas.julian.Attempt;
import com.github.ljtfreitas.julian.Kind;
import com.github.ljtfreitas.julian.Promise;
import com.github.ljtfreitas.julian.Subscriber;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class MonoPromise implements Promise {

    private final Mono mono;

    public MonoPromise(Mono mono) {
        this.mono = mono;
    }

    @Override
    public MonoPromise onSuccess(Consumer fn) {
        return new MonoPromise<>(mono.doOnNext(fn));
    }

    @Override
    public  MonoPromise then(Function fn) {
        return new MonoPromise<>(mono.map(fn));
    }

    @Override
    public  MonoPromise bind(Function> fn) {
        return new MonoPromise<>(mono.flatMap(value -> fn.andThen(that -> that.cast(new Kind>(){})
                        .map(MonoPromise::mono)
                        .orElseGet(() -> Mono.fromCompletionStage(that.future())))
                        .apply(value)));
    }

    @Override
    public  MonoPromise zip(Promise other, BiFunction fn) {
        Mono m2 = other.cast(new Kind>() {})
                .map(MonoPromise::mono)
                .orElseGet(() -> Mono.fromCompletionStage(other.future()));

        return new MonoPromise<>(mono.zipWith(m2).map(t -> fn.apply(t.getT1(), t.getT2())));
    }

    @Override
    public  R fold(Function success, Function failure) {
        return mono.map(success)
                .onErrorResume(e -> Mono.just(failure.apply((Exception) e)))
                .block();
    }

    @Override
    public MonoPromise recover(Function fn) {
        return new MonoPromise<>(mono.onErrorResume(e -> Mono.just(fn.apply((Exception) e))));
    }

    @Override
    public  MonoPromise recover(Class expected, Function fn) {
        return new MonoPromise<>(mono.onErrorResume(expected, e -> Mono.just(fn.apply(e))));
    }

    @Override
    public  MonoPromise failure(Function fn) {
        return new MonoPromise<>(mono.onErrorMap(t -> fn.apply((Exception) t)));
    }

    @Override
    public Promise recover(Predicate p, Function fn) {
        return new MonoPromise<>(mono.onErrorResume(t -> p.test((Exception) t), e -> Mono.just(fn.apply((Exception) e))));
    }

    @Override
    public MonoPromise onFailure(Consumer fn) {
        return new MonoPromise<>(mono.doOnError(t -> fn.accept((Exception) t)));
    }

    @Override
    public Attempt join() {
        return Attempt.run(mono::block);
    }

    @Override
    public CompletableFuture future() {
        return mono.toFuture();
    }

    @Override
    public MonoPromise subscribe(Subscriber subscriber) {
        mono.subscribe(subscriber::success, e -> subscriber.failure((Exception) e), subscriber::done);
        return this;
    }

    public Mono mono() {
        return mono;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy