From 857361972006f6fd335d4df10933b470899898e7 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 16 May 2009 21:15:41 -0600 Subject: [PATCH] refine Continuations.java and add Coroutines.java --- test/extra/Continuations.java | 34 ++++++++-------- test/extra/Coroutines.java | 77 +++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 test/extra/Coroutines.java diff --git a/test/extra/Continuations.java b/test/extra/Continuations.java index 2c85ef2758..938ea0328c 100644 --- a/test/extra/Continuations.java +++ b/test/extra/Continuations.java @@ -6,23 +6,23 @@ import avian.CallbackReceiver; import avian.Callback; public class Continuations { - public static void main(String[] args) throws Exception { - System.out.println - ("result: " + - callWithCurrentContinuation(new CallbackReceiver() { - public Integer receive(Callback continuation) { - continuation.handleResult(42); - throw new RuntimeException("unreachable"); - } - })); + private static void expect(boolean v) { + if (! v) throw new RuntimeException(); + } - System.out.println - ("result: " + - callWithCurrentContinuation(new CallbackReceiver() { - public Integer receive(Callback continuation) { - return 43; - } - })); + public static void main(String[] args) throws Exception { + expect(callWithCurrentContinuation(new CallbackReceiver() { + public Integer receive(Callback continuation) { + continuation.handleResult(42); + throw new RuntimeException("unreachable"); + } + }) == 42); + + expect(callWithCurrentContinuation(new CallbackReceiver() { + public Integer receive(Callback continuation) { + return 43; + } + }) == 43); try { callWithCurrentContinuation(new CallbackReceiver() { @@ -31,6 +31,7 @@ public class Continuations { throw new RuntimeException("unreachable"); } }); + throw new RuntimeException("unreachable"); } catch (MyException e) { e.printStackTrace(); } @@ -43,6 +44,7 @@ public class Continuations { throw new MyException(); } }); + throw new RuntimeException("unreachable"); } catch (MyException e) { e.printStackTrace(); } diff --git a/test/extra/Coroutines.java b/test/extra/Coroutines.java new file mode 100644 index 0000000000..9623f37dd7 --- /dev/null +++ b/test/extra/Coroutines.java @@ -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 consumer) throws Exception { + consumer.consume('a'); + consumer.consume('b'); + consumer.consume('c'); + } + + private static void consume(Producer 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 state = new CoroutineState(); + + final Consumer consumer = new Consumer() { + 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 producer = new Producer() { + public Character produce() throws Exception { + return callWithCurrentContinuation(new CallbackReceiver() { + public Character receive(Callback 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 { + public Callback produceNext; + public Callback consumeNext; + } + + private interface Producer { + public T produce() throws Exception; + } + + private interface Consumer { + public void consume(T value) throws Exception; + } +}