PageRenderTime 394ms CodeModel.GetById 201ms app.highlight 11ms RepoModel.GetById 180ms app.codeStats 0ms

/src/main/java/com/google/ie/common/openid/appengine/SerialExecutorService.java

http://thoughtsite.googlecode.com/
Java | 153 lines | 106 code | 30 blank | 17 comment | 5 complexity | 126627bf35160ac1c6123c2b7a39cfea MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * 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
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.common.openid.appengine;
 17
 18import com.google.appengine.repackaged.com.google.common.collect.Lists;
 19import com.google.common.collect.ImmutableList;
 20
 21import java.util.Collection;
 22import java.util.List;
 23import java.util.concurrent.Callable;
 24import java.util.concurrent.ExecutionException;
 25import java.util.concurrent.ExecutorService;
 26import java.util.concurrent.Executors;
 27import java.util.concurrent.Future;
 28import java.util.concurrent.TimeUnit;
 29
 30/**
 31 * Executes Callables in the current thread.
 32 */
 33public class SerialExecutorService implements ExecutorService {
 34
 35    public boolean awaitTermination(long timeout, TimeUnit unit) {
 36        return false;
 37    }
 38
 39    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
 40
 41        List<Future<T>> futures = Lists.newArrayListWithCapacity(tasks.size());
 42        for (Callable<T> task : tasks) {
 43            futures.add(submit(task));
 44        }
 45        return futures;
 46    }
 47
 48    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
 49                    long timeout, TimeUnit unit) {
 50        return invokeAll(tasks);
 51    }
 52
 53    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
 54                    throws ExecutionException {
 55
 56        Exception lastException = null;
 57        for (Callable<T> task : tasks) {
 58            try {
 59                T result = task.call();
 60                return result;
 61            } catch (Exception e) {
 62                lastException = e;
 63                continue;
 64            }
 65        }
 66
 67        throw new ExecutionException("none of the tasks completed successfully",
 68                        lastException);
 69    }
 70
 71    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout,
 72                    TimeUnit unit) throws ExecutionException {
 73        return invokeAny(tasks);
 74    }
 75
 76    public boolean isShutdown() {
 77        return false;
 78    }
 79
 80    public boolean isTerminated() {
 81        return false;
 82    }
 83
 84    public void shutdown() {
 85    }
 86
 87    public List<Runnable> shutdownNow() {
 88        return ImmutableList.of();
 89    }
 90
 91    public <T> Future<T> submit(Callable<T> task) {
 92        try {
 93            T result = task.call();
 94            return new NoFuture<T>(result);
 95        } catch (Exception e) {
 96            return new NoFuture<T>(new ExecutionException(e));
 97        }
 98    }
 99
100    public Future<?> submit(Runnable task) {
101        return submit(Executors.callable(task));
102    }
103
104    public <T> Future<T> submit(Runnable task, T result) {
105        task.run();
106        return new NoFuture<T>(result);
107    }
108
109    public void execute(Runnable command) {
110        command.run();
111    }
112
113    private class NoFuture<T> implements Future<T> {
114
115        private final T value;
116        private final ExecutionException ex;
117
118        public NoFuture(T value) {
119            this.value = value;
120            this.ex = null;
121        }
122
123        public NoFuture(ExecutionException ex) {
124            this.value = null;
125            this.ex = ex;
126        }
127
128        public boolean cancel(boolean mayInterruptIfRunning) {
129            return false;
130        }
131
132        public T get() throws ExecutionException {
133            if (ex == null) {
134                return value;
135            } else {
136                throw ex;
137            }
138        }
139
140        public T get(long timeout, TimeUnit unit) throws ExecutionException {
141            return get();
142        }
143
144        public boolean isCancelled() {
145            return false;
146        }
147
148        public boolean isDone() {
149            return true;
150        }
151    }
152}
153