net.tascalate.concurrent.CompletablePromise 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
/**
* 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.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Supplier;
/**
* The {@link CompletablePromise} is an adapter of a {@link CompletableFuture} to the {@link Promise} API
*
* @author vsilaev
*
* @param
* a type of the successfully resolved promise value
*/
public class CompletablePromise extends CompletableFutureWrapper {
public CompletablePromise() {
this(new CompletableFuture<>());
}
public CompletablePromise(CompletableFuture delegate) {
super(delegate);
}
protected boolean onSuccess(T value) {
return delegate.complete(value);
}
protected boolean onFailure(Throwable ex) {
return delegate.completeExceptionally(ex);
}
public Promise completeAsync(Supplier extends T> supplier) {
return completeAsync(supplier, ForkJoinPool.commonPool());
}
public Promise completeAsync(Supplier extends T> supplier, Executor executor) {
CompletableTask.supplyAsync(supplier, executor).thenAccept(this::onSuccess);
return this;
}
@Override
protected Promise wrap(CompletionStage original) {
return new CompletablePromise<>((CompletableFuture)original);
}
}