blob: f1e7cd3cc3f5f43015b6d9aa5d96e4436b6cc7df [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2011, Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package net.floodlightcontroller.storage;
19
20import java.util.ArrayList;
21import java.util.Collection;
22import java.util.List;
23import java.util.concurrent.ExecutorService;
24import java.util.concurrent.Callable;
25import java.util.concurrent.ExecutionException;
26import java.util.concurrent.Future;
27import java.util.concurrent.TimeUnit;
28import java.util.concurrent.TimeoutException;
29
30public class SynchronousExecutorService implements ExecutorService {
31
32 class SynchronousFuture<T> implements Future<T> {
33
34 T result;
35 Exception exc;
36
37 public SynchronousFuture() {
38 }
39
40 public SynchronousFuture(T result) {
41 this.result = result;
42 }
43
44 public SynchronousFuture(Exception exc) {
45 this.exc = exc;
46 }
47
48 @Override
49 public boolean cancel(boolean mayInterruptIfRunning) {
50 return false;
51 }
52
53 @Override
54 public boolean isCancelled() {
55 return false;
56 }
57
58 @Override
59 public boolean isDone() {
60 return true;
61 }
62
63 @Override
64 public T get() throws InterruptedException, ExecutionException {
65 if (exc != null)
66 throw new ExecutionException(exc);
67 return result;
68 }
69
70 @Override
71 public T get(long timeout, TimeUnit unit) throws InterruptedException,
72 ExecutionException, TimeoutException {
73 return get();
74 }
75 }
76
77 @Override
78 public void shutdown() {
79 }
80
81 @Override
82 public List<Runnable> shutdownNow() {
83 return null;
84 }
85
86 @Override
87 public boolean isShutdown() {
88 return false;
89 }
90
91 @Override
92 public boolean isTerminated() {
93 return false;
94 }
95
96 @Override
97 public boolean awaitTermination(long timeout, TimeUnit unit)
98 throws InterruptedException {
99 return false;
100 }
101
102 @Override
103 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
104 throws InterruptedException {
105 List<Future<T>> l = new ArrayList<Future<T>>();
106 for (Callable<T> task : tasks) {
107 Future<T> future = submit(task);
108 l.add(future);
109 }
110 return l;
111 }
112
113 @Override
114 public <T> List<Future<T>> invokeAll(
115 Collection<? extends Callable<T>> tasks, long timeout, TimeUnit units)
116 throws InterruptedException {
117 return invokeAll(tasks);
118 }
119
120 @Override
121 public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
122 throws InterruptedException, ExecutionException {
123 for (Callable<T> task : tasks) {
124 try {
125 task.call();
126 } catch (Exception e) {
127
128 }
129 }
130 throw new ExecutionException(new Exception("no task completed successfully"));
131 }
132
133 @Override
134 public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout,
135 TimeUnit units) throws InterruptedException, ExecutionException,
136 TimeoutException {
137 return invokeAny(tasks);
138 }
139
140 @Override
141 public <T> Future<T> submit(Callable<T> callable) {
142 try {
143 T result = callable.call();
144 return new SynchronousFuture<T>(result);
145 }
146 catch (Exception exc) {
147 return new SynchronousFuture<T>(exc);
148 }
149 }
150
151 @Override
152 public Future<?> submit(Runnable runnable) {
153 try {
154 runnable.run();
155 return new SynchronousFuture<Void>();
156 }
157 catch (Exception exc) {
158 return new SynchronousFuture<Void>(exc);
159 }
160 }
161
162 @Override
163 public <T> Future<T> submit(Runnable runnable, T result) {
164 try {
165 runnable.run();
166 return new SynchronousFuture<T>(result);
167 }
168 catch (Exception exc) {
169 return new SynchronousFuture<T>(exc);
170 }
171 }
172
173 @Override
174 public void execute(Runnable command) {
175 command.run();
176 }
177}