/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. package com.google.ie.common.openid.appengine;
  16. import com.google.appengine.repackaged.com.google.common.collect.Lists;
  17. import com.google.common.collect.ImmutableList;
  18. import java.util.Collection;
  19. import java.util.List;
  20. import java.util.concurrent.Callable;
  21. import java.util.concurrent.ExecutionException;
  22. import java.util.concurrent.ExecutorService;
  23. import java.util.concurrent.Executors;
  24. import java.util.concurrent.Future;
  25. import java.util.concurrent.TimeUnit;
  26. /**
  27. * Executes Callables in the current thread.
  28. */
  29. public class SerialExecutorService implements ExecutorService {
  30. public boolean awaitTermination(long timeout, TimeUnit unit) {
  31. return false;
  32. }
  33. public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
  34. List<Future<T>> futures = Lists.newArrayListWithCapacity(tasks.size());
  35. for (Callable<T> task : tasks) {
  36. futures.add(submit(task));
  37. }
  38. return futures;
  39. }
  40. public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
  41. long timeout, TimeUnit unit) {
  42. return invokeAll(tasks);
  43. }
  44. public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
  45. throws ExecutionException {
  46. Exception lastException = null;
  47. for (Callable<T> task : tasks) {
  48. try {
  49. T result = task.call();
  50. return result;
  51. } catch (Exception e) {
  52. lastException = e;
  53. continue;
  54. }
  55. }
  56. throw new ExecutionException("none of the tasks completed successfully",
  57. lastException);
  58. }
  59. public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout,
  60. TimeUnit unit) throws ExecutionException {
  61. return invokeAny(tasks);
  62. }
  63. public boolean isShutdown() {
  64. return false;
  65. }
  66. public boolean isTerminated() {
  67. return false;
  68. }
  69. public void shutdown() {
  70. }
  71. public List<Runnable> shutdownNow() {
  72. return ImmutableList.of();
  73. }
  74. public <T> Future<T> submit(Callable<T> task) {
  75. try {
  76. T result = task.call();
  77. return new NoFuture<T>(result);
  78. } catch (Exception e) {
  79. return new NoFuture<T>(new ExecutionException(e));
  80. }
  81. }
  82. public Future<?> submit(Runnable task) {
  83. return submit(Executors.callable(task));
  84. }
  85. public <T> Future<T> submit(Runnable task, T result) {
  86. task.run();
  87. return new NoFuture<T>(result);
  88. }
  89. public void execute(Runnable command) {
  90. command.run();
  91. }
  92. private class NoFuture<T> implements Future<T> {
  93. private final T value;
  94. private final ExecutionException ex;
  95. public NoFuture(T value) {
  96. this.value = value;
  97. this.ex = null;
  98. }
  99. public NoFuture(ExecutionException ex) {
  100. this.value = null;
  101. this.ex = ex;
  102. }
  103. public boolean cancel(boolean mayInterruptIfRunning) {
  104. return false;
  105. }
  106. public T get() throws ExecutionException {
  107. if (ex == null) {
  108. return value;
  109. } else {
  110. throw ex;
  111. }
  112. }
  113. public T get(long timeout, TimeUnit unit) throws ExecutionException {
  114. return get();
  115. }
  116. public boolean isCancelled() {
  117. return false;
  118. }
  119. public boolean isDone() {
  120. return true;
  121. }
  122. }
  123. }