Coverage Summary for Class: TrustedListenableFutureTask (com.google.common.util.concurrent)
| Class | Method, % | Line, % |
|---|---|---|
| TrustedListenableFutureTask | 75% (6/8) | 80% (20/25) |
| TrustedListenableFutureTask$TrustedFutureInterruptibleAsyncTask | 0% (0/6) | 0% (0/11) |
| TrustedListenableFutureTask$TrustedFutureInterruptibleTask | 100% (6/6) | 100% (10/10) |
| Total | 60% (12/20) | 65.2% (30/46) |
1 /* 2 * Copyright (C) 2014 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.j2objc.annotations.WeakOuter; 21 import java.util.concurrent.Callable; 22 import java.util.concurrent.Executors; 23 import java.util.concurrent.RunnableFuture; 24 import javax.annotation.CheckForNull; 25 import org.checkerframework.checker.nullness.qual.Nullable; 26 27 /** 28 * A {@link RunnableFuture} that also implements the {@link ListenableFuture} interface. 29 * 30 * <p>This should be used in preference to {@link ListenableFutureTask} when possible for 31 * performance reasons. 32 */ 33 @GwtCompatible 34 @ElementTypesAreNonnullByDefault 35 class TrustedListenableFutureTask<V extends @Nullable Object> extends FluentFuture.TrustedFuture<V> 36 implements RunnableFuture<V> { 37 38 static <V extends @Nullable Object> TrustedListenableFutureTask<V> create( 39 AsyncCallable<V> callable) { 40 return new TrustedListenableFutureTask<V>(callable); 41 } 42 43 static <V extends @Nullable Object> TrustedListenableFutureTask<V> create(Callable<V> callable) { 44 return new TrustedListenableFutureTask<V>(callable); 45 } 46 47 /** 48 * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code 49 * Runnable}, and arrange that {@code get} will return the given result on successful completion. 50 * 51 * @param runnable the runnable task 52 * @param result the result to return on successful completion. If you don't need a particular 53 * result, consider using constructions of the form: {@code ListenableFuture<?> f = 54 * ListenableFutureTask.create(runnable, null)} 55 */ 56 static <V extends @Nullable Object> TrustedListenableFutureTask<V> create( 57 Runnable runnable, @ParametricNullness V result) { 58 return new TrustedListenableFutureTask<V>(Executors.callable(runnable, result)); 59 } 60 61 /* 62 * In certain circumstances, this field might theoretically not be visible to an afterDone() call 63 * triggered by cancel(). For details, see the comments on the fields of TimeoutFuture. 64 * 65 * <p>{@code volatile} is required for j2objc transpiling: 66 * https://developers.google.com/j2objc/guides/j2objc-memory-model#atomicity 67 */ 68 @CheckForNull private volatile InterruptibleTask<?> task; 69 70 TrustedListenableFutureTask(Callable<V> callable) { 71 this.task = new TrustedFutureInterruptibleTask(callable); 72 } 73 74 TrustedListenableFutureTask(AsyncCallable<V> callable) { 75 this.task = new TrustedFutureInterruptibleAsyncTask(callable); 76 } 77 78 @Override 79 public void run() { 80 InterruptibleTask<?> localTask = task; 81 if (localTask != null) { 82 localTask.run(); 83 } 84 /* 85 * In the Async case, we may have called setFuture(pendingFuture), in which case afterDone() 86 * won't have been called yet. 87 */ 88 this.task = null; 89 } 90 91 @Override 92 protected void afterDone() { 93 super.afterDone(); 94 95 if (wasInterrupted()) { 96 InterruptibleTask<?> localTask = task; 97 if (localTask != null) { 98 localTask.interruptTask(); 99 } 100 } 101 102 this.task = null; 103 } 104 105 @Override 106 @CheckForNull 107 protected String pendingToString() { 108 InterruptibleTask<?> localTask = task; 109 if (localTask != null) { 110 return "task=[" + localTask + "]"; 111 } 112 return super.pendingToString(); 113 } 114 115 @WeakOuter 116 private final class TrustedFutureInterruptibleTask extends InterruptibleTask<V> { 117 private final Callable<V> callable; 118 119 TrustedFutureInterruptibleTask(Callable<V> callable) { 120 this.callable = checkNotNull(callable); 121 } 122 123 @Override 124 final boolean isDone() { 125 return TrustedListenableFutureTask.this.isDone(); 126 } 127 128 @Override 129 @ParametricNullness 130 V runInterruptibly() throws Exception { 131 return callable.call(); 132 } 133 134 @Override 135 void afterRanInterruptiblySuccess(@ParametricNullness V result) { 136 TrustedListenableFutureTask.this.set(result); 137 } 138 139 @Override 140 void afterRanInterruptiblyFailure(Throwable error) { 141 setException(error); 142 } 143 144 @Override 145 String toPendingString() { 146 return callable.toString(); 147 } 148 } 149 150 @WeakOuter 151 private final class TrustedFutureInterruptibleAsyncTask 152 extends InterruptibleTask<ListenableFuture<V>> { 153 private final AsyncCallable<V> callable; 154 155 TrustedFutureInterruptibleAsyncTask(AsyncCallable<V> callable) { 156 this.callable = checkNotNull(callable); 157 } 158 159 @Override 160 final boolean isDone() { 161 return TrustedListenableFutureTask.this.isDone(); 162 } 163 164 @Override 165 ListenableFuture<V> runInterruptibly() throws Exception { 166 return checkNotNull( 167 callable.call(), 168 "AsyncCallable.call returned null instead of a Future. " 169 + "Did you mean to return immediateFuture(null)? %s", 170 callable); 171 } 172 173 @Override 174 void afterRanInterruptiblySuccess(ListenableFuture<V> result) { 175 setFuture(result); 176 } 177 178 @Override 179 void afterRanInterruptiblyFailure(Throwable error) { 180 setException(error); 181 } 182 183 @Override 184 String toPendingString() { 185 return callable.toString(); 186 } 187 } 188 }