mirror of
https://github.com/corda/corda.git
synced 2025-01-07 13:38:47 +00:00
refine Continuations.java and add Coroutines.java
This commit is contained in:
parent
195d95d809
commit
8573619720
@ -6,23 +6,23 @@ import avian.CallbackReceiver;
|
|||||||
import avian.Callback;
|
import avian.Callback;
|
||||||
|
|
||||||
public class Continuations {
|
public class Continuations {
|
||||||
public static void main(String[] args) throws Exception {
|
private static void expect(boolean v) {
|
||||||
System.out.println
|
if (! v) throw new RuntimeException();
|
||||||
("result: " +
|
}
|
||||||
callWithCurrentContinuation(new CallbackReceiver<Integer>() {
|
|
||||||
public Integer receive(Callback<Integer> continuation) {
|
|
||||||
continuation.handleResult(42);
|
|
||||||
throw new RuntimeException("unreachable");
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
System.out.println
|
public static void main(String[] args) throws Exception {
|
||||||
("result: " +
|
expect(callWithCurrentContinuation(new CallbackReceiver<Integer>() {
|
||||||
callWithCurrentContinuation(new CallbackReceiver<Integer>() {
|
public Integer receive(Callback<Integer> continuation) {
|
||||||
public Integer receive(Callback<Integer> continuation) {
|
continuation.handleResult(42);
|
||||||
return 43;
|
throw new RuntimeException("unreachable");
|
||||||
}
|
}
|
||||||
}));
|
}) == 42);
|
||||||
|
|
||||||
|
expect(callWithCurrentContinuation(new CallbackReceiver<Integer>() {
|
||||||
|
public Integer receive(Callback<Integer> continuation) {
|
||||||
|
return 43;
|
||||||
|
}
|
||||||
|
}) == 43);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
callWithCurrentContinuation(new CallbackReceiver<Integer>() {
|
callWithCurrentContinuation(new CallbackReceiver<Integer>() {
|
||||||
@ -31,6 +31,7 @@ public class Continuations {
|
|||||||
throw new RuntimeException("unreachable");
|
throw new RuntimeException("unreachable");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
throw new RuntimeException("unreachable");
|
||||||
} catch (MyException e) {
|
} catch (MyException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -43,6 +44,7 @@ public class Continuations {
|
|||||||
throw new MyException();
|
throw new MyException();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
throw new RuntimeException("unreachable");
|
||||||
} catch (MyException e) {
|
} catch (MyException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
77
test/extra/Coroutines.java
Normal file
77
test/extra/Coroutines.java
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package extra;
|
||||||
|
|
||||||
|
import static avian.Continuations.callWithCurrentContinuation;
|
||||||
|
|
||||||
|
import avian.CallbackReceiver;
|
||||||
|
import avian.Callback;
|
||||||
|
|
||||||
|
public class Coroutines {
|
||||||
|
private static void expect(boolean v) {
|
||||||
|
if (! v) throw new RuntimeException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void produce(Consumer<Character> consumer) throws Exception {
|
||||||
|
consumer.consume('a');
|
||||||
|
consumer.consume('b');
|
||||||
|
consumer.consume('c');
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void consume(Producer<Character> producer) throws Exception {
|
||||||
|
expect(producer.produce() == 'a');
|
||||||
|
expect(producer.produce() == 'b');
|
||||||
|
expect(producer.produce() == 'c');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
final CoroutineState<Character> state = new CoroutineState<Character>();
|
||||||
|
|
||||||
|
final Consumer<Character> consumer = new Consumer<Character>() {
|
||||||
|
public void consume(final Character c) throws Exception {
|
||||||
|
callWithCurrentContinuation(new CallbackReceiver() {
|
||||||
|
public Object receive(Callback continuation) {
|
||||||
|
state.produceNext = continuation;
|
||||||
|
|
||||||
|
state.consumeNext.handleResult(c);
|
||||||
|
|
||||||
|
throw new RuntimeException("unreachable");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Producer<Character> producer = new Producer<Character>() {
|
||||||
|
public Character produce() throws Exception {
|
||||||
|
return callWithCurrentContinuation(new CallbackReceiver<Character>() {
|
||||||
|
public Character receive(Callback<Character> continuation)
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
state.consumeNext = continuation;
|
||||||
|
|
||||||
|
if (state.produceNext == null) {
|
||||||
|
Coroutines.produce(consumer);
|
||||||
|
} else {
|
||||||
|
state.produceNext.handleResult(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException("unreachable");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
consume(producer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CoroutineState<T> {
|
||||||
|
public Callback<T> produceNext;
|
||||||
|
public Callback<T> consumeNext;
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface Producer<T> {
|
||||||
|
public T produce() throws Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface Consumer<T> {
|
||||||
|
public void consume(T value) throws Exception;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user