1/* 2 * Copyright (C) 2009 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.util.concurrent.testing; 18 19import com.google.common.base.Preconditions; 20import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture; 21import com.google.common.util.concurrent.ListenableFuture; 22import com.google.common.util.concurrent.ListenableScheduledFuture; 23import com.google.common.util.concurrent.ListeningExecutorService; 24import com.google.common.util.concurrent.ListeningScheduledExecutorService; 25import com.google.common.util.concurrent.MoreExecutors; 26 27import java.util.Collection; 28import java.util.List; 29import java.util.concurrent.AbstractExecutorService; 30import java.util.concurrent.Callable; 31import java.util.concurrent.Delayed; 32import java.util.concurrent.ExecutionException; 33import java.util.concurrent.Future; 34import java.util.concurrent.TimeUnit; 35import java.util.concurrent.TimeoutException; 36 37/** 38 * A ScheduledExecutorService that executes all scheduled actions immediately 39 * in the calling thread. 40 * 41 * See {@link TestingExecutors#sameThreadScheduledExecutor()} for a full list of 42 * constraints. 43 * 44 * @author John Sirois 45 * @author Zach van Schouwen 46 */ 47class SameThreadScheduledExecutorService extends AbstractExecutorService 48 implements ListeningScheduledExecutorService { 49 50 private final ListeningExecutorService delegate = 51 MoreExecutors.sameThreadExecutor(); 52 53 @Override 54 public void shutdown() { 55 delegate.shutdown(); 56 } 57 58 @Override 59 public List<Runnable> shutdownNow() { 60 return delegate.shutdownNow(); 61 } 62 63 @Override 64 public boolean isShutdown() { 65 return delegate.isShutdown(); 66 } 67 68 @Override 69 public boolean isTerminated() { 70 return delegate.isTerminated(); 71 } 72 73 @Override 74 public boolean awaitTermination(long timeout, TimeUnit unit) 75 throws InterruptedException { 76 Preconditions.checkNotNull(unit, "unit must not be null!"); 77 return delegate.awaitTermination(timeout, unit); 78 } 79 80 @Override 81 public <T> ListenableFuture<T> submit(Callable<T> task) { 82 Preconditions.checkNotNull(task, "task must not be null!"); 83 return delegate.submit(task); 84 } 85 86 @Override 87 public <T> ListenableFuture<T> submit(Runnable task, T result) { 88 Preconditions.checkNotNull(task, "task must not be null!"); 89 Preconditions.checkNotNull(result, "result must not be null!"); 90 return delegate.submit(task, result); 91 } 92 93 @Override 94 public ListenableFuture<?> submit(Runnable task) { 95 Preconditions.checkNotNull(task, "task must not be null!"); 96 return delegate.submit(task); 97 } 98 99 @Override 100 public <T> List<Future<T>> invokeAll( 101 Collection<? extends Callable<T>> tasks) throws InterruptedException { 102 Preconditions.checkNotNull(tasks, "tasks must not be null!"); 103 return delegate.invokeAll(tasks); 104 } 105 106 @Override 107 public <T> List<Future<T>> invokeAll( 108 Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 109 throws InterruptedException { 110 Preconditions.checkNotNull(tasks, "tasks must not be null!"); 111 Preconditions.checkNotNull(unit, "unit must not be null!"); 112 return delegate.invokeAll(tasks, timeout, unit); 113 } 114 115 @Override 116 public <T> T invokeAny(Collection<? extends Callable<T>> tasks) 117 throws InterruptedException, ExecutionException { 118 Preconditions.checkNotNull(tasks, "tasks must not be null!"); 119 return delegate.invokeAny(tasks); 120 } 121 122 @Override 123 public <T> T invokeAny(Collection<? extends Callable<T>> tasks, 124 long timeout, TimeUnit unit) 125 throws InterruptedException, ExecutionException, TimeoutException { 126 Preconditions.checkNotNull(tasks, "tasks must not be null!"); 127 Preconditions.checkNotNull(unit, "unit must not be null!"); 128 return delegate.invokeAny(tasks, timeout, unit); 129 } 130 131 @Override 132 public void execute(Runnable command) { 133 Preconditions.checkNotNull(command, "command must not be null!"); 134 delegate.execute(command); 135 } 136 137 @Override 138 public ListenableScheduledFuture<?> schedule(Runnable command, long delay, 139 TimeUnit unit) { 140 Preconditions.checkNotNull(command, "command must not be null"); 141 Preconditions.checkNotNull(unit, "unit must not be null!"); 142 return schedule(java.util.concurrent.Executors.callable(command), 143 delay, unit); 144 } 145 146 private static class ImmediateScheduledFuture<V> 147 extends SimpleForwardingListenableFuture<V> 148 implements ListenableScheduledFuture<V> { 149 private ExecutionException exception; 150 151 protected ImmediateScheduledFuture(ListenableFuture<V> future) { 152 super(future); 153 } 154 155 @Override 156 public V get(long timeout, TimeUnit unit) 157 throws InterruptedException, ExecutionException, TimeoutException { 158 Preconditions.checkNotNull(unit, "unit must not be null!"); 159 return get(); 160 } 161 162 @Override 163 public long getDelay(TimeUnit unit) { 164 Preconditions.checkNotNull(unit, "unit must not be null!"); 165 return 0; 166 } 167 168 @Override 169 public int compareTo(Delayed other) { 170 Preconditions.checkNotNull(other, "other must not be null!"); 171 return 0; 172 } 173 } 174 175 @Override 176 public <V> ListenableScheduledFuture<V> schedule(final Callable<V> callable, 177 long delay, TimeUnit unit) { 178 Preconditions.checkNotNull(callable, "callable must not be null!"); 179 Preconditions.checkNotNull(unit, "unit must not be null!"); 180 ListenableFuture<V> delegateFuture = submit(callable); 181 return new ImmediateScheduledFuture<V>(delegateFuture); 182 } 183 184 @Override 185 public ListenableScheduledFuture<?> scheduleAtFixedRate(Runnable command, 186 long initialDelay, long period, TimeUnit unit) { 187 throw new UnsupportedOperationException( 188 "scheduleAtFixedRate is not supported."); 189 } 190 191 @Override 192 public ListenableScheduledFuture<?> scheduleWithFixedDelay(Runnable command, 193 long initialDelay, long delay, TimeUnit unit) { 194 throw new UnsupportedOperationException( 195 "scheduleWithFixedDelay is not supported."); 196 } 197} 198