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

io.micronaut.core.execution.CompletableFutureExecutionFlowImpl Maven / Gradle / Ivy

There is a newer version: 4.7.9
Show newest version
/*
 * Copyright 2017-2022 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.core.execution;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.Nullable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * The completable future execution flow implementation.
 *
 * @author Denis Stepanov
 * @since 4.0.0
 */
@Internal
final class CompletableFutureExecutionFlowImpl implements CompletableFutureExecutionFlow {

    private CompletionStage stage;

    CompletableFutureExecutionFlowImpl(CompletionStage stage) {
        this.stage = stage;
    }

    @Override
    public  ExecutionFlow flatMap(Function> transformer) {
        ImperativeExecutionFlow completedFlow = tryComplete();
        if (completedFlow != null) {
            return completedFlow.flatMap(transformer);
        }
        stage = stage.thenCompose(value -> {
            if (value != null) {
                return (CompletionStage) transformer.apply(value).toCompletableFuture();
            }
            return CompletableFuture.completedFuture(null);
        });
        return (ExecutionFlow) this;
    }

    @Override
    public  ExecutionFlow then(Supplier> supplier) {
        stage = stage.thenCompose(value -> (CompletionStage) supplier.get().toCompletableFuture());
        return (ExecutionFlow) this;
    }

    @Override
    public  ExecutionFlow map(Function function) {
        stage = stage.thenApply(function);
        return (ExecutionFlow) this;
    }

    @Override
    public ExecutionFlow onErrorResume(Function> fallback) {
        ImperativeExecutionFlow completedFlow = tryComplete();
        if (completedFlow != null) {
            return completedFlow.onErrorResume(fallback);
        }
        stage = stage.exceptionallyCompose(throwable -> {
            if (throwable instanceof CompletionException completionException) {
                throwable = completionException.getCause();
            }
            return (CompletionStage) fallback.apply(throwable).toCompletableFuture();
        });
        return this;
    }

    @Override
    public ExecutionFlow putInContext(String key, Object value) {
        return this;
    }

    @Override
    public void onComplete(BiConsumer fn) {
        ImperativeExecutionFlow completedFlow = tryComplete();
        if (completedFlow != null) {
            completedFlow.onComplete(fn);
            return;
        }
        stage.handle((o, throwable) -> {
            if (throwable instanceof CompletionException completionException) {
                throwable = completionException.getCause();
            }
            fn.accept(o, throwable);
            return null;
        });
    }

    @Nullable
    @Override
    public ImperativeExecutionFlow tryComplete() {
        CompletableFuture completableFuture = stage.toCompletableFuture();
        if (completableFuture.isDone()) {
            try {
                return new ImperativeExecutionFlowImpl(completableFuture.getNow(null), null);
            } catch (Throwable throwable) {
                if (throwable instanceof CompletionException completionException) {
                    throwable = completionException.getCause();
                }
                return new ImperativeExecutionFlowImpl(null, throwable);
            }
        } else {
            return null;
        }
    }

    @Override
    public CompletableFuture toCompletableFuture() {
        return stage.toCompletableFuture();
    }

}