Coverage Summary for Class: ImmediateFuture (com.google.common.util.concurrent)

Class Method, % Line, %
ImmediateFuture 0% (0/9) 0% (0/17)
ImmediateFuture$ImmediateCancelledFuture 0% (0/1) 0% (0/2)
ImmediateFuture$ImmediateFailedFuture 0% (0/1) 0% (0/2)
Total 0% (0/11) 0% (0/21)


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; 20 import com.google.common.util.concurrent.AbstractFuture.TrustedFuture; 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; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27  28 /** Implementation of {@link Futures#immediateFuture}. */ 29 @GwtCompatible 30 @ElementTypesAreNonnullByDefault 31 // TODO(cpovirk): Make this final (but that may break Mockito spy calls). 32 class ImmediateFuture<V extends @Nullable Object> implements ListenableFuture<V> { 33  static final ListenableFuture<?> NULL = new ImmediateFuture<@Nullable Object>(null); 34  35  private static final Logger log = Logger.getLogger(ImmediateFuture.class.getName()); 36  37  @ParametricNullness private final V value; 38  39  ImmediateFuture(@ParametricNullness V value) { 40  this.value = value; 41  } 42  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  64  // TODO(lukes): Consider throwing InterruptedException when appropriate. 65  @Override 66  @ParametricNullness 67  public V get() { 68  return value; 69  } 70  71  @Override 72  @ParametricNullness 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  88  @Override 89  public String toString() { 90  // Behaviour analogous to AbstractFuture#toString(). 91  return super.toString() + "[status=SUCCESS, result=[" + value + "]]"; 92  } 93  94  static final class ImmediateFailedFuture<V extends @Nullable Object> extends TrustedFuture<V> { 95  ImmediateFailedFuture(Throwable thrown) { 96  setException(thrown); 97  } 98  } 99  100  static final class ImmediateCancelledFuture<V extends @Nullable Object> extends TrustedFuture<V> { 101  ImmediateCancelledFuture() { 102  cancel(false); 103  } 104  } 105 }