Guest User

Untitled

a guest
Jan 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. @RunWith(JUnit4.class)
  2. public class ExceptionHandlingTests {
  3.  
  4. class ShortCircuitCF<T> {
  5. final CompletableFuture<T> result = new CompletableFuture<>();
  6. final CompletableFuture<T> notCompleted = new CompletableFuture<>();
  7. CompletableFuture<T> propagate(T value) {
  8. result.complete(value);
  9. return notCompleted;
  10. }
  11. CompletableFuture<T> complete(T value) {
  12. return CompletableFuture.completedFuture(value);
  13. }
  14. CompletableFuture<T> getResult() {
  15. return result;
  16. }
  17. }
  18.  
  19. @Test
  20. public void test1() {
  21. for(int shortCutAt: IntStream.range(0, 4).toArray()) {
  22. System.out.println("Example execution with "
  23. + (shortCutAt == 0 ? "no shortcut" : "shortcut at " + shortCutAt));
  24.  
  25. ShortCircuitCF<Integer> result = new ShortCircuitCF<>();
  26. CompletableFuture.completedFuture(null).thenCompose(justVoid -> { // runAsync
  27. System.out.println("Completing result1. Result: " + result.getResult().isDone());
  28. if (shortCutAt == 1) {
  29. return result.propagate(10);
  30. }
  31. return result.complete(null);
  32. }).thenCompose(x -> {
  33. System.out.println("Completing result2. Result: " + result.getResult().isDone());
  34. if (shortCutAt == 2) {
  35. return result.propagate(10);
  36. }
  37. return result.complete(5);
  38. }).thenCompose(x -> {
  39. System.out.println("Completing result3. Result: " + result.getResult().isDone());
  40. if (shortCutAt == 3) {
  41. return result.propagate(10);
  42. }
  43. return result.complete(5);
  44. }).applyToEither(result.getResult(), Function.identity())
  45. .thenAccept(fr -> System.out.println("final result: " + fr));
  46.  
  47. System.out.println();
  48. }
  49. }
  50. }
Add Comment
Please, Sign In to add comment