|
22 | 22 |
|
23 | 23 | import java.util.concurrent.Callable; |
24 | 24 |
|
| 25 | +import javaslang.CheckedFunction0; |
| 26 | + |
25 | 27 | /** |
26 | 28 | * <h1>async request processing</h1> |
27 | 29 | * A Deferred result, useful for async request processing. Application can produces a result from a |
|
94 | 96 | * error. |
95 | 97 | * </p> |
96 | 98 | * <p> |
97 | | - * Checkout the utility method {@link #resolve(Callable)} and/or {@link #run(Callable)}. Both of |
98 | | - * them catch and handle exceptions for you. |
| 99 | + * Checkout the utility method {@link #resolve(CheckedFunction0)} and/or |
| 100 | + * {@link #run(CheckedFunction0)}. Both of them catch and handle exceptions for you. |
99 | 101 | * </p> |
100 | 102 | * |
101 | 103 | * @author edgar |
@@ -160,8 +162,7 @@ public static interface Handler { |
160 | 162 | * @param initializer An initializer. |
161 | 163 | */ |
162 | 164 | public Deferred(final Initializer0 initializer) { |
163 | | - requireNonNull(initializer, "Initializer is required."); |
164 | | - this.initializer = (req, deferred) -> initializer.run(deferred); |
| 165 | + this((req, deferred) -> initializer.run(deferred)); |
165 | 166 | } |
166 | 167 |
|
167 | 168 | /** |
@@ -220,42 +221,44 @@ public void reject(final Throwable cause) { |
220 | 221 | } |
221 | 222 |
|
222 | 223 | /** |
223 | | - * Produces a {@link Runnable} that runs the given {@link Callable} and {@link #resolve(Callable)} |
224 | | - * or {@link #reject(Throwable)} the deferred. |
| 224 | + * Produces a {@link Runnable} that runs the given {@link Callable} and |
| 225 | + * {@link #resolve(CheckedFunction0)} or {@link #reject(Throwable)} the deferred. |
225 | 226 | * |
226 | 227 | * Please note, the given {@link Callable} runs in the caller thread. |
227 | 228 | * |
228 | 229 | * @param block Callable that produces a result. |
229 | 230 | * @param <T> Resulting type. |
230 | 231 | * @return This deferred as {@link Runnable}. |
231 | 232 | */ |
232 | | - public <T> Runnable run(final Callable<T> block) { |
| 233 | + public <T> Runnable run(final CheckedFunction0<T> block) { |
233 | 234 | return () -> { |
234 | 235 | resolve(block); |
235 | 236 | }; |
236 | 237 | } |
237 | 238 |
|
238 | 239 | /** |
239 | | - * Run the given {@link Callable} and {@link #resolve(Callable)} or {@link #reject(Throwable)} the |
| 240 | + * Run the given {@link Callable} and {@link #resolve(CheckedFunction0)} or |
| 241 | + * {@link #reject(Throwable)} the |
240 | 242 | * deferred. |
241 | 243 | * |
242 | 244 | * Please note, the given {@link Callable} runs in the caller thread. |
243 | 245 | * |
244 | 246 | * @param block Callable that produces a result. |
245 | 247 | * @param <T> Resulting type. |
246 | 248 | */ |
247 | | - public <T> void resolve(final Callable<T> block) { |
| 249 | + public <T> void resolve(final CheckedFunction0<T> block) { |
248 | 250 | try { |
249 | | - resolve(block.call()); |
250 | | - } catch (Exception ex) { |
251 | | - reject(ex); |
| 251 | + resolve(block.apply()); |
| 252 | + } catch (Throwable x) { |
| 253 | + reject(x); |
252 | 254 | } |
253 | 255 | } |
254 | 256 |
|
255 | 257 | /** |
256 | 258 | * Setup a handler for this deferred. Application code should never call this method: INTERNAL USE |
257 | 259 | * ONLY. |
258 | 260 | * |
| 261 | + * @param req Current request. |
259 | 262 | * @param handler A response handler. |
260 | 263 | * @throws Exception If initializer fails to start. |
261 | 264 | */ |
|
0 commit comments