Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. import java.util.concurrent.CompletableFuture;
  2. import java.util.concurrent.ThreadLocalRandom;
  3. import java.util.concurrent.TimeUnit;
  4. import java.util.function.Function;
  5. import java.util.stream.Stream;
  6.  
  7. abstract class Monad<T> {
  8. static <U> Monad<U> unit(U element) { return null; }
  9. abstract <U> Monad<U> bind(Function<T, Monad<U>> f);
  10. }
  11.  
  12. class Wrapper<T> extends Monad<T> {
  13. private T value;
  14. private Wrapper(T element) { this.value = element; }
  15. static <U> Monad<U> unit(U element) { return new Wrapper<>(element); }
  16. public <U> Monad<U> bind(Function<T, Monad<U>> f) { return f.apply(this.value); }
  17. }
  18.  
  19. public class Main {
  20. private static int result;
  21.  
  22. /**
  23. * This method represents a "query" to a database. It waits for 2 seconds
  24. * before returning a randomly generated integer that can then be used
  25. * for further processing
  26. *
  27. * @return Randomly generated integer representing a query from a database
  28. */
  29. private static CompletableFuture<Integer> queryDB() {
  30. return CompletableFuture.supplyAsync(() -> {
  31. System.out.println("Querying DB...");
  32. try {
  33. TimeUnit.SECONDS.sleep(2);
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37.  
  38. int queryValue = ThreadLocalRandom.current().nextInt(0, 100);
  39. System.out.printf("Query complete! Returning %d%n", queryValue);
  40. return queryValue;
  41. });
  42. }
  43.  
  44. /**
  45. * This is a composable function that takes an integer and returns a
  46. * Future where the integer has been modified.In this case, by
  47. * multiplying it by 2 and adding 1. Intended to be used in a
  48. * flatMap or Bind operation
  49. *
  50. * @param i Integer to be modified
  51. * @return Future containing the modified value
  52. */
  53. private static CompletableFuture<Integer> modifyInteger(int i) {
  54. return CompletableFuture.supplyAsync(() -> i * 2 + 1);
  55. }
  56.  
  57. /**
  58. * This Action will execute a read on the result variable at a future
  59. * point in time, and is composable in a chain of other monadic futures
  60. *
  61. * @return Future of Void representing the Action of reading the value
  62. * from the result variable at a future point in time
  63. */
  64. private static CompletableFuture<Void> readResult() {
  65. return CompletableFuture.runAsync(() -> System.out.printf("Read value %d from result%n", result));
  66. }
  67.  
  68.  
  69. /**
  70. * This Action will execute a write on the result variable at a future
  71. * point in time, and is composable in a chain of other monadic futures
  72. *
  73. * @param i Value to write into the result variable
  74. * @return Future of Void representing the Action of writing the value
  75. * passed in to the result variable at a future point in time
  76. */
  77. private static CompletableFuture<Void> writeResult(int i) {
  78. return CompletableFuture.runAsync(() -> {
  79. result = i;
  80. System.out.printf("Wrote value %d to result variable %n", i);
  81. }
  82. );
  83. }
  84.  
  85. public static void main(String[] args) {
  86. CompletableFuture<Void> result = Stream.generate(Main::queryDB)
  87. .limit(5)
  88. .map(future -> future.thenCompose(Main::modifyInteger))
  89. // This is an operation on Streams, rather than on Futures. It
  90. // takes a Stream of Futures of ints, and uses a reduction
  91. // to take it simply to a Future of an int. In this case, it
  92. // simply adds them up
  93. .reduce(CompletableFuture.supplyAsync(() -> 0), (x, y) -> x.thenCombine(y, Integer::sum))
  94. .thenCompose(Main::writeResult)
  95. .thenRun(Main::readResult);
  96.  
  97. // The blocking wait here is due to the fact that this program will
  98. // have nothing left to do after running these commands, so without
  99. // this it would exit before printing the result. If it were a
  100. // long-running process, like a web-server, or had other work to do,
  101. // it would be unnecessary
  102. while (!result.isDone()) {}
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement