|
22 | 22 | import java.util.Arrays;
|
23 | 23 | import java.util.List;
|
24 | 24 | import java.util.Map;
|
| 25 | +import java.util.concurrent.CountDownLatch; |
25 | 26 | import java.util.concurrent.Future;
|
26 | 27 | import java.util.concurrent.TimeUnit;
|
27 | 28 | import java.util.concurrent.TimeoutException;
|
| 29 | +import java.util.concurrent.atomic.AtomicReference; |
28 | 30 |
|
29 | 31 | import org.junit.Before;
|
30 | 32 | import org.junit.Test;
|
@@ -332,6 +334,84 @@ public void onNext(T args) {
|
332 | 334 | });
|
333 | 335 | }
|
334 | 336 |
|
| 337 | + /** |
| 338 | + * Invokes an action for each element in the observable sequence, and blocks until the sequence is terminated. |
| 339 | + * <p> |
| 340 | + * NOTE: This will block even if the Observable is asynchronous. |
| 341 | + * <p> |
| 342 | + * This is similar to {@link #subscribe(Observer)} but blocks. Because it blocks it does not need the {@link Observer#onCompleted()} or {@link Observer#onError(Exception)} methods. |
| 343 | + * |
| 344 | + * @param onNext |
| 345 | + * {@link Action1} |
| 346 | + * @throws RuntimeException |
| 347 | + * if error occurs |
| 348 | + */ |
| 349 | + public void forEach(final Action1<T> onNext) { |
| 350 | + final CountDownLatch latch = new CountDownLatch(1); |
| 351 | + final AtomicReference<Exception> exceptionFromOnError = new AtomicReference<Exception>(); |
| 352 | + |
| 353 | + subscribe(new Observer<T>() { |
| 354 | + public void onCompleted() { |
| 355 | + latch.countDown(); |
| 356 | + } |
| 357 | + |
| 358 | + public void onError(Exception e) { |
| 359 | + /* |
| 360 | + * If we receive an onError event we set the reference on the outer thread |
| 361 | + * so we can git it and throw after the latch.await(). |
| 362 | + * |
| 363 | + * We do this instead of throwing directly since this may be on a different thread and the latch is still waiting. |
| 364 | + */ |
| 365 | + exceptionFromOnError.set(e); |
| 366 | + latch.countDown(); |
| 367 | + } |
| 368 | + |
| 369 | + public void onNext(T args) { |
| 370 | + onNext.call(args); |
| 371 | + } |
| 372 | + }); |
| 373 | + // block until the subscription completes and then return |
| 374 | + try { |
| 375 | + latch.await(); |
| 376 | + } catch (InterruptedException e) { |
| 377 | + // set the interrupted flag again so callers can still get it |
| 378 | + // for more information see https://github.com/Netflix/RxJava/pull/147#issuecomment-13624780 |
| 379 | + Thread.currentThread().interrupt(); |
| 380 | + // using Runtime so it is not checked |
| 381 | + throw new RuntimeException("Interrupted while waiting for subscription to complete.", e); |
| 382 | + } |
| 383 | + |
| 384 | + if (exceptionFromOnError.get() != null) { |
| 385 | + if (exceptionFromOnError.get() instanceof RuntimeException) { |
| 386 | + throw (RuntimeException) exceptionFromOnError.get(); |
| 387 | + } else { |
| 388 | + throw new RuntimeException(exceptionFromOnError.get()); |
| 389 | + } |
| 390 | + } |
| 391 | + } |
| 392 | + |
| 393 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 394 | + public void forEach(final Object o) { |
| 395 | + if (o instanceof Action1) { |
| 396 | + // in case a dynamic language is not correctly handling the overloaded methods and we receive an Action1 just forward to the correct method. |
| 397 | + forEach((Action1) o); |
| 398 | + } |
| 399 | + |
| 400 | + // lookup and memoize onNext |
| 401 | + if (o == null) { |
| 402 | + throw new RuntimeException("onNext must be implemented"); |
| 403 | + } |
| 404 | + final FuncN onNext = Functions.from(o); |
| 405 | + |
| 406 | + forEach(new Action1() { |
| 407 | + |
| 408 | + public void call(Object args) { |
| 409 | + onNext.call(args); |
| 410 | + } |
| 411 | + |
| 412 | + }); |
| 413 | + } |
| 414 | + |
335 | 415 | /**
|
336 | 416 | * Allow the {@link RxJavaErrorHandler} to receive the exception from onError.
|
337 | 417 | *
|
@@ -2543,7 +2623,6 @@ public void testSequenceEqual() {
|
2543 | 2623 | verify(result, times(1)).onNext(false);
|
2544 | 2624 | }
|
2545 | 2625 |
|
2546 |
| - |
2547 |
| - |
2548 | 2626 | }
|
| 2627 | + |
2549 | 2628 | }
|
0 commit comments