Madan Jampani | 7a7ef6d | 2016-01-26 22:01:13 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 7a7ef6d | 2016-01-26 22:01:13 -0800 | [diff] [blame] | 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.atomic.AtomicLong; |
| 19 | import java.util.function.Consumer; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | |
| 24 | /** |
| 25 | * A synchronization utility that defers invocation of a {@link Consumer consumer} |
| 26 | * callback until a set number of actions tracked by a {@code long} counter complete. |
| 27 | * <p> |
| 28 | * Each completion is recorded by invoking the {@link CountDownCompleter#countDown countDown} |
| 29 | * method. When the total number of completions is equal to the preset counter value, |
| 30 | * this instance is marked as completed and the callback invoked by supplying the object |
| 31 | * held by this instance. |
| 32 | * |
| 33 | * @param <T> object type |
| 34 | */ |
| 35 | public final class CountDownCompleter<T> { |
| 36 | |
| 37 | private final T object; |
| 38 | private final Consumer<T> onCompleteCallback; |
| 39 | private final AtomicLong counter; |
| 40 | |
| 41 | /** |
| 42 | * Constructor. |
| 43 | * @param object object |
| 44 | * @param count total number of times countDown must be invoked for this completer to complete |
| 45 | * @param onCompleteCallback callback to invoke when completer is completed |
| 46 | */ |
| 47 | public CountDownCompleter(T object, long count, Consumer<T> onCompleteCallback) { |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 48 | checkState(count >= 0, "count must be non-negative"); |
Madan Jampani | 7a7ef6d | 2016-01-26 22:01:13 -0800 | [diff] [blame] | 49 | this.counter = new AtomicLong(count); |
| 50 | this.object = checkNotNull(object); |
| 51 | this.onCompleteCallback = checkNotNull(onCompleteCallback); |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 52 | if (count == 0) { |
| 53 | onCompleteCallback.accept(object); |
| 54 | } |
Madan Jampani | 7a7ef6d | 2016-01-26 22:01:13 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the object. |
| 59 | * @return object |
| 60 | */ |
| 61 | public T object() { |
| 62 | return object; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Records a single completion. |
| 67 | * <p> |
| 68 | * If this instance has already completed, this method has no effect |
| 69 | */ |
| 70 | public void countDown() { |
| 71 | if (counter.decrementAndGet() == 0) { |
| 72 | onCompleteCallback.accept(object); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns if this instance has completed. |
| 78 | * @return {@code true} if completed |
| 79 | */ |
| 80 | public boolean isComplete() { |
| 81 | return counter.get() <= 0; |
| 82 | } |
| 83 | } |