You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.dht;
  44. import java.util.concurrent.CountDownLatch;
  45. import java.util.concurrent.TimeUnit;
  46. import java.util.concurrent.TimeoutException;
  47. /**
  48. * Helper to implement a synchronous method in terms of an asynchronous one.
  49. * <p>
  50. * Implementors can use this type to wait for an asynchronous computation to
  51. * complete on a background thread by passing the Sync instance as though it
  52. * were the AsyncCallback:
  53. *
  54. * <pre>
  55. * Sync&lt;T&gt; sync = Sync.create();
  56. * async(..., sync);
  57. * return sync.get(timeout, TimeUnit.MILLISECONDS);
  58. * </pre>
  59. *
  60. * @param <T>
  61. * type of value object.
  62. */
  63. public abstract class Sync<T> implements AsyncCallback<T> {
  64. private static final Sync<?> NONE = new Sync<Object>() {
  65. public void onSuccess(Object result) {
  66. // Discard
  67. }
  68. public void onFailure(DhtException error) {
  69. // Discard
  70. }
  71. @Override
  72. public Object get(long timeout, TimeUnit unit) throws DhtException,
  73. InterruptedException, TimeoutException {
  74. return null;
  75. }
  76. };
  77. /**
  78. * Helper method to create a new sync object.
  79. *
  80. * @param <T>
  81. * type of value object.
  82. * @return a new instance.
  83. */
  84. public static <T> Sync<T> create() {
  85. return new Value<T>();
  86. }
  87. /**
  88. * Singleton callback that ignores onSuccess, onFailure.
  89. *
  90. * @param <T>
  91. * type of value object.
  92. * @return callback that discards all results.
  93. */
  94. @SuppressWarnings("unchecked")
  95. public static <T> Sync<T> none() {
  96. return (Sync<T>) NONE;
  97. }
  98. /**
  99. * Wait for the asynchronous operation to complete.
  100. * <p>
  101. * To prevent application deadlock, waiting can only be performed with the
  102. * supplied timeout.
  103. *
  104. * @param timeout
  105. * amount of time to wait before failing.
  106. * @return the returned value.
  107. * @throws DhtException
  108. * the asynchronous operation failed.
  109. * @throws InterruptedException
  110. * the current thread was interrupted before the operation
  111. * completed.
  112. * @throws TimeoutException
  113. * the timeout elapsed before the operation completed.
  114. */
  115. public T get(Timeout timeout) throws DhtException, InterruptedException,
  116. TimeoutException {
  117. return get(timeout.getTime(), timeout.getUnit());
  118. }
  119. /**
  120. * Wait for the asynchronous operation to complete.
  121. * <p>
  122. * To prevent application deadlock, waiting can only be performed with the
  123. * supplied timeout.
  124. *
  125. * @param timeout
  126. * amount of time to wait before failing.
  127. * @param unit
  128. * units of {@code timeout}. For example
  129. * {@link TimeUnit#MILLISECONDS}.
  130. * @return the returned value.
  131. * @throws DhtException
  132. * the asynchronous operation failed.
  133. * @throws InterruptedException
  134. * the current thread was interrupted before the operation
  135. * completed.
  136. * @throws TimeoutException
  137. * the timeout elapsed before the operation completed.
  138. */
  139. public abstract T get(long timeout, TimeUnit unit) throws DhtException,
  140. InterruptedException, TimeoutException;
  141. private static class Value<T> extends Sync<T> {
  142. private final CountDownLatch wait = new CountDownLatch(1);
  143. private T data;
  144. private DhtException error;
  145. /**
  146. * Wait for the asynchronous operation to complete.
  147. * <p>
  148. * To prevent application deadlock, waiting can only be performed with
  149. * the supplied timeout.
  150. *
  151. * @param timeout
  152. * amount of time to wait before failing.
  153. * @param unit
  154. * units of {@code timeout}. For example
  155. * {@link TimeUnit#MILLISECONDS}.
  156. * @return the returned value.
  157. * @throws DhtException
  158. * the asynchronous operation failed.
  159. * @throws InterruptedException
  160. * the current thread was interrupted before the operation
  161. * completed.
  162. * @throws TimeoutException
  163. * the timeout elapsed before the operation completed.
  164. */
  165. public T get(long timeout, TimeUnit unit) throws DhtException,
  166. InterruptedException, TimeoutException {
  167. if (wait.await(timeout, unit)) {
  168. if (error != null)
  169. throw error;
  170. return data;
  171. }
  172. throw new TimeoutException();
  173. }
  174. public void onSuccess(T obj) {
  175. data = obj;
  176. wait.countDown();
  177. }
  178. public void onFailure(DhtException err) {
  179. error = err;
  180. wait.countDown();
  181. }
  182. }
  183. }