1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (T) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.concurrency.Promise;
13 
14 import hunt.Exceptions;
15 
16 /**
17  * <p>A callback abstraction that handles completed/failed events of asynchronous operations.</p>
18  *
19  * @param <T> the type of the context object
20  *
21  * See_Also:
22  *  https://www.eclipse.org/jetty/javadoc/9.4.7.v20170914/org/eclipse/jetty/util/Promise.html
23  */
24 interface Promise(T) {
25 
26     /**
27      * <p>Callback invoked when the operation completes.</p>
28      *
29      * @param result the context
30      * @see #failed(Throwable)
31      */
32     static if (is(T == void)) {
33         bool succeeded();
34     } else {
35         bool succeeded(T result);
36     }
37     /**
38      * <p>Callback invoked when the operation fails.</p>
39      *
40      * @param x the reason for the operation failure
41      */
42     bool failed(Throwable x);
43 }
44 
45 /**
46  * <p>Empty implementation of {@link Promise}.</p>
47  *
48  * @param (T) the type of the result
49  */
50 class DefaultPromise(T) : Promise!T {
51 
52     static if (is(T == void)) {
53         bool succeeded() {
54             return true;
55         }
56     } else {
57         bool succeeded(T result) {
58             return true;
59         }
60     }
61 
62     bool failed(Throwable x) {
63         return true;
64     }
65 }