Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. import com.google.common.base.MoreObjects;
  2. import com.google.common.base.Preconditions;
  3. import com.google.common.util.concurrent.*;
  4.  
  5. import java.util.Optional;
  6. import java.util.concurrent.ExecutionException;
  7. import java.util.concurrent.Executor;
  8. import java.util.concurrent.Future;
  9. import java.util.function.Consumer;
  10. import java.util.function.Function;
  11. import java.util.function.Supplier;
  12.  
  13. @SuppressWarnings("unchecked")
  14. public class FutureHelper {
  15.  
  16. public static <T, V> ListenableFuture<T> getOrCreateAsync(Function<V, ListenableFuture<Optional<T>>> getter, Function<V, ListenableFuture<T>> factory, V dto) {
  17. return getOrCreateAsync(getter, factory, dto, MoreExecutors.directExecutor());
  18. }
  19.  
  20. public static <T, V> ListenableFuture<T> getOrCreateAsync(Function<V, ListenableFuture<Optional<T>>> getter, Function<V, ListenableFuture<T>> factory, V dto, Executor executor) {
  21. ListenableFuture<Optional<T>> future = getter.apply(dto);
  22. AsyncFunction<Optional<T>, T> asyncFunction = optional -> optional.map(Futures::immediateFuture).orElseGet(() -> factory.apply(dto));
  23. return Futures.transformAsync(future, asyncFunction, executor);
  24. }
  25.  
  26.  
  27. public static <T, V> ListenableFuture<T> getOrCreate(Function<V, ListenableFuture<Optional<T>>> getter, Function<V, T> factory, V dto) {
  28. return getOrCreate(getter, factory, dto, MoreExecutors.directExecutor());
  29. }
  30.  
  31. public static <T, V> ListenableFuture<T> getOrCreate(Function<V, ListenableFuture<Optional<T>>> getter, Function<V, T> factory, V dto, Executor executor) {
  32. ListenableFuture<Optional<T>> future = getter.apply(dto);
  33. Function<Optional<T>, T> function = optional -> optional.orElseGet(() -> factory.apply(dto));
  34. return Futures.transform(future, function::apply, executor);
  35. }
  36.  
  37. public static <T, V> ListenableFuture<T> getOrCreateAsync(Supplier<ListenableFuture<Optional<T>>> getter,
  38. Supplier<ListenableFuture<T>> factory) {
  39. return getOrCreateAsync(getter, factory, MoreExecutors.directExecutor());
  40. }
  41.  
  42. public static <T, V> ListenableFuture<T> getOrCreateAsync(Supplier<ListenableFuture<Optional<T>>> getter,
  43. Supplier<ListenableFuture<T>> factory, Executor executor) {
  44. Preconditions.checkNotNull(getter, "Null getter");
  45. Preconditions.checkNotNull(factory, "Null factory");
  46. Preconditions.checkNotNull(executor, "Null executor");
  47.  
  48. ListenableFuture<Optional<T>> future = getter.get();
  49. AsyncFunction<Optional<T>, T> asyncFunction = optional -> optional.map(Futures::immediateFuture).orElseGet(factory);
  50. return Futures.transformAsync(future, asyncFunction, executor);
  51. }
  52.  
  53. public static <V> void addCallback(ListenableFuture<V> future, Consumer<V> onSuccess, Consumer<Throwable> onFailure) {
  54. Preconditions.checkNotNull(future, "future");
  55. Preconditions.checkNotNull(onSuccess, "onSuccess");
  56. Preconditions.checkNotNull(onFailure, "onFailure");
  57. future.addListener(new FunctionalCallbackListener<>(future, onSuccess, onFailure), MoreExecutors.directExecutor());
  58. }
  59.  
  60.  
  61. private static final class FunctionalCallbackListener<V> implements Runnable {
  62. final Future<V> future;
  63. final Consumer<V> onSuccess;
  64. final Consumer<Throwable> onFailure;
  65.  
  66. FunctionalCallbackListener(Future<V> future, Consumer<V> onSuccess, Consumer<Throwable> onFailure) {
  67. this.future = future;
  68. this.onSuccess = onSuccess;
  69. this.onFailure = onFailure;
  70. }
  71.  
  72. @Override
  73. public void run() {
  74. final V value;
  75. try {
  76. value = Futures.getDone(future);
  77. } catch (ExecutionException e) {
  78. onFailure.accept(e.getCause());
  79. return;
  80. } catch (RuntimeException | Error e) {
  81. onFailure.accept(e);
  82. return;
  83. }
  84. onSuccess.accept(value);
  85. }
  86.  
  87. @Override
  88. public String toString() {
  89. return "FunctionalCallbackListener{" +
  90. "future=" + future +
  91. ", onSuccess=" + onSuccess +
  92. ", onFailure=" + onFailure +
  93. '}';
  94. }
  95. }
  96.  
  97. }
Add Comment
Please, Sign In to add comment