2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util.concurrent;
10 import static java.util.Objects.requireNonNull;
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;
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.
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.
29 * @author Robert Varga
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));
40 private FluentFutures() {
45 * Return a {@link FluentFuture} which is immediately {@link Future#cancel(boolean)}led.
47 * @return An immediately-cancelled FluentFuture.
49 @SuppressWarnings("unchecked")
50 public static <T> FluentFuture<T> immediateCancelledFluentFuture() {
51 return (FluentFuture<T>) CANCELLED_FUTURE;
55 * Return a {@link FluentFuture} which is immediately failed, reporting specified failure {@code cause}.
57 * @param cause failure cause
58 * @return An immediately-failed FluentFuture.
59 * @throws NullPointerException if {@code cause} is null
61 public static <T> FluentFuture<T> immediateFailedFluentFuture(final Throwable cause) {
62 return FluentFuture.from(Futures.immediateFailedFuture(requireNonNull(cause)));
66 * Return a {@link FluentFuture} which is immediately completed, reporting specified {@code result}.
68 * @param result result of the future
69 * @return An immediately-completed FluentFuture.
70 * @throws NullPointerException if {@code result} is null
72 public static <T> FluentFuture<T> immediateFluentFuture(final T result) {
73 return FluentFuture.from(Futures.immediateFuture(requireNonNull(result)));
77 * Return a {@link FluentFuture} which is immediately completed with a {@code null} result.
79 * @return An immediately-completed FluentFuture.
81 @SuppressWarnings("unchecked")
82 public static <@Nullable T> FluentFuture<T> immediateNullFluentFuture() {
83 return (FluentFuture<T>) NULL_FUTURE;
87 * Return a {@link FluentFuture} which is immediately completed, reporting specified {@code result}.
89 * @param result boolean result
90 * @return An immediately-completed FluentFuture reporting specified {@code result}
91 * @throws NullPointerException if {@code result} is null
93 public static FluentFuture<Boolean> immediateBooleanFluentFuture(final Boolean result) {
94 return immediateBooleanFluentFuture(result.booleanValue());
98 * Return a {@link FluentFuture} which is immediately completed, reporting specified {@code result}.
100 * @param result boolean result
101 * @return An immediately-completed FluentFuture reporting specified {@code result}
103 public static FluentFuture<Boolean> immediateBooleanFluentFuture(final boolean result) {
104 return result ? TRUE_FUTURE : FALSE_FUTURE;
108 * Return a {@link FluentFuture} which is immediately completed, reporting specified {@link Boolean#TRUE}.
110 * @return An immediately-completed FluentFuture reporting {@link Boolean#TRUE}
112 public static FluentFuture<Boolean> immediateTrueFluentFuture() {
117 * Return a {@link FluentFuture} which is immediately completed, reporting specified {@link Boolean#TRUE}.
119 * @return An immediately-completed FluentFuture reporting {@link Boolean#TRUE}
121 public static FluentFuture<Boolean> immediateFalseFluentFuture() {