Debian Codegraph @

guava-testlib/src/com/google/common/util/concurrent/testing/SameThreadScheduledExecutorService.java

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