|
| 1 | +package org.djava.async; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.Callable; |
| 6 | +import java.util.concurrent.atomic.AtomicInteger; |
| 7 | + |
| 8 | +import org.djava.async.Callbacks.FailureCallBack; |
| 9 | +import org.djava.async.Callbacks.NotificationCallBack; |
| 10 | +import org.djava.async.Callbacks.NotificationEvent; |
| 11 | +import org.djava.async.Callbacks.SuccessCallBack; |
| 12 | +import org.djava.async.util.DeferredContainer; |
| 13 | +import org.djava.async.util.VoidType; |
| 14 | +import org.djava.async.util.DeferredContainer.DeferredTask; |
| 15 | +import org.djava.async.util.DeferredContainer.RunnableDeffered; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unrestricted interface to access the deferred object. The interface should not be |
| 19 | + * supplied to a consumer who should not change the promise's state. Use {@link Promise} for |
| 20 | + * restricted access. |
| 21 | + * |
| 22 | + * <p> |
| 23 | + * Use {@link DeferredFactory} to create a deferred object. |
| 24 | + * </p> |
| 25 | + * |
| 26 | + * @author Prasun Paul |
| 27 | + * |
| 28 | + * @param <R> the resolved value type of the promise |
| 29 | + */ |
| 30 | +@SuppressWarnings("unchecked") |
| 31 | +public abstract class Deferred<R> extends Promise<R> { |
| 32 | + |
| 33 | + public abstract Promise<R> promise(); |
| 34 | + |
| 35 | + public abstract void resolve(R value); |
| 36 | + |
| 37 | + public abstract void reject(Exception ex); |
| 38 | + |
| 39 | + public abstract void notify(NotificationEvent event); |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets the current deferred object. |
| 43 | + * |
| 44 | + * @return the deferred object |
| 45 | + */ |
| 46 | + public Deferred<R> get() { |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * The when method. |
| 52 | + * |
| 53 | + * <p> |
| 54 | + * When all of the promises are done the next pending promise will be |
| 55 | + * resolved. The resolve value will be list of resolve values of the promises. The |
| 56 | + * list order is maintained how the promises appear in when arguments. |
| 57 | + * </p> |
| 58 | + * |
| 59 | + * @param promises the array of promises |
| 60 | + * |
| 61 | + * @return a new <tt>promise</tt> |
| 62 | + */ |
| 63 | + public static <T> Promise<List<T>> when(Promise<T>... promises) { |
| 64 | + final Deferred<List<T>> deferred = DeferredFactory.createDeferred(); |
| 65 | + |
| 66 | + if(promises == null || promises.length == 0) { |
| 67 | + deferred.resolve(null); |
| 68 | + return deferred.promise(); |
| 69 | + } |
| 70 | + |
| 71 | + final T[] result = (T[]) new Object[promises.length]; |
| 72 | + final AtomicInteger completionRemainCount = new AtomicInteger(promises.length); |
| 73 | + |
| 74 | + for(int i = 0; i < promises.length; i++) { |
| 75 | + promises[i].then(new SuccessCallBack<Object, T>(i) { |
| 76 | + @Override |
| 77 | + public Object call(T value) { |
| 78 | + result[index] = value; |
| 79 | + if(completionRemainCount.decrementAndGet() == 0) { |
| 80 | + deferred.resolve(Arrays.asList(result)); |
| 81 | + } |
| 82 | + return VoidType.NOTHING; |
| 83 | + } |
| 84 | + }, new FailureCallBack() { |
| 85 | + @Override |
| 86 | + public VoidType call(Exception reason) { |
| 87 | + deferred.reject(reason); |
| 88 | + return VoidType.NOTHING; |
| 89 | + } |
| 90 | + }, new NotificationCallBack() { |
| 91 | + @Override |
| 92 | + public VoidType call(NotificationEvent event) { |
| 93 | + deferred.notify(event); |
| 94 | + return VoidType.NOTHING; |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + return deferred.promise(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * The when method. A useful helper method when there is only one promise |
| 104 | + * to resolve compare to multiple promise version. |
| 105 | + * |
| 106 | + * @param promise the promise |
| 107 | + * |
| 108 | + * @return a new promise |
| 109 | + */ |
| 110 | + public static <P> Promise<P> when(Promise<P> promise) { |
| 111 | + |
| 112 | + final Deferred<P> deferred = DeferredFactory.createDeferred(); |
| 113 | + promise.then(new SuccessCallBack<P, P>() { |
| 114 | + @Override |
| 115 | + public Object call(P value) { |
| 116 | + deferred.resolve(value); |
| 117 | + return VoidType.NOTHING; |
| 118 | + } |
| 119 | + }, new FailureCallBack() { |
| 120 | + @Override |
| 121 | + public VoidType call(Exception reason) { |
| 122 | + deferred.reject(reason); |
| 123 | + return VoidType.NOTHING; |
| 124 | + } |
| 125 | + }, new NotificationCallBack() { |
| 126 | + @Override |
| 127 | + public VoidType call(NotificationEvent event) { |
| 128 | + deferred.notify(event); |
| 129 | + return VoidType.NOTHING; |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + return deferred.promise(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * When method for a callable. The callable will be converted to a <tt>deferred</tt> |
| 138 | + * task. The task will be submitted to the deferred container for execution. Deferred promise |
| 139 | + * of the task will be returned. |
| 140 | + * |
| 141 | + * @see DeferredTask |
| 142 | + * @see DeferredContainer |
| 143 | + * |
| 144 | + * @param callable the callable |
| 145 | + * |
| 146 | + * @return the promise |
| 147 | + */ |
| 148 | + public static <T> Promise<T> when(Callable<T> callable) { |
| 149 | + RunnableDeffered<T> rp = DeferredFactory.promisify(callable); |
| 150 | + rp.submit(); |
| 151 | + return rp.promise(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * The when method for multiple callables. All of the callables will be converted to its |
| 156 | + * deferred task. When all of the deferred tasks are completed next pending promise will |
| 157 | + * be resolved. |
| 158 | + * |
| 159 | + * @see DeferredTask |
| 160 | + * @see DeferredContainer |
| 161 | + * |
| 162 | + * @param callables the array of callables |
| 163 | + * |
| 164 | + * @return a new promise |
| 165 | + */ |
| 166 | + public static <T> Promise<List<T>> when(Callable<T>... callables) { |
| 167 | + Promise<T>[] promises = (Promise<T>[]) new Promise<?>[callables.length]; |
| 168 | + for(int i = 0; i < callables.length; i++) { |
| 169 | + promises[i] = when(callables[i]); |
| 170 | + } |
| 171 | + return when(promises); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments