corda/test/extra/Continuations.java
Joel Dice fd778c2c76 remove redundant interfaces and generalize shift/reset generics
Turns out Function can do the jobs of both CallbackReceiver and
FunctionReceiver, so I've removed the latter two.

Also, shift and reset should work with a combination of types, not
just a single type, so I've expanded their generic signatures.
2014-03-21 07:38:29 -06:00

59 lines
1.5 KiB
Java

package extra;
import static avian.Continuations.callWithCurrentContinuation;
import avian.Function;
import avian.Callback;
public class Continuations {
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
public static void main(String[] args) throws Exception {
expect
(callWithCurrentContinuation
(new Function<Callback<Integer>,Integer>() {
public Integer call(Callback<Integer> continuation) {
continuation.handleResult(42);
throw new AssertionError();
}
}) == 42);
expect
(callWithCurrentContinuation
(new Function<Callback<Integer>,Integer>() {
public Integer call(Callback<Integer> continuation) {
return 43;
}
}) == 43);
try {
callWithCurrentContinuation(new Function<Callback<Integer>,Integer>() {
public Integer call(Callback<Integer> continuation) {
continuation.handleException(new MyException());
throw new AssertionError();
}
});
throw new AssertionError();
} catch (MyException e) {
e.printStackTrace();
}
try {
callWithCurrentContinuation(new Function<Callback<Integer>,Integer>() {
public Integer call(Callback<Integer> continuation)
throws MyException
{
throw new MyException();
}
});
throw new AssertionError();
} catch (MyException e) {
e.printStackTrace();
}
}
private static class MyException extends Exception { }
}