`Future` is an abstraction to deal with asynchronicity without having to use callbacks directly or blocking threads. The primary usage for `Futures` on the JVM is to perform IO operations, which are asynchronous by nature. Although most IO APIs return synchronously, they do that by blocking the cur
| 97 | * @param <T> the type of the asynchronous computation result |
| 98 | */ |
| 99 | public interface Future<T> extends InterruptHandler { |
| 100 | |
| 101 | /** |
| 102 | * A constant void future. Useful to represent completed side effects. |
| 103 | */ |
| 104 | static final Future<Void> VOID = Future.value((Void) null); |
| 105 | |
| 106 | /** |
| 107 | * A constant `false` future. |
| 108 | */ |
| 109 | static final Future<Boolean> FALSE = Future.value(false); |
| 110 | |
| 111 | /** |
| 112 | * A constant `true` future. |
| 113 | */ |
| 114 | static final Future<Boolean> TRUE = Future.value(true); |
| 115 | |
| 116 | /** |
| 117 | * Returns a future that is never satisfied. |
| 118 | * |
| 119 | * @return the unsatisfied future. |
| 120 | * @param <T> the type of the never satisfied future. |
| 121 | */ |
| 122 | public static <T> Future<T> never() { |
| 123 | return FutureConstants.NEVER.unsafeCast(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Creates a future with the result of the supplier. Note that the supplier is |
| 128 | * executed by the current thread. Use FuturePool.async to execute the |
| 129 | * supplier on a separate thread. |
| 130 | * |
| 131 | * @param s a supplier that may throw an exception |
| 132 | * @param <T> the type of the value returned by the Supplier. |
| 133 | * @return a satisfied future if s doesn't throw or else a failed future with |
| 134 | * the supplier exception. |
| 135 | */ |
| 136 | public static <T> Future<T> apply(final Supplier<T> s) { |
| 137 | try { |
| 138 | return new ValueFuture<>(s.get()); |
| 139 | } catch (final Throwable ex) { |
| 140 | return new ExceptionFuture<>(ex); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Applies the provided supplier and flattens the result. This method is useful |
| 146 | * to apply a supplier safely without having to catch possible synchronous exceptions |
| 147 | * that the supplier may throw. |
| 148 | * |
| 149 | * @param s a supplier that may throw an exception |
| 150 | * @return the future produced by the supplier or a failed future if the supplier |
| 151 | * throws a synchronous exception (not a failed Future) |
| 152 | */ |
| 153 | public static <T> Future<T> flatApply(final Supplier<Future<T>> s) { |
| 154 | try { |
| 155 | return s.get(); |
| 156 | } catch (final Throwable ex) { |
no test coverage detected
searching dependent graphs…