Allow AsynchronousExecutor.getSkipQueue() to pull tasks from the queue

This commit is contained in:
Wesley Wolfe 2014-02-01 17:57:52 -06:00
parent 842962530a
commit 4a6a81e724

View file

@ -24,7 +24,7 @@ import org.apache.commons.lang.Validate;
* @param <T> The type of object you provide. This is created in stage 1, and passed to stage 2, 3, and returned if get() is called. * @param <T> The type of object you provide. This is created in stage 1, and passed to stage 2, 3, and returned if get() is called.
* @param <C> The type of callback you provide. You may register many of these to be passed to the provider in stage 3, one at a time. * @param <C> The type of callback you provide. You may register many of these to be passed to the provider in stage 3, one at a time.
* @param <E> A type of exception you may throw and expect to be handled by the main thread * @param <E> A type of exception you may throw and expect to be handled by the main thread
* @author Wesley Wolfe (c) 2012 * @author Wesley Wolfe (c) 2012, 2014
*/ */
public final class AsynchronousExecutor<P, T, C, E extends Throwable> { public final class AsynchronousExecutor<P, T, C, E extends Throwable> {
@ -208,6 +208,7 @@ public final class AsynchronousExecutor<P, T, C, E extends Throwable> {
/** /**
* Adds a callback to the parameter provided, adding parameter to the queue if needed. * Adds a callback to the parameter provided, adding parameter to the queue if needed.
* <p>
* This should always be synchronous. * This should always be synchronous.
*/ */
public void add(P parameter, C callback) { public void add(P parameter, C callback) {
@ -221,6 +222,7 @@ public final class AsynchronousExecutor<P, T, C, E extends Throwable> {
/** /**
* This method attempts to skip the waiting period for said parameter. * This method attempts to skip the waiting period for said parameter.
* <p>
* This should always be synchronous. * This should always be synchronous.
* @throws IllegalStateException if the parameter is not in the queue anymore, or sometimes if called from asynchronous thread * @throws IllegalStateException if the parameter is not in the queue anymore, or sometimes if called from asynchronous thread
*/ */
@ -236,14 +238,14 @@ public final class AsynchronousExecutor<P, T, C, E extends Throwable> {
* Processes a parameter as if it was in the queue, without ever passing to another thread. * Processes a parameter as if it was in the queue, without ever passing to another thread.
*/ */
public T getSkipQueue(P parameter) throws E { public T getSkipQueue(P parameter) throws E {
return skipQueue(provider, parameter); return skipQueue(parameter);
} }
/** /**
* Processes a parameter as if it was in the queue, without ever passing to another thread. * Processes a parameter as if it was in the queue, without ever passing to another thread.
*/ */
public T getSkipQueue(P parameter, C callback) throws E { public T getSkipQueue(P parameter, C callback) throws E {
final T object = skipQueue(provider, parameter); final T object = skipQueue(parameter);
provider.callStage3(parameter, object, callback); provider.callStage3(parameter, object, callback);
return object; return object;
} }
@ -253,7 +255,7 @@ public final class AsynchronousExecutor<P, T, C, E extends Throwable> {
*/ */
public T getSkipQueue(P parameter, C...callbacks) throws E { public T getSkipQueue(P parameter, C...callbacks) throws E {
final CallBackProvider<P, T, C, E> provider = this.provider; final CallBackProvider<P, T, C, E> provider = this.provider;
final T object = skipQueue(provider, parameter); final T object = skipQueue(parameter);
for (C callback : callbacks) { for (C callback : callbacks) {
provider.callStage3(parameter, object, callback); provider.callStage3(parameter, object, callback);
} }
@ -265,14 +267,18 @@ public final class AsynchronousExecutor<P, T, C, E extends Throwable> {
*/ */
public T getSkipQueue(P parameter, Iterable<C> callbacks) throws E { public T getSkipQueue(P parameter, Iterable<C> callbacks) throws E {
final CallBackProvider<P, T, C, E> provider = this.provider; final CallBackProvider<P, T, C, E> provider = this.provider;
final T object = skipQueue(provider, parameter); final T object = skipQueue(parameter);
for (C callback : callbacks) { for (C callback : callbacks) {
provider.callStage3(parameter, object, callback); provider.callStage3(parameter, object, callback);
} }
return object; return object;
} }
private static <T, P, E extends Throwable> T skipQueue(CallBackProvider<P, T, ?, E> provider, P parameter) throws E { private T skipQueue(P parameter) throws E {
Task task = tasks.get(parameter);
if (task != null) {
return task.get();
}
T object = provider.callStage1(parameter); T object = provider.callStage1(parameter);
provider.callStage2(parameter, object); provider.callStage2(parameter, object);
return object; return object;