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.annotations.GwtIncompatible; 21 import com.google.common.util.concurrent.AbstractFuture.TrustedFuture; 22 import java.util.concurrent.ExecutionException; 23 import java.util.concurrent.Executor; 24 import java.util.concurrent.TimeUnit; 25 import java.util.logging.Level; 26 import java.util.logging.Logger; 27 import org.checkerframework.checker.nullness.compatqual.NullableDecl; 28 29 /** Implementations of {@code Futures.immediate*}. */ 30 @GwtCompatible(emulated = true) 31 abstract class ImmediateFuture<V> implements ListenableFuture<V> { 32 private static final Logger log = Logger.getLogger(ImmediateFuture.class.getName()); 33 34 @Override addListener(Runnable listener, Executor executor)35 public void addListener(Runnable listener, Executor executor) { 36 checkNotNull(listener, "Runnable was null."); 37 checkNotNull(executor, "Executor was null."); 38 try { 39 executor.execute(listener); 40 } catch (RuntimeException e) { 41 // ListenableFuture's contract is that it will not throw unchecked exceptions, so log the bad 42 // runnable and/or executor and swallow it. 43 log.log( 44 Level.SEVERE, 45 "RuntimeException while executing runnable " + listener + " with executor " + executor, 46 e); 47 } 48 } 49 50 @Override cancel(boolean mayInterruptIfRunning)51 public boolean cancel(boolean mayInterruptIfRunning) { 52 return false; 53 } 54 55 @Override get()56 public abstract V get() throws ExecutionException; 57 58 @Override get(long timeout, TimeUnit unit)59 public V get(long timeout, TimeUnit unit) throws ExecutionException { 60 checkNotNull(unit); 61 return get(); 62 } 63 64 @Override isCancelled()65 public boolean isCancelled() { 66 return false; 67 } 68 69 @Override isDone()70 public boolean isDone() { 71 return true; 72 } 73 74 static class ImmediateSuccessfulFuture<V> extends ImmediateFuture<V> { 75 static final ImmediateSuccessfulFuture<Object> NULL = new ImmediateSuccessfulFuture<>(null); 76 @NullableDecl private final V value; 77 ImmediateSuccessfulFuture(@ullableDecl V value)78 ImmediateSuccessfulFuture(@NullableDecl V value) { 79 this.value = value; 80 } 81 82 // TODO(lukes): Consider throwing InterruptedException when appropriate. 83 @Override get()84 public V get() { 85 return value; 86 } 87 88 @Override toString()89 public String toString() { 90 // Behaviour analogous to AbstractFuture#toString(). 91 return super.toString() + "[status=SUCCESS, result=[" + value + "]]"; 92 } 93 } 94 95 @GwtIncompatible // TODO 96 static class ImmediateSuccessfulCheckedFuture<V, X extends Exception> extends ImmediateFuture<V> 97 implements CheckedFuture<V, X> { 98 @NullableDecl private final V value; 99 ImmediateSuccessfulCheckedFuture(@ullableDecl V value)100 ImmediateSuccessfulCheckedFuture(@NullableDecl V value) { 101 this.value = value; 102 } 103 104 @Override get()105 public V get() { 106 return value; 107 } 108 109 @Override checkedGet()110 public V checkedGet() { 111 return value; 112 } 113 114 @Override checkedGet(long timeout, TimeUnit unit)115 public V checkedGet(long timeout, TimeUnit unit) { 116 checkNotNull(unit); 117 return value; 118 } 119 120 @Override toString()121 public String toString() { 122 // Behaviour analogous to AbstractFuture#toString(). 123 return super.toString() + "[status=SUCCESS, result=[" + value + "]]"; 124 } 125 } 126 127 static final class ImmediateFailedFuture<V> extends TrustedFuture<V> { ImmediateFailedFuture(Throwable thrown)128 ImmediateFailedFuture(Throwable thrown) { 129 setException(thrown); 130 } 131 } 132 133 static final class ImmediateCancelledFuture<V> extends TrustedFuture<V> { ImmediateCancelledFuture()134 ImmediateCancelledFuture() { 135 cancel(false); 136 } 137 } 138 139 @GwtIncompatible // TODO 140 static class ImmediateFailedCheckedFuture<V, X extends Exception> extends ImmediateFuture<V> 141 implements CheckedFuture<V, X> { 142 private final X thrown; 143 ImmediateFailedCheckedFuture(X thrown)144 ImmediateFailedCheckedFuture(X thrown) { 145 this.thrown = thrown; 146 } 147 148 @Override get()149 public V get() throws ExecutionException { 150 throw new ExecutionException(thrown); 151 } 152 153 @Override checkedGet()154 public V checkedGet() throws X { 155 throw thrown; 156 } 157 158 @Override checkedGet(long timeout, TimeUnit unit)159 public V checkedGet(long timeout, TimeUnit unit) throws X { 160 checkNotNull(unit); 161 throw thrown; 162 } 163 164 @Override toString()165 public String toString() { 166 // Behaviour analogous to AbstractFuture#toString(). 167 return super.toString() + "[status=FAILURE, cause=[" + thrown + "]]"; 168 } 169 } 170 } 171