Merge branch 'master' of ../controller
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / FluentFutures.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util.concurrent;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.Futures;
15 import java.util.concurrent.Future;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18
19 /**
20  * Utility methods for working with {@link FluentFuture}s. This class provides methods which should really live in
21  * Guava's, for example in {@link Futures}, as the implementations provided by {@link Futures#immediateFuture(Object)}
22  * and others already implement {@link FluentFuture} and so getting a FluentFuture is a matter of pure boiler-plate.
23  *
24  * <p>
25  * {@link #immediateBooleanFluentFuture(boolean)}, {@link #immediateBooleanFluentFuture(Boolean)},
26  * {@link #immediateFalseFluentFuture()}, {@link #immediateTrueFluentFuture()} and {@link #immediateNullFluentFuture()}
27  * provide low-cardinality constants, which are generally useful to reduce allocations.
28  *
29  * @author Robert Varga
30  */
31 @Beta
32 @NonNullByDefault
33 @SuppressWarnings("null")
34 public final class FluentFutures {
35     private static final FluentFuture<?> CANCELLED_FUTURE = FluentFuture.from(Futures.immediateCancelledFuture());
36     private static final FluentFuture<?> NULL_FUTURE = FluentFuture.from(Futures.immediateFuture(null));
37     private static final FluentFuture<Boolean> FALSE_FUTURE = FluentFuture.from(Futures.immediateFuture(Boolean.FALSE));
38     private static final FluentFuture<Boolean> TRUE_FUTURE = FluentFuture.from(Futures.immediateFuture(Boolean.TRUE));
39
40     private FluentFutures() {
41
42     }
43
44     /**
45      * Return a {@link FluentFuture} which is immediately {@link Future#cancel(boolean)}led.
46      *
47      * @return An immediately-cancelled FluentFuture.
48      */
49     @SuppressWarnings("unchecked")
50     public static <T> FluentFuture<T> immediateCancelledFluentFuture() {
51         return (FluentFuture<T>) CANCELLED_FUTURE;
52     }
53
54     /**
55      * Return a {@link FluentFuture} which is immediately failed, reporting specified failure {@code cause}.
56      *
57      * @param cause failure cause
58      * @return An immediately-failed FluentFuture.
59      * @throws NullPointerException if {@code cause} is null
60      */
61     public static <T> FluentFuture<T> immediateFailedFluentFuture(final Throwable cause) {
62         return FluentFuture.from(Futures.immediateFailedFuture(requireNonNull(cause)));
63     }
64
65     /**
66      * Return a {@link FluentFuture} which is immediately completed, reporting specified {@code result}.
67      *
68      * @param result result of the future
69      * @return An immediately-completed FluentFuture.
70      * @throws NullPointerException if {@code result} is null
71      */
72     public static <T> FluentFuture<T> immediateFluentFuture(final T result) {
73         return FluentFuture.from(Futures.immediateFuture(requireNonNull(result)));
74     }
75
76     /**
77      * Return a {@link FluentFuture} which is immediately completed with a {@code null} result.
78      *
79      * @return An immediately-completed FluentFuture.
80      */
81     @SuppressWarnings("unchecked")
82     public static <@Nullable T> FluentFuture<T> immediateNullFluentFuture() {
83         return (FluentFuture<T>) NULL_FUTURE;
84     }
85
86     /**
87      * Return a {@link FluentFuture} which is immediately completed, reporting specified {@code result}.
88      *
89      * @param result boolean result
90      * @return An immediately-completed FluentFuture reporting specified {@code result}
91      * @throws NullPointerException if {@code result} is null
92      */
93     public static FluentFuture<Boolean> immediateBooleanFluentFuture(final Boolean result) {
94         return immediateBooleanFluentFuture(result.booleanValue());
95     }
96
97     /**
98      * Return a {@link FluentFuture} which is immediately completed, reporting specified {@code result}.
99      *
100      * @param result boolean result
101      * @return An immediately-completed FluentFuture reporting specified {@code result}
102      */
103     public static FluentFuture<Boolean> immediateBooleanFluentFuture(final boolean result) {
104         return result ? TRUE_FUTURE : FALSE_FUTURE;
105     }
106
107     /**
108      * Return a {@link FluentFuture} which is immediately completed, reporting specified {@link Boolean#TRUE}.
109      *
110      * @return An immediately-completed FluentFuture reporting {@link Boolean#TRUE}
111      */
112     public static FluentFuture<Boolean> immediateTrueFluentFuture() {
113         return TRUE_FUTURE;
114     }
115
116     /**
117      * Return a {@link FluentFuture} which is immediately completed, reporting specified {@link Boolean#TRUE}.
118      *
119      * @return An immediately-completed FluentFuture reporting {@link Boolean#TRUE}
120      */
121     public static FluentFuture<Boolean> immediateFalseFluentFuture() {
122         return FALSE_FUTURE;
123     }
124 }