blob: 8b1c17ae48bed798f2f3179f57156428bc69d020 [file] [log] [blame]
cpovirk16679272015-09-28 08:14:29 -07001/*
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
15package com.google.common.util.concurrent;
16
17import static com.google.common.base.Preconditions.checkNotNull;
18
19import com.google.common.annotations.GwtCompatible;
cpovirka27bcd22016-03-14 05:37:02 -070020import com.google.common.util.concurrent.AbstractFuture.TrustedFuture;
cpovirk16679272015-09-28 08:14:29 -070021import java.util.concurrent.ExecutionException;
22import java.util.concurrent.Executor;
23import java.util.concurrent.TimeUnit;
24import java.util.logging.Level;
25import java.util.logging.Logger;
cushon0a2258e2018-05-08 10:48:52 -070026import org.checkerframework.checker.nullness.qual.Nullable;
cpovirk16679272015-09-28 08:14:29 -070027
cpovirkfd9a17f2019-09-12 04:48:14 -070028/** Implementation of {@link Futures#immediateFuture}. */
cpovirkdf25b6f2019-05-19 10:34:00 -070029@GwtCompatible
cpovirk4b02d3c2021-05-05 15:25:30 -070030@ElementTypesAreNonnullByDefault
cpovirkfd9a17f2019-09-12 04:48:14 -070031// TODO(cpovirk): Make this final (but that may break Mockito spy calls).
cpovirk4b02d3c2021-05-05 15:25:30 -070032class ImmediateFuture<V extends @Nullable Object> implements ListenableFuture<V> {
33 static final ListenableFuture<?> NULL = new ImmediateFuture<@Nullable Object>(null);
cpovirkfd9a17f2019-09-12 04:48:14 -070034
cpovirk16679272015-09-28 08:14:29 -070035 private static final Logger log = Logger.getLogger(ImmediateFuture.class.getName());
36
cpovirk4b02d3c2021-05-05 15:25:30 -070037 @ParametricNullness private final V value;
cpovirkfd9a17f2019-09-12 04:48:14 -070038
cpovirk4b02d3c2021-05-05 15:25:30 -070039 ImmediateFuture(@ParametricNullness V value) {
cpovirkfd9a17f2019-09-12 04:48:14 -070040 this.value = value;
41 }
42
cpovirk16679272015-09-28 08:14:29 -070043 @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
cpovirkfd9a17f2019-09-12 04:48:14 -070064 // TODO(lukes): Consider throwing InterruptedException when appropriate.
cpovirk16679272015-09-28 08:14:29 -070065 @Override
cpovirk4b02d3c2021-05-05 15:25:30 -070066 @ParametricNullness
cpovirkfd9a17f2019-09-12 04:48:14 -070067 public V get() {
68 return value;
69 }
cpovirk16679272015-09-28 08:14:29 -070070
71 @Override
cpovirk4b02d3c2021-05-05 15:25:30 -070072 @ParametricNullness
cpovirk16679272015-09-28 08:14:29 -070073 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
cpovirkfd9a17f2019-09-12 04:48:14 -070088 @Override
89 public String toString() {
90 // Behaviour analogous to AbstractFuture#toString().
91 return super.toString() + "[status=SUCCESS, result=[" + value + "]]";
cpovirk16679272015-09-28 08:14:29 -070092 }
93
cpovirk4b02d3c2021-05-05 15:25:30 -070094 static final class ImmediateFailedFuture<V extends @Nullable Object> extends TrustedFuture<V> {
cpovirk16679272015-09-28 08:14:29 -070095 ImmediateFailedFuture(Throwable thrown) {
cpovirka27bcd22016-03-14 05:37:02 -070096 setException(thrown);
cpovirk16679272015-09-28 08:14:29 -070097 }
98 }
99
cpovirk4b02d3c2021-05-05 15:25:30 -0700100 static final class ImmediateCancelledFuture<V extends @Nullable Object> extends TrustedFuture<V> {
Google Java Core Libraries7256a1d2022-02-28 11:49:22 -0800101 static final @Nullable ImmediateCancelledFuture<Object> INSTANCE =
102 AbstractFuture.GENERATE_CANCELLATION_CAUSES ? null : new ImmediateCancelledFuture<>();
103
cpovirk16679272015-09-28 08:14:29 -0700104 ImmediateCancelledFuture() {
cpovirka27bcd22016-03-14 05:37:02 -0700105 cancel(false);
cpovirk16679272015-09-28 08:14:29 -0700106 }
107 }
cpovirk16679272015-09-28 08:14:29 -0700108}