cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Guava Authors |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | * in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | * the License. |
| 13 | */ |
| 14 | |
| 15 | package com.google.common.util.concurrent; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| 19 | import com.google.common.annotations.GwtCompatible; |
cpovirk | a27bcd2 | 2016-03-14 05:37:02 -0700 | [diff] [blame] | 20 | import com.google.common.util.concurrent.AbstractFuture.TrustedFuture; |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 21 | import java.util.concurrent.ExecutionException; |
| 22 | import java.util.concurrent.Executor; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.logging.Level; |
| 25 | import java.util.logging.Logger; |
cushon | 0a2258e | 2018-05-08 10:48:52 -0700 | [diff] [blame] | 26 | import org.checkerframework.checker.nullness.qual.Nullable; |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 27 | |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 28 | /** Implementation of {@link Futures#immediateFuture}. */ |
cpovirk | df25b6f | 2019-05-19 10:34:00 -0700 | [diff] [blame] | 29 | @GwtCompatible |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 30 | @ElementTypesAreNonnullByDefault |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 31 | // TODO(cpovirk): Make this final (but that may break Mockito spy calls). |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 32 | class ImmediateFuture<V extends @Nullable Object> implements ListenableFuture<V> { |
| 33 | static final ListenableFuture<?> NULL = new ImmediateFuture<@Nullable Object>(null); |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 34 | |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 35 | private static final Logger log = Logger.getLogger(ImmediateFuture.class.getName()); |
| 36 | |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 37 | @ParametricNullness private final V value; |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 38 | |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 39 | ImmediateFuture(@ParametricNullness V value) { |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 40 | this.value = value; |
| 41 | } |
| 42 | |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 43 | @Override |
| 44 | public void addListener(Runnable listener, Executor executor) { |
| 45 | checkNotNull(listener, "Runnable was null."); |
| 46 | checkNotNull(executor, "Executor was null."); |
| 47 | try { |
| 48 | executor.execute(listener); |
| 49 | } catch (RuntimeException e) { |
| 50 | // ListenableFuture's contract is that it will not throw unchecked exceptions, so log the bad |
| 51 | // runnable and/or executor and swallow it. |
| 52 | log.log( |
| 53 | Level.SEVERE, |
| 54 | "RuntimeException while executing runnable " + listener + " with executor " + executor, |
| 55 | e); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 61 | return false; |
| 62 | } |
| 63 | |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 64 | // TODO(lukes): Consider throwing InterruptedException when appropriate. |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 65 | @Override |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 66 | @ParametricNullness |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 67 | public V get() { |
| 68 | return value; |
| 69 | } |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 70 | |
| 71 | @Override |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 72 | @ParametricNullness |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 73 | public V get(long timeout, TimeUnit unit) throws ExecutionException { |
| 74 | checkNotNull(unit); |
| 75 | return get(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean isCancelled() { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean isDone() { |
| 85 | return true; |
| 86 | } |
| 87 | |
cpovirk | fd9a17f | 2019-09-12 04:48:14 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public String toString() { |
| 90 | // Behaviour analogous to AbstractFuture#toString(). |
| 91 | return super.toString() + "[status=SUCCESS, result=[" + value + "]]"; |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 92 | } |
| 93 | |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 94 | static final class ImmediateFailedFuture<V extends @Nullable Object> extends TrustedFuture<V> { |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 95 | ImmediateFailedFuture(Throwable thrown) { |
cpovirk | a27bcd2 | 2016-03-14 05:37:02 -0700 | [diff] [blame] | 96 | setException(thrown); |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
cpovirk | 4b02d3c | 2021-05-05 15:25:30 -0700 | [diff] [blame] | 100 | static final class ImmediateCancelledFuture<V extends @Nullable Object> extends TrustedFuture<V> { |
Google Java Core Libraries | 7256a1d | 2022-02-28 11:49:22 -0800 | [diff] [blame] | 101 | static final @Nullable ImmediateCancelledFuture<Object> INSTANCE = |
| 102 | AbstractFuture.GENERATE_CANCELLATION_CAUSES ? null : new ImmediateCancelledFuture<>(); |
| 103 | |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 104 | ImmediateCancelledFuture() { |
cpovirk | a27bcd2 | 2016-03-14 05:37:02 -0700 | [diff] [blame] | 105 | cancel(false); |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
cpovirk | 1667927 | 2015-09-28 08:14:29 -0700 | [diff] [blame] | 108 | } |