refine Continuations.java and add Coroutines.java

This commit is contained in:
Joel Dice 2009-05-16 21:15:41 -06:00
parent 195d95d809
commit 8573619720
2 changed files with 95 additions and 16 deletions

View File

@ -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<Integer>() {
public Integer receive(Callback<Integer> 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<Integer>() {
public Integer receive(Callback<Integer> continuation) {
return 43;
}
}));
public static void main(String[] args) throws Exception {
expect(callWithCurrentContinuation(new CallbackReceiver<Integer>() {
public Integer receive(Callback<Integer> continuation) {
continuation.handleResult(42);
throw new RuntimeException("unreachable");
}
}) == 42);
expect(callWithCurrentContinuation(new CallbackReceiver<Integer>() {
public Integer receive(Callback<Integer> continuation) {
return 43;
}
}) == 43);
try {
callWithCurrentContinuation(new CallbackReceiver<Integer>() {
@ -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();
}

View 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;
}
}