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

net.tascalate.concurrent.PromiseAdapter Maven / Gradle / Ivy

Go to download

Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s

There is a newer version: 0.7.1
Show newest version
/**
 * Original work: copyright 2009-2015 Lukáš Křečan
 * https://github.com/lukas-krecan/completion-stage
 * 
 * This class is based on the work create by Lukáš Křečan 
 * under the Apache License, Version 2.0. Please see 
 * https://github.com/lukas-krecan/completion-stage/blob/completion-stage-0.0.9/src/main/java/net/javacrumbs/completionstage/CompletionStageAdapter.java
 * 
 * Modified work: copyright 2015-2017 Valery Silaev (http://vsilaev.com)
 * 
 * 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 net.tascalate.concurrent;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Helper class to create a concrete {@link Promise} subclass as an
 * implementation from scratch.
 * @author vsilaev
 *
 * @param 
 *   a type of the successfully resolved promise value   
 */
abstract public class PromiseAdapter implements Promise {
    protected static final Executor SAME_THREAD_EXECUTOR = new Executor() {
        public void execute(Runnable command) {
            command.run();
        }

        public String toString() {
            return "SAME_THREAD_EXECUTOR";
        }
    };
    private final Executor defaultExecutor;

    PromiseAdapter(Executor defaultExecutor) {
        this.defaultExecutor = defaultExecutor;
    }

    @Override
    public  Promise thenApply(Function fn) {
        return thenApplyAsync(fn, SAME_THREAD_EXECUTOR);
    }

    @Override
    public  Promise thenApplyAsync(Function fn) {
        return thenApplyAsync(fn, this.defaultExecutor);
    }

    @Override
    public Promise thenAccept(Consumer action) {
        return thenAcceptAsync(action, SAME_THREAD_EXECUTOR);
    }

    @Override
    public Promise thenAcceptAsync(Consumer action) {
        return thenAcceptAsync(action, this.defaultExecutor);
    }

    @Override
    public Promise thenRun(Runnable action) {
        return thenRunAsync(action, SAME_THREAD_EXECUTOR);
    }

    @Override
    public Promise thenRunAsync(Runnable action) {
        return thenRunAsync(action, this.defaultExecutor);
    }

    @Override
    public  Promise thenCombine(CompletionStage other, BiFunction fn) {
        return thenCombineAsync(other, fn, SAME_THREAD_EXECUTOR);
    }

    @Override
    public  Promise thenCombineAsync(CompletionStage other, BiFunction fn) {
        return thenCombineAsync(other, fn, this.defaultExecutor);
    }
    
    @Override
    public  Promise thenAcceptBoth(CompletionStage other, BiConsumer action) {
        return thenAcceptBothAsync(other, action, SAME_THREAD_EXECUTOR);
    }

    @Override
    public  Promise thenAcceptBothAsync(CompletionStage other, BiConsumer action) {
        return thenAcceptBothAsync(other, action, this.defaultExecutor);
    }

    @Override
    public Promise runAfterBoth(CompletionStage other, Runnable action) {
        return runAfterBothAsync(other, action, SAME_THREAD_EXECUTOR);
    }

    @Override
    public Promise runAfterBothAsync(CompletionStage other, Runnable action) {
        return runAfterBothAsync(other, action, this.defaultExecutor);
    }
    
    @Override
    public  Promise applyToEither(CompletionStage other, Function fn) {
        return applyToEitherAsync(other, fn, SAME_THREAD_EXECUTOR);
    }

    @Override
    public  Promise applyToEitherAsync(CompletionStage other, Function fn) {
        return applyToEitherAsync(other, fn, this.defaultExecutor);
    }

    @Override
    public Promise acceptEither(CompletionStage other, Consumer action) {
        return acceptEitherAsync(other, action, SAME_THREAD_EXECUTOR);
    }

    @Override
    public Promise acceptEitherAsync(CompletionStage other, Consumer action) {
        return acceptEitherAsync(other, action, this.defaultExecutor);
    }

    @Override
    public Promise runAfterEither(CompletionStage other, Runnable action) {
        return runAfterEitherAsync(other, action, SAME_THREAD_EXECUTOR);
    }

    @Override
    public Promise runAfterEitherAsync(CompletionStage other, Runnable action) {
        return runAfterEitherAsync(other, action, this.defaultExecutor);
    }
    
    @Override
    public  Promise thenCompose(Function> fn) {
        return thenComposeAsync(fn, SAME_THREAD_EXECUTOR);
    }

    @Override
    public  Promise thenComposeAsync(Function> fn) {
        return thenComposeAsync(fn, this.defaultExecutor);
    }

    @Override
    public Promise whenComplete(BiConsumer action) {
        return whenCompleteAsync(action, SAME_THREAD_EXECUTOR);
    }

    @Override
    public Promise whenCompleteAsync(BiConsumer action) {
        return whenCompleteAsync(action, this.defaultExecutor);
    }

    @Override
    public  Promise handle(BiFunction fn) {
        return handleAsync(fn, SAME_THREAD_EXECUTOR);
    }

    @Override
    public  Promise handleAsync(BiFunction fn) {
        return handleAsync(fn, this.defaultExecutor);
    }
   

    protected final Executor getDefaultExecutor() {
        return this.defaultExecutor;
    }
}