Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.util; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | import java.util.concurrent.CompletionStage; |
| 20 | import java.util.concurrent.ExecutionException; |
| 21 | import java.util.concurrent.Executor; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | import java.util.concurrent.TimeoutException; |
| 24 | import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | import java.util.function.BiConsumer; |
| 26 | import java.util.function.BiFunction; |
| 27 | import java.util.function.Consumer; |
| 28 | import java.util.function.Function; |
| 29 | |
| 30 | /** |
| 31 | * A {@link CompletableFuture} that tracks whether the future or one of its descendants has been blocked on |
| 32 | * a {@link CompletableFuture#get()} or {@link CompletableFuture#join()} call. |
| 33 | */ |
| 34 | public class BlockingAwareFuture<T> extends CompletableFuture<T> { |
| 35 | private final AtomicBoolean blocked; |
| 36 | |
| 37 | public BlockingAwareFuture() { |
| 38 | this(new AtomicBoolean()); |
| 39 | } |
| 40 | |
| 41 | private BlockingAwareFuture(AtomicBoolean blocked) { |
| 42 | this.blocked = blocked; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns a boolean indicating whether the future is blocked. |
| 47 | * |
| 48 | * @return indicates whether the future is blocked |
| 49 | */ |
| 50 | public boolean isBlocked() { |
| 51 | return blocked.get(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public T get() throws InterruptedException, ExecutionException { |
| 56 | blocked.set(true); |
| 57 | try { |
| 58 | return super.get(); |
| 59 | } finally { |
| 60 | blocked.set(false); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 66 | blocked.set(true); |
| 67 | try { |
| 68 | return super.get(timeout, unit); |
| 69 | } finally { |
| 70 | blocked.set(false); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public synchronized T join() { |
| 76 | blocked.set(true); |
| 77 | try { |
| 78 | return super.join(); |
| 79 | } finally { |
| 80 | blocked.set(false); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Wraps the given future in a new blockable future. |
| 86 | * |
| 87 | * @param future the future to wrap |
| 88 | * @param <U> the future value type |
| 89 | * @return a new blockable future |
| 90 | */ |
| 91 | private <U> CompletableFuture<U> wrap(CompletableFuture<U> future) { |
| 92 | BlockingAwareFuture<U> blockingFuture = new BlockingAwareFuture<U>(blocked); |
| 93 | future.whenComplete((result, error) -> { |
| 94 | if (error == null) { |
| 95 | blockingFuture.complete(result); |
| 96 | } else { |
| 97 | blockingFuture.completeExceptionally(error); |
| 98 | } |
| 99 | }); |
| 100 | return blockingFuture; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn) { |
| 105 | return wrap(super.thenApply(fn)); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn) { |
| 110 | return wrap(super.thenApplyAsync(fn)); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) { |
| 115 | return wrap(super.thenApplyAsync(fn, executor)); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public CompletableFuture<Void> thenAccept(Consumer<? super T> action) { |
| 120 | return wrap(super.thenAccept(action)); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) { |
| 125 | return wrap(super.thenAcceptAsync(action)); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor) { |
| 130 | return wrap(super.thenAcceptAsync(action, executor)); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Void> thenRun(Runnable action) { |
| 135 | return wrap(super.thenRun(action)); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public CompletableFuture<Void> thenRunAsync(Runnable action) { |
| 140 | return wrap(super.thenRunAsync(action)); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor) { |
| 145 | return wrap(super.thenRunAsync(action, executor)); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public <U, V> CompletableFuture<V> thenCombine( |
| 150 | CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) { |
| 151 | return wrap(super.thenCombine(other, fn)); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public <U, V> CompletableFuture<V> thenCombineAsync( |
| 156 | CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) { |
| 157 | return wrap(super.thenCombineAsync(other, fn)); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public <U, V> CompletableFuture<V> thenCombineAsync( |
| 162 | CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor) { |
| 163 | return wrap(super.thenCombineAsync(other, fn, executor)); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public <U> CompletableFuture<Void> thenAcceptBoth( |
| 168 | CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) { |
| 169 | return wrap(super.thenAcceptBoth(other, action)); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public <U> CompletableFuture<Void> thenAcceptBothAsync( |
| 174 | CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) { |
| 175 | return wrap(super.thenAcceptBothAsync(other, action)); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public <U> CompletableFuture<Void> thenAcceptBothAsync( |
| 180 | CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor) { |
| 181 | return wrap(super.thenAcceptBothAsync(other, action, executor)); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action) { |
| 186 | return wrap(super.runAfterBoth(other, action)); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action) { |
| 191 | return wrap(super.runAfterBothAsync(other, action)); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor) { |
| 196 | return wrap(super.runAfterBothAsync(other, action, executor)); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn) { |
| 201 | return wrap(super.applyToEither(other, fn)); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) { |
| 206 | return wrap(super.applyToEitherAsync(other, fn)); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public <U> CompletableFuture<U> applyToEitherAsync( |
| 211 | CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor) { |
| 212 | return wrap(super.applyToEitherAsync(other, fn, executor)); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) { |
| 217 | return wrap(super.acceptEither(other, action)); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) { |
| 222 | return wrap(super.acceptEitherAsync(other, action)); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public CompletableFuture<Void> acceptEitherAsync( |
| 227 | CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) { |
| 228 | return wrap(super.acceptEitherAsync(other, action, executor)); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action) { |
| 233 | return wrap(super.runAfterEither(other, action)); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action) { |
| 238 | return wrap(super.runAfterEitherAsync(other, action)); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor) { |
| 243 | return wrap(super.runAfterEitherAsync(other, action, executor)); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) { |
| 248 | return wrap(super.thenCompose(fn)); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) { |
| 253 | return wrap(super.thenComposeAsync(fn)); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public <U> CompletableFuture<U> thenComposeAsync( |
| 258 | Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) { |
| 259 | return wrap(super.thenComposeAsync(fn, executor)); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action) { |
| 264 | return wrap(super.whenComplete(action)); |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action) { |
| 269 | return wrap(super.whenCompleteAsync(action)); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor) { |
| 274 | return wrap(super.whenCompleteAsync(action, executor)); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) { |
| 279 | return wrap(super.handle(fn)); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) { |
| 284 | return wrap(super.handleAsync(fn)); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) { |
| 289 | return wrap(super.handleAsync(fn, executor)); |
| 290 | } |
| 291 | } |