This repository has been archived by the owner on Aug 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
264 lines (206 loc) · 17.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<meta name="description" content="IOU is a Promises/A+ compliant library for JVM with implementations for both vanilla Java and Android, which allows you to write structured asynchronous code" />
<meta name="keywords" content="Java, Android, Promise, IOU, ioweyou, A+, Promises/A+, Deferred, Future" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>IOU - Promises/A+ for Java</title>
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59953914-4', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<section class="page-header">
<h1 class="project-name">IOU</h1>
<h2 class="project-tagline">Promises/A+ for Java</h2>
<a href="https://github.com/ioweyou" class="btn">View on GitHub</a>
</section>
<section class="main-content">
<p>
<a href="https://promisesaplus.com/">
<img src="https://promisesaplus.com/assets/logo-small.png" alt="Promises/A+ logo" title="Promises/A+ 1.0 compliant" align="right" />
</a>
IOU is a <a href="https://github.com/promises-aplus/promises-spec">Promises/A+</a> compliant promise library. It is written in Java, so it can be plugged in to any regular Java, Groovy, Kotlin or Android-project.
</p>
<p><a href="#implementations">Implementations</a></p>
<p><a href="#dependency-management">Dependency management</a></p>
<ul>
<li><a href="#maven">Maven</a></li>
<li><a href="#gradle">Gradle</a></li>
</ul>
<p><a href="#examples">Examples</a></p>
<ul>
<li><a href="#call-with-single-then">Call with single then</a></li>
<li><a href="#call-with-single-then-and-java-8-lambda">Call with single then and Java 8 lambda</a></li>
<li><a href="#chained-or-piped-promise">Chained or piped promise</a></li>
<li><a href="#sequential-promise">Sequential promise</a></li>
<li><a href="#rejecting-a-promise">Rejecting a promise</a></li>
<li><a href="#failing-a-promise">Failing a promise</a></li>
</ul>
<h2>
<a id="implementations" class="anchor" href="#implementations" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Implementations</h2>
<ul>
<li><a href="https://github.com/ioweyou/iou-java">IOU Java</a></li>
<li><a href="https://github.com/ioweyou/iou-android">IOU Android</a></li>
</ul>
<h2>
<a id="dependency-management" class="anchor" href="#dependency-management" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Dependency management</h2>
<h3>
<a id="maven" class="anchor" href="#maven" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Maven</h3>
<hr>
<h4>
<a id="regular" class="anchor" href="#regular" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Regular</h4>
<div class="highlight highlight-text-xml"><pre><<span class="pl-ent">dependency</span>>
<<span class="pl-ent">groupId</span>>nl.brusque.iou</<span class="pl-ent">groupId</span>>
<<span class="pl-ent">artifactId</span>>iou-java</<span class="pl-ent">artifactId</span>>
<<span class="pl-ent">version</span>>1.0.0-beta-01</<span class="pl-ent">version</span>>
</<span class="pl-ent">dependency</span>></pre></div>
<h4>
<a id="android" class="anchor" href="#android" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Android</h4>
<div class="highlight highlight-text-xml"><pre><<span class="pl-ent">dependency</span>>
<<span class="pl-ent">groupId</span>>nl.brusque.iou</<span class="pl-ent">groupId</span>>
<<span class="pl-ent">artifactId</span>>iou-android</<span class="pl-ent">artifactId</span>>
<<span class="pl-ent">version</span>>1.0.0-beta-02</<span class="pl-ent">version</span>>
</<span class="pl-ent">dependency</span>></pre></div>
<h3>
<a id="gradle" class="anchor" href="#gradle" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Gradle</h3>
<hr>
<h4>
<a id="regular-1" class="anchor" href="#regular-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Regular</h4>
<pre><code>compile 'nl.brusque.iou:iou-java:1.0.0-beta-01'
</code></pre>
<h4>
<a id="android-1" class="anchor" href="#android-1" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Android</h4>
<pre><code>compile 'nl.brusque.iou:iou-android:1.0.0-beta-02'
</code></pre>
<h2>
<a id="examples" class="anchor" href="#examples" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Examples</h2>
<hr>
<h3>
<a id="given-the-example-implementation" class="anchor" href="#given-the-example-implementation" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Given the example implementation</h3>
<div class="highlight highlight-source-java"><pre><span class="pl-k">class</span> <span class="pl-en">TestTypedIOU</span><TInput> <span class="pl-k">extends</span> <span class="pl-e">AbstractIOU<<span class="pl-smi">TInput</span>></span> {
<span class="pl-c1">...</span>
}
<span class="pl-k">class</span> <span class="pl-en">TestTypedPromise</span><TInput> <span class="pl-k">extends</span> <span class="pl-e">AbstractPromise<<span class="pl-smi">TInput</span>></span> {
<span class="pl-c1">...</span>
}
<span class="pl-k">TestTypedIOU<<span class="pl-smi">Integer</span>></span> iou <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-k">TestTypedIOU<></span>();
</pre></div>
<h3>
<a id="call-with-single-then" class="anchor" href="#call-with-single-then" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Call with single then</h3>
<div class="highlight highlight-source-java"><pre>iou<span class="pl-k">.</span>getPromise()
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">Void</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Void</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">integer</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(integer);
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
});
iou<span class="pl-k">.</span>resolve(<span class="pl-c1">42</span>); <span class="pl-c">// prints 42</span></pre></div>
<h3>
<a id="call-with-single-then-and-java-8-lambda" class="anchor" href="#call-with-single-then-and-java-8-lambda" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Call with single then and Java 8 lambda</h3>
<div class="highlight highlight-source-java"><pre>iou<span class="pl-k">.</span>getPromise()
.then((<span class="pl-smi">Integer</span> integer) <span class="pl-k">-</span><span class="pl-k">></span> {
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(integer);
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
});
iou<span class="pl-k">.</span>resolve(<span class="pl-c1">42</span>); <span class="pl-c">// prints 42</span></pre></div>
<h3>
<a id="chained-or-piped-promise" class="anchor" href="#chained-or-piped-promise" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Chained or piped promise</h3>
<div class="highlight highlight-source-java"><pre>iou<span class="pl-k">.</span>getPromise()
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">Integer</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Integer</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">input</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-k">return</span> input <span class="pl-k">*</span> <span class="pl-c1">10</span>;
}
})
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">String</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">input</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-k">return</span> <span class="pl-smi">String</span><span class="pl-k">.</span>format(<span class="pl-s"><span class="pl-pds">"</span>The result: %d<span class="pl-pds">"</span></span>, input);
}
})
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">String</span>, <span class="pl-smi">Void</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Void</span> <span class="pl-en">apply</span>(<span class="pl-smi">String</span> <span class="pl-v">input</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(input);
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
});
iou<span class="pl-k">.</span>resolve(<span class="pl-c1">42</span>); <span class="pl-c">// prints "The result: 420"</span></pre></div>
<h3>
<a id="sequential-promise" class="anchor" href="#sequential-promise" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Sequential promise</h3>
<div class="highlight highlight-source-java"><pre><span class="pl-k">TestTypedPromise<<span class="pl-smi">Integer</span>></span> promise <span class="pl-k">=</span> iou<span class="pl-k">.</span>getPromise();
promise
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">Void</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Void</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">input</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(input);
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
});
promise
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">String</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Void</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">input</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-smi">String</span> result <span class="pl-k">=</span> <span class="pl-smi">String</span><span class="pl-k">.</span>format(<span class="pl-s"><span class="pl-pds">"</span>%d * 10 = %d<span class="pl-pds">"</span></span>, input, input <span class="pl-k">*</span> <span class="pl-c1">10</span>);
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(result);
<span class="pl-k">return</span> result;
}
});
iou<span class="pl-k">.</span>resolve(<span class="pl-c1">42</span>); <span class="pl-c">// prints "42" and "42 * 10 = 420" in exactly this order</span></pre></div>
<h3>
<a id="rejecting-a-promise" class="anchor" href="#rejecting-a-promise" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Rejecting a promise</h3>
<div class="highlight highlight-source-java"><pre>iou<span class="pl-k">.</span>getPromise()
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">Integer</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Integer</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">integer</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-k">return</span> integer <span class="pl-k">*</span> <span class="pl-c1">42</span>;
}
}, <span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Object</span>, <span class="pl-smi">Void</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Void</span> <span class="pl-en">apply</span>(<span class="pl-smi">Object</span> <span class="pl-v">reason</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(<span class="pl-smi">String</span><span class="pl-k">.</span>format(<span class="pl-s"><span class="pl-pds">"</span>%s I can't do that.<span class="pl-pds">"</span></span>, reason));
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
});
iou<span class="pl-k">.</span>reject(<span class="pl-s"><span class="pl-pds">"</span>I'm sorry, Dave.<span class="pl-pds">"</span></span>); <span class="pl-c">// prints "I'm sorry, Dave. I can't do that."</span></pre></div>
<h3>
<a id="failing-a-promise" class="anchor" href="#failing-a-promise" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Failing a promise</h3>
<div class="highlight highlight-source-java"><pre>iou<span class="pl-k">.</span>getPromise()
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">Integer</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Integer</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">input</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-smi">Exception</span>(<span class="pl-s"><span class="pl-pds">"</span>I just don't care.<span class="pl-pds">"</span></span>);
}
})
.then(<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Integer</span>, <span class="pl-smi">Void</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Void</span> <span class="pl-en">apply</span>(<span class="pl-smi">Integer</span> <span class="pl-v">input</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(<span class="pl-s"><span class="pl-pds">"</span>What would you say it is you do here?<span class="pl-pds">"</span></span>);
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
} ,<span class="pl-k">new</span> <span class="pl-k">IThenCallable<<span class="pl-smi">Object</span>, <span class="pl-smi">Void</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-smi">Void</span> <span class="pl-en">apply</span>(<span class="pl-smi">Object</span> <span class="pl-v">reason</span>) <span class="pl-k">throws</span> <span class="pl-smi">Exception</span> {
<span class="pl-smi">System</span><span class="pl-k">.</span>out<span class="pl-k">.</span>println(<span class="pl-smi">String</span><span class="pl-k">.</span>format(<span class="pl-s"><span class="pl-pds">"</span>It's not that I'm lazy, it's that %s<span class="pl-pds">"</span></span>, ((<span class="pl-smi">Exception</span>)reason)<span class="pl-k">.</span>getMessage()));
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
});
iou<span class="pl-k">.</span>resolve(<span class="pl-c1">42</span>); <span class="pl-c">// prints "It's not that I'm lazy, it's that I just don't care."</span></pre></div>
<footer class="site-footer">
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span>
</footer>
</section>
</body>
</html>