Skip to content

Commit 9a7740f

Browse files
committed
some examples added
1 parent 038d19f commit 9a7740f

4 files changed

Lines changed: 250 additions & 0 deletions

File tree

examples/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.djava</groupId>
4+
<artifactId>examples</artifactId>
5+
<version>1.0-SNAPSHOT</version>
6+
<name>examples</name>
7+
<description>Quick examples</description>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.djava</groupId>
12+
<artifactId>deferredjava</artifactId>
13+
<version>0.1-SNAPSHOT</version>
14+
</dependency>
15+
</dependencies>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>3.1</version>
22+
<configuration>
23+
<encoding>UTF-8</encoding>
24+
<source>1.7</source>
25+
<target>1.7</target>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.djava.examples;
2+
3+
import org.djava.async.Callbacks;
4+
import org.djava.async.Callbacks.FailureCallBack;
5+
import org.djava.async.Callbacks.NotificationEvent;
6+
import org.djava.async.Callbacks.SuccessCallBack;
7+
import org.djava.async.Deferred;
8+
import org.djava.async.DeferredFactory;
9+
import org.djava.async.util.VoidType;
10+
11+
public class HelloWorldExample {
12+
13+
public void printHelloWorldAccept() {
14+
Deferred<String> deferred = DeferredFactory.createDeferred();
15+
16+
deferred.then(new SuccessCallBack<VoidType, String>() {
17+
@Override
18+
public Object call(String value) {
19+
System.out.println(value);
20+
return VoidType.NOTHING;
21+
}
22+
});
23+
24+
deferred.resolve("Hello World!!");
25+
}
26+
27+
public void printHelloWorldAReject() {
28+
Deferred<String> deferred = DeferredFactory.createDeferred();
29+
30+
deferred.then(
31+
new SuccessCallBack<VoidType, String>() {
32+
@Override
33+
public Object call(String value) {
34+
System.out.println(value);
35+
return VoidType.NOTHING;
36+
}
37+
},
38+
new FailureCallBack() {
39+
@Override
40+
public VoidType call(Exception reason) {
41+
System.out.println(reason.getMessage());
42+
return VoidType.NOTHING;
43+
}
44+
}, null);
45+
46+
deferred.reject(new RuntimeException("Hello Hell!!"));
47+
}
48+
49+
public void printHelloWorldANotify() {
50+
Deferred<String> deferred = DeferredFactory.createDeferred();
51+
52+
deferred.then(
53+
new Callbacks.SuccessCallBack<VoidType, String>() {
54+
@Override
55+
public Object call(String value) {
56+
System.out.println(value);
57+
return VoidType.NOTHING;
58+
}
59+
},
60+
new Callbacks.FailureCallBack() {
61+
@Override
62+
public VoidType call(Exception reason) {
63+
System.out.println(reason.getMessage());
64+
return VoidType.NOTHING;
65+
}
66+
},
67+
new Callbacks.NotificationCallBack() {
68+
@Override
69+
public VoidType call(NotificationEvent event) {
70+
System.out.println("An update received!!");
71+
return null;
72+
}
73+
});
74+
75+
deferred.notify(new NotificationEvent(deferred.promise()));
76+
}
77+
78+
public static void main(String[] args) {
79+
HelloWorldExample example = new HelloWorldExample();
80+
example.printHelloWorldAccept();
81+
example.printHelloWorldAReject();
82+
example.printHelloWorldANotify();
83+
}
84+
85+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.djava.examples;
2+
3+
import org.djava.async.Callbacks;
4+
import org.djava.async.Callbacks.FailureCallBack;
5+
import org.djava.async.Deferred;
6+
import org.djava.async.DeferredFactory;
7+
import org.djava.async.util.VoidType;
8+
9+
public class ThenChainExample {
10+
11+
public void printHelloWorld(boolean accept) {
12+
Deferred<String> deferred = DeferredFactory.createDeferred();
13+
14+
deferred.then(new Callbacks.SuccessCallBack<String, String>() {
15+
@Override
16+
public Object call(String value) {
17+
return value + " ";
18+
}
19+
})
20+
.then(new Callbacks.SuccessCallBack<String, String>() {
21+
@Override
22+
public Object call(String value) {
23+
return value + "World";
24+
}
25+
})
26+
.then(new Callbacks.SuccessCallBack<String, String>() {
27+
@Override
28+
public Object call(String value) {
29+
return value + "!!";
30+
}
31+
})
32+
.then(new Callbacks.SuccessCallBack<String, String>() {
33+
@Override
34+
public Object call(String value) {
35+
System.out.println(value);
36+
return null;
37+
}
38+
})
39+
.fail(new FailureCallBack() {
40+
@Override
41+
public VoidType call(Exception ex) {
42+
System.out.println(ex.getMessage());
43+
return null;
44+
}
45+
});
46+
47+
if(accept) {
48+
deferred.resolve("Hello");
49+
return;
50+
}
51+
52+
deferred.reject(new RuntimeException("Hello Hell!!"));
53+
}
54+
55+
public static void main(String[] args) {
56+
ThenChainExample example = new ThenChainExample();
57+
example.printHelloWorld(true);
58+
example.printHelloWorld(false);
59+
}
60+
61+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.djava.examples;
2+
3+
import java.util.List;
4+
import java.util.concurrent.Callable;
5+
6+
import org.djava.async.Callbacks;
7+
import org.djava.async.Callbacks.SuccessCallBack;
8+
import org.djava.async.Deferred;
9+
import org.djava.async.util.DeferredContainer;
10+
import org.djava.async.util.VoidType;
11+
12+
public class WhenExample {
13+
14+
@SuppressWarnings("unchecked")
15+
public void whenAllExample(final boolean accept) throws Exception {
16+
17+
Deferred.when(new Callable<String>(){
18+
public String call() throws Exception {
19+
Thread.sleep(100);
20+
return "Hello";
21+
}
22+
}, new Callable<String>(){
23+
public String call() throws Exception {
24+
Thread.sleep(200);
25+
if(accept) {
26+
return " ";
27+
}
28+
29+
throw new RuntimeException("Second call failed");
30+
}
31+
}, new Callable<String>(){
32+
public String call() throws Exception {
33+
return "World!!";
34+
}
35+
}).then(new SuccessCallBack<Object, List<String>>() {
36+
@Override
37+
public Object call(List<String> values) {
38+
String result = "";
39+
for(String value : values) {
40+
result = result + value;
41+
}
42+
System.out.println(result);
43+
return VoidType.NOTHING;
44+
}
45+
}).fail(new Callbacks.FailureCallBack() {
46+
@Override
47+
public VoidType call(Exception reason) {
48+
System.out.println(reason.getMessage());
49+
return VoidType.NOTHING;
50+
}
51+
});
52+
53+
}
54+
55+
public static void main(String[] args) throws Exception {
56+
//first, we should create the container
57+
DeferredContainer.createNewContainer();
58+
59+
WhenExample example = new WhenExample();
60+
example.whenAllExample(true);
61+
62+
//give some time to finish the asynchronous tasks
63+
Thread.sleep(1000);
64+
65+
example.whenAllExample(false);
66+
67+
//give some time to finish the asynchronous tasks
68+
Thread.sleep(1000);
69+
70+
//finally, we should stop the container
71+
DeferredContainer.getContainer().stop();
72+
}
73+
74+
}

0 commit comments

Comments
 (0)