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

Class Class, % Method, % Line, %
ListenableFutureTask 0% (0/1) 0% (0/7) 0% (0/12)


1 /* 2  * Copyright (C) 2008 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 java.lang.Math.min; 18 import static java.util.concurrent.TimeUnit.NANOSECONDS; 19  20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.errorprone.annotations.CanIgnoreReturnValue; 22 import java.util.concurrent.Callable; 23 import java.util.concurrent.ExecutionException; 24 import java.util.concurrent.Executor; 25 import java.util.concurrent.FutureTask; 26 import java.util.concurrent.TimeUnit; 27 import java.util.concurrent.TimeoutException; 28 import org.checkerframework.checker.nullness.qual.Nullable; 29  30 /** 31  * A {@link FutureTask} that also implements the {@link ListenableFuture} interface. Unlike {@code 32  * FutureTask}, {@code ListenableFutureTask} does not provide an overrideable {@link 33  * FutureTask#done() done()} method. For similar functionality, call {@link #addListener}. 34  * 35  * <p>Few users should use this class. It is intended primarily for those who are implementing an 36  * {@code ExecutorService}. Most users should call {@link ListeningExecutorService#submit(Callable) 37  * ListeningExecutorService.submit} on a service obtained from {@link 38  * MoreExecutors#listeningDecorator}. 39  * 40  * @author Sven Mawson 41  * @since 1.0 42  */ 43 @GwtIncompatible 44 @ElementTypesAreNonnullByDefault 45 public class ListenableFutureTask<V extends @Nullable Object> extends FutureTask<V> 46  implements ListenableFuture<V> { 47  // TODO(cpovirk): explore ways of making ListenableFutureTask final. There are some valid reasons 48  // such as BoundedQueueExecutorService to allow extends but it would be nice to make it final to 49  // avoid unintended usage. 50  51  // The execution list to hold our listeners. 52  private final ExecutionList executionList = new ExecutionList(); 53  54  /** 55  * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code 56  * Callable}. 57  * 58  * @param callable the callable task 59  * @since 10.0 60  */ 61  public static <V extends @Nullable Object> ListenableFutureTask<V> create(Callable<V> callable) { 62  return new ListenableFutureTask<V>(callable); 63  } 64  65  /** 66  * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code 67  * Runnable}, and arrange that {@code get} will return the given result on successful completion. 68  * 69  * @param runnable the runnable task 70  * @param result the result to return on successful completion. If you don't need a particular 71  * result, consider using constructions of the form: {@code ListenableFuture<?> f = 72  * ListenableFutureTask.create(runnable, null)} 73  * @since 10.0 74  */ 75  public static <V extends @Nullable Object> ListenableFutureTask<V> create( 76  Runnable runnable, @ParametricNullness V result) { 77  return new ListenableFutureTask<V>(runnable, result); 78  } 79  80  ListenableFutureTask(Callable<V> callable) { 81  super(callable); 82  } 83  84  ListenableFutureTask(Runnable runnable, @ParametricNullness V result) { 85  super(runnable, result); 86  } 87  88  @Override 89  public void addListener(Runnable listener, Executor exec) { 90  executionList.add(listener, exec); 91  } 92  93  @CanIgnoreReturnValue 94  @Override 95  @ParametricNullness 96  public V get(long timeout, TimeUnit unit) 97  throws TimeoutException, InterruptedException, ExecutionException { 98  99  long timeoutNanos = unit.toNanos(timeout); 100  if (timeoutNanos <= OverflowAvoidingLockSupport.MAX_NANOSECONDS_THRESHOLD) { 101  return super.get(timeout, unit); 102  } 103  // Waiting 68 years should be enough for any program. 104  return super.get( 105  min(timeoutNanos, OverflowAvoidingLockSupport.MAX_NANOSECONDS_THRESHOLD), NANOSECONDS); 106  } 107  108  /** Internal implementation detail used to invoke the listeners. */ 109  @Override 110  protected void done() { 111  executionList.execute(); 112  } 113 }