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

com.digitalpetri.netty.fsm.util.CompletionBuilders Maven / Gradle / Ivy

There is a newer version: 1.0.0-RC2
Show newest version
/*
 * Copyright 2018 Kevin Herron
 *
 * 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
 *
 *     http://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 com.digitalpetri.netty.fsm.util;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

public class CompletionBuilders {

    /**
     * Complete {@code future} with the result of the {@link CompletableFuture} that is provided to the returned
     * {@link CompletionBuilder}.
     *
     * @param future the future to complete.
     * @param     the type returned by {@code future}.
     * @return a {@link CompletionBuilder}.
     */
    public static  CompletionBuilder complete(CompletableFuture future) {
        return new CompletionBuilder<>(future);
    }

    /**
     * Complete {@code future} asynchronously with the result of the {@link CompletableFuture} that is provided to
     * the returned {@link CompletionBuilder}.
     *
     * @param future   the future to complete.
     * @param executor the {@link Executor} to use.
     * @param       the type returned by {@code future}.
     * @return a {@link CompletionBuilder}.
     */
    public static  CompletionBuilder completeAsync(CompletableFuture future, Executor executor) {
        return new AsyncCompletionBuilder<>(future, executor);
    }

    public static class CompletionBuilder {

        final CompletableFuture toComplete;

        private CompletionBuilder(CompletableFuture toComplete) {
            this.toComplete = toComplete;
        }

        /**
         * Turn this {@link CompletionBuilder} into an {@link AsyncCompletionBuilder}.
         *
         * @param executor the {@link Executor} to use for the async completion.
         * @return an {@link AsyncCompletionBuilder}.
         */
        public CompletionBuilder async(Executor executor) {
            return new AsyncCompletionBuilder<>(toComplete, executor);
        }

        /**
         * Complete the contained to-be-completed {@link CompletableFuture} using the result of {@code future}.
         *
         * @param future the {@link CompletableFuture} to use as the result for the contained future.
         * @return the original, to-be-completed future provided to this {@link CompletionBuilder}.
         */
        public CompletableFuture with(CompletableFuture future) {
            future.whenComplete((v, ex) -> {
                if (ex != null) toComplete.completeExceptionally(ex);
                else toComplete.complete(v);
            });

            return toComplete;
        }

    }

    private static final class AsyncCompletionBuilder extends CompletionBuilder {

        private final Executor executor;

        AsyncCompletionBuilder(CompletableFuture toComplete, Executor executor) {
            super(toComplete);

            this.executor = executor;
        }

        @Override
        public CompletableFuture with(CompletableFuture future) {
            future.whenCompleteAsync((v, ex) -> {
                if (ex != null) toComplete.completeExceptionally(ex);
                else toComplete.complete(v);
            }, executor);

            return toComplete;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy