View Javadoc
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  
17  package com.google.common.util.concurrent.testing;
18  
19  import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
20  
21  import com.google.common.annotations.GwtIncompatible;
22  import com.google.common.base.Preconditions;
23  import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
24  import com.google.common.util.concurrent.ListenableFuture;
25  import com.google.common.util.concurrent.ListenableScheduledFuture;
26  import com.google.common.util.concurrent.ListeningExecutorService;
27  import com.google.common.util.concurrent.ListeningScheduledExecutorService;
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.concurrent.AbstractExecutorService;
31  import java.util.concurrent.Callable;
32  import java.util.concurrent.Delayed;
33  import java.util.concurrent.ExecutionException;
34  import java.util.concurrent.Future;
35  import java.util.concurrent.TimeUnit;
36  import java.util.concurrent.TimeoutException;
37  
38  /**
39   * A ScheduledExecutorService that executes all scheduled actions immediately
40   * in the calling thread.
41   *
42   * See {@link TestingExecutors#sameThreadScheduledExecutor()} for a full list of
43   * constraints.
44   *
45   * @author John Sirois
46   * @author Zach van Schouwen
47   */
48  @GwtIncompatible
49  class SameThreadScheduledExecutorService extends AbstractExecutorService
50      implements ListeningScheduledExecutorService {
51  
52    private final ListeningExecutorService delegate = newDirectExecutorService();
53  
54    @Override
55    public void shutdown() {
56      delegate.shutdown();
57    }
58  
59    @Override
60    public List<Runnable> shutdownNow() {
61      return delegate.shutdownNow();
62    }
63  
64    @Override
65    public boolean isShutdown() {
66      return delegate.isShutdown();
67    }
68  
69    @Override
70    public boolean isTerminated() {
71      return delegate.isTerminated();
72    }
73  
74    @Override
75    public boolean awaitTermination(long timeout, TimeUnit unit)
76        throws InterruptedException {
77      Preconditions.checkNotNull(unit, "unit must not be null!");
78      return delegate.awaitTermination(timeout, unit);
79    }
80  
81    @Override
82    public <T> ListenableFuture<T> submit(Callable<T> task) {
83      Preconditions.checkNotNull(task, "task must not be null!");
84      return delegate.submit(task);
85    }
86  
87    @Override
88    public <T> ListenableFuture<T> submit(Runnable task, T result) {
89      Preconditions.checkNotNull(task, "task must not be null!");
90      Preconditions.checkNotNull(result, "result must not be null!");
91      return delegate.submit(task, result);
92    }
93  
94    @Override
95    public ListenableFuture<?> submit(Runnable task) {
96      Preconditions.checkNotNull(task, "task must not be null!");
97      return delegate.submit(task);
98    }
99  
100   @Override
101   public <T> List<Future<T>> invokeAll(
102       Collection<? extends Callable<T>> tasks) throws InterruptedException {
103     Preconditions.checkNotNull(tasks, "tasks must not be null!");
104     return delegate.invokeAll(tasks);
105   }
106 
107   @Override
108   public <T> List<Future<T>> invokeAll(
109       Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
110       throws InterruptedException {
111     Preconditions.checkNotNull(tasks, "tasks must not be null!");
112     Preconditions.checkNotNull(unit, "unit must not be null!");
113     return delegate.invokeAll(tasks, timeout, unit);
114   }
115 
116   @Override
117   public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
118       throws InterruptedException, ExecutionException {
119     Preconditions.checkNotNull(tasks, "tasks must not be null!");
120     return delegate.invokeAny(tasks);
121   }
122 
123   @Override
124   public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
125       long timeout, TimeUnit unit)
126       throws InterruptedException, ExecutionException, TimeoutException {
127     Preconditions.checkNotNull(tasks, "tasks must not be null!");
128     Preconditions.checkNotNull(unit, "unit must not be null!");
129     return delegate.invokeAny(tasks, timeout, unit);
130   }
131 
132   @Override
133   public void execute(Runnable command) {
134     Preconditions.checkNotNull(command, "command must not be null!");
135     delegate.execute(command);
136   }
137 
138   @Override
139   public ListenableScheduledFuture<?> schedule(Runnable command, long delay,
140       TimeUnit unit) {
141     Preconditions.checkNotNull(command, "command must not be null");
142     Preconditions.checkNotNull(unit, "unit must not be null!");
143     return schedule(java.util.concurrent.Executors.callable(command),
144         delay, unit);
145   }
146 
147   private static class ImmediateScheduledFuture<V>
148       extends SimpleForwardingListenableFuture<V>
149       implements ListenableScheduledFuture<V> {
150     private ExecutionException exception;
151 
152     protected ImmediateScheduledFuture(ListenableFuture<V> future) {
153       super(future);
154     }
155 
156     @Override
157     public V get(long timeout, TimeUnit unit)
158         throws InterruptedException, ExecutionException, TimeoutException {
159       Preconditions.checkNotNull(unit, "unit must not be null!");
160       return get();
161     }
162 
163     @Override
164     public long getDelay(TimeUnit unit) {
165       Preconditions.checkNotNull(unit, "unit must not be null!");
166       return 0;
167     }
168 
169     @Override
170     public int compareTo(Delayed other) {
171       Preconditions.checkNotNull(other, "other must not be null!");
172       return 0;
173     }
174   }
175 
176   @Override
177   public <V> ListenableScheduledFuture<V> schedule(final Callable<V> callable,
178       long delay, TimeUnit unit) {
179     Preconditions.checkNotNull(callable, "callable must not be null!");
180     Preconditions.checkNotNull(unit, "unit must not be null!");
181     ListenableFuture<V> delegateFuture = submit(callable);
182     return new ImmediateScheduledFuture<V>(delegateFuture);
183   }
184 
185   @Override
186   public ListenableScheduledFuture<?> scheduleAtFixedRate(Runnable command,
187       long initialDelay, long period, TimeUnit unit) {
188     throw new UnsupportedOperationException(
189         "scheduleAtFixedRate is not supported.");
190   }
191 
192   @Override
193   public ListenableScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
194       long initialDelay, long delay, TimeUnit unit) {
195     throw new UnsupportedOperationException(
196         "scheduleWithFixedDelay is not supported.");
197   }
198 }