net.tascalate.concurrent.CallbackRegistry 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/CallbackRegistry.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.LinkedList;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.Consumer;
import java.util.function.Function;
class CallbackRegistry {
private State state = InitialState.instance();
private final Object mutex = new Object();
/**
* Adds the given callbacks to this registry.
*/
void addCallbacks(Consumer super Callable> stageTransition,
Function super T, ? extends U> successCallback,
Function failureCallback,
Executor executor) {
Objects.requireNonNull(successCallback, "'successCallback' must not be null");
Objects.requireNonNull(failureCallback, "'failureCallback' must not be null");
Objects.requireNonNull(executor, "'executor' must not be null");
@SuppressWarnings("unchecked")
Consumer super Callable>> typedTransition = (Consumer super Callable>>)stageTransition;
synchronized (mutex) {
state = state.addCallbacks(typedTransition, successCallback, failureCallback, executor);
}
}
/**
* To be called to set the result value.
*
* @param result
* the result value
* @return true if this result will be used (first result registered)
*/
boolean success(T result) {
State oldState;
synchronized (mutex) {
if (state.isCompleted()) {
return false;
}
oldState = state;
state = state.getSuccessState(result);
}
oldState.callSuccessCallbacks(result);
return true;
}
/**
* To be called to set the failure exception
*
* @param failure
* the exception
* @return true if this result will be used (first result registered)
*/
boolean failure(Throwable failure) {
State oldState;
synchronized (mutex) {
if (state.isCompleted()) {
return false;
}
oldState = state;
state = state.getFailureState(failure);
}
oldState.callFailureCallbacks(failure);
return true;
}
/**
* State of the registry. All subclasses are meant to be used form a
* synchronized block and are NOT thread safe on their own.
*/
private static abstract class State {
protected abstract State addCallbacks(Consumer super Callable>> stageTransition,
Function super S, ?> successCallback,
Function failureCallback,
Executor executor);
protected State getSuccessState(S result) {
throw new IllegalStateException("success method should not be called multiple times");
}
protected void callSuccessCallbacks(S result) {
}
protected State getFailureState(Throwable failure) {
throw new IllegalStateException("failure method should not be called multiple times");
}
protected void callFailureCallbacks(Throwable failure) {
}
protected boolean isCompleted() {
return true;
}
}
/**
* Result is not known yet and no callbacks registered. Using shared
* instance so we do not allocate instance where it may not be needed.
*/
private static class InitialState extends State {
private static final InitialState