package com.google.common.util.concurrent;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.GwtCompatible;
import com.google.common.util.concurrent.AbstractFuture.TrustedFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
@GwtCompatible
class ImmediateFuture<V> implements ListenableFuture<V> {
static final ListenableFuture<?> NULL = new ImmediateFuture<>(null);
private static final Logger log = Logger.getLogger(ImmediateFuture.class.getName());
private final @Nullable V value;
ImmediateFuture(@Nullable V value) {
this.value = value;
}
@Override
public void addListener(Runnable listener, Executor executor) {
checkNotNull(listener, "Runnable was null.");
checkNotNull(executor, "Executor was null.");
try {
executor.execute(listener);
} catch (RuntimeException e) {
log.log(
Level.SEVERE,
"RuntimeException while executing runnable " + listener + " with executor " + executor,
e);
}
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public V get() {
return value;
}
@Override
public V get(long timeout, TimeUnit unit) throws ExecutionException {
checkNotNull(unit);
return get();
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return true;
}
@Override
public String toString() {
return super.toString() + "[status=SUCCESS, result=[" + value + "]]";
}
static final class ImmediateFailedFuture<V> extends TrustedFuture<V> {
ImmediateFailedFuture(Throwable thrown) {
setException(thrown);
}
}
static final class ImmediateCancelledFuture<V> extends TrustedFuture<V> {
ImmediateCancelledFuture() {
cancel(false);
}
}
}