net.tascalate.concurrent.PromiseAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of net.tascalate.concurrent Show documentation
Show all versions of net.tascalate.concurrent Show documentation
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage
and related extensions to java.util.concurrent.ExecutorService-s
/**
* 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-2019 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;
protected PromiseAdapter(Executor defaultExecutor) {
this.defaultExecutor = defaultExecutor;
}
@Override
public Promise thenApply(Function super T, ? extends U> fn) {
return thenApplyAsync(fn, SAME_THREAD_EXECUTOR);
}
@Override
public Promise thenApplyAsync(Function super T, ? extends U> fn) {
return thenApplyAsync(fn, this.defaultExecutor);
}
@Override
public Promise thenAccept(Consumer super T> action) {
return thenAcceptAsync(action, SAME_THREAD_EXECUTOR);
}
@Override
public Promise thenAcceptAsync(Consumer super T> 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 extends U> other, BiFunction super T, ? super U, ? extends V> fn) {
return thenCombineAsync(other, fn, SAME_THREAD_EXECUTOR);
}
@Override
public Promise thenCombineAsync(CompletionStage extends U> other, BiFunction super T, ? super U, ? extends V> fn) {
return thenCombineAsync(other, fn, this.defaultExecutor);
}
@Override
public Promise thenAcceptBoth(CompletionStage extends U> other, BiConsumer super T, ? super U> action) {
return thenAcceptBothAsync(other, action, SAME_THREAD_EXECUTOR);
}
@Override
public Promise thenAcceptBothAsync(CompletionStage extends U> other, BiConsumer super T, ? super U> 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 extends T> other, Function super T, U> fn) {
return applyToEitherAsync(other, fn, SAME_THREAD_EXECUTOR);
}
@Override
public Promise applyToEitherAsync(CompletionStage extends T> other, Function super T, U> fn) {
return applyToEitherAsync(other, fn, this.defaultExecutor);
}
@Override
public Promise acceptEither(CompletionStage extends T> other, Consumer super T> action) {
return acceptEitherAsync(other, action, SAME_THREAD_EXECUTOR);
}
@Override
public Promise acceptEitherAsync(CompletionStage extends T> other, Consumer super T> 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 super T, ? extends CompletionStage> fn) {
return thenComposeAsync(fn, SAME_THREAD_EXECUTOR);
}
@Override
public Promise thenComposeAsync(Function super T, ? extends CompletionStage> fn) {
return thenComposeAsync(fn, this.defaultExecutor);
}
@Override
public Promise whenComplete(BiConsumer super T, ? super Throwable> action) {
return whenCompleteAsync(action, SAME_THREAD_EXECUTOR);
}
@Override
public Promise whenCompleteAsync(BiConsumer super T, ? super Throwable> action) {
return whenCompleteAsync(action, this.defaultExecutor);
}
@Override
public Promise handle(BiFunction super T, Throwable, ? extends U> fn) {
return handleAsync(fn, SAME_THREAD_EXECUTOR);
}
@Override
public Promise handleAsync(BiFunction super T, Throwable, ? extends U> fn) {
return handleAsync(fn, this.defaultExecutor);
}
protected final Executor getDefaultExecutor() {
return this.defaultExecutor;
}
}