| 1 |
/************************************************************************ |
| 2 |
* Licensed under Public Domain (CC0) * |
| 3 |
* * |
| 4 |
* To the extent possible under law, the person who associated CC0 with * |
| 5 |
* this code has waived all copyright and related or neighboring * |
| 6 |
* rights to this code. * |
| 7 |
* * |
| 8 |
* You should have received a copy of the CC0 legalcode along with this * |
| 9 |
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.* |
| 10 |
************************************************************************/ |
| 11 |
|
| 12 |
package org.reactivestreams.tck.flow.support; |
| 13 |
|
| 14 |
import java.util.concurrent.Flow; |
| 15 |
|
| 16 |
/** |
| 17 |
* SyncTriggeredDemandSubscriber is an implementation of Reactive Streams `Subscriber`, |
| 18 |
* it runs synchronously (on the Publisher's thread) and requests demand triggered from |
| 19 |
* "the outside" using its `triggerDemand` method and from "the inside" using the return |
| 20 |
* value of its user-defined `whenNext` method which is invoked to process each element. |
| 21 |
* |
| 22 |
* NOTE: The code below uses a lot of try-catches to show the reader where exceptions can be expected, and where they are forbidden. |
| 23 |
*/ |
| 24 |
// FIXME, depend on the reactive streams version? but that's in test scope... |
| 25 |
public abstract class SyncTriggeredDemandFlowSubscriber<T> implements Flow.Subscriber<T> { |
| 26 |
private Flow.Subscription subscription; // Obeying rule 3.1, we make this private! |
| 27 |
private boolean done = false; |
| 28 |
|
| 29 |
@Override public void onSubscribe(final Flow.Subscription s) { |
| 30 |
// As per rule 2.13, we need to throw a `java.lang.NullPointerException` if the `Subscription` is `null` |
| 31 |
if (s == null) throw null; |
| 32 |
|
| 33 |
if (subscription != null) { // If someone has made a mistake and added this Subscriber multiple times, let's handle it gracefully |
| 34 |
try { |
| 35 |
s.cancel(); // Cancel the additional subscription |
| 36 |
} catch(final Throwable t) { |
| 37 |
//Subscription.cancel is not allowed to throw an exception, according to rule 3.15 |
| 38 |
(new IllegalStateException(s + " violated the Reactive Streams rule 3.15 by throwing an exception from cancel.", t)).printStackTrace(System.err); |
| 39 |
} |
| 40 |
} else { |
| 41 |
// We have to assign it locally before we use it, if we want to be a synchronous `Subscriber` |
| 42 |
// Because according to rule 3.10, the Subscription is allowed to call `onNext` synchronously from within `request` |
| 43 |
subscription = s; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Requests the provided number of elements from the `Subscription` of this `Subscriber`. |
| 49 |
* NOTE: This makes no attempt at thread safety so only invoke it once from the outside to initiate the demand. |
| 50 |
* @return `true` if successful and `false` if not (either due to no `Subscription` or due to exceptions thrown) |
| 51 |
*/ |
| 52 |
public boolean triggerDemand(final long n) { |
| 53 |
final Flow.Subscription s = subscription; |
| 54 |
if (s == null) return false; |
| 55 |
else { |
| 56 |
try { |
| 57 |
s.request(n); |
| 58 |
} catch(final Throwable t) { |
| 59 |
// Subscription.request is not allowed to throw according to rule 3.16 |
| 60 |
(new IllegalStateException(s + " violated the Reactive Streams rule 3.16 by throwing an exception from request.", t)).printStackTrace(System.err); |
| 61 |
return false; |
| 62 |
} |
| 63 |
return true; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
@Override public void onNext(final T element) { |
| 68 |
if (subscription == null) { // Technically this check is not needed, since we are expecting Publishers to conform to the spec |
| 69 |
(new IllegalStateException("Publisher violated the Reactive Streams rule 1.09 signalling onNext prior to onSubscribe.")).printStackTrace(System.err); |
| 70 |
} else { |
| 71 |
// As per rule 2.13, we need to throw a `java.lang.NullPointerException` if the `element` is `null` |
| 72 |
if (element == null) throw null; |
| 73 |
|
| 74 |
if (!done) { // If we aren't already done |
| 75 |
try { |
| 76 |
final long need = foreach(element); |
| 77 |
if (need > 0) triggerDemand(need); |
| 78 |
else if (need == 0) {} |
| 79 |
else { |
| 80 |
done(); |
| 81 |
} |
| 82 |
} catch (final Throwable t) { |
| 83 |
done(); |
| 84 |
try { |
| 85 |
onError(t); |
| 86 |
} catch (final Throwable t2) { |
| 87 |
//Subscriber.onError is not allowed to throw an exception, according to rule 2.13 |
| 88 |
(new IllegalStateException(this + " violated the Reactive Streams rule 2.13 by throwing an exception from onError.", t2)).printStackTrace(System.err); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Showcases a convenience method to idempotently marking the Subscriber as "done", so we don't want to process more elements |
| 96 |
// herefor we also need to cancel our `Subscription`. |
| 97 |
private void done() { |
| 98 |
//On this line we could add a guard against `!done`, but since rule 3.7 says that `Subscription.cancel()` is idempotent, we don't need to. |
| 99 |
done = true; // If we `whenNext` throws an exception, let's consider ourselves done (not accepting more elements) |
| 100 |
try { |
| 101 |
subscription.cancel(); // Cancel the subscription |
| 102 |
} catch(final Throwable t) { |
| 103 |
//Subscription.cancel is not allowed to throw an exception, according to rule 3.15 |
| 104 |
(new IllegalStateException(subscription + " violated the Reactive Streams rule 3.15 by throwing an exception from cancel.", t)).printStackTrace(System.err); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// This method is left as an exercise to the reader/extension point |
| 109 |
// Don't forget to call `triggerDemand` at the end if you are interested in more data, |
| 110 |
// a return value of < 0 indicates that the subscription should be cancelled, |
| 111 |
// a value of 0 indicates that there is no current need, |
| 112 |
// a value of > 0 indicates the current need. |
| 113 |
protected abstract long foreach(final T element); |
| 114 |
|
| 115 |
@Override public void onError(final Throwable t) { |
| 116 |
if (subscription == null) { // Technically this check is not needed, since we are expecting Publishers to conform to the spec |
| 117 |
(new IllegalStateException("Publisher violated the Reactive Streams rule 1.09 signalling onError prior to onSubscribe.")).printStackTrace(System.err); |
| 118 |
} else { |
| 119 |
// As per rule 2.13, we need to throw a `java.lang.NullPointerException` if the `Throwable` is `null` |
| 120 |
if (t == null) throw null; |
| 121 |
// Here we are not allowed to call any methods on the `Subscription` or the `Publisher`, as per rule 2.3 |
| 122 |
// And anyway, the `Subscription` is considered to be cancelled if this method gets called, as per rule 2.4 |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Override public void onComplete() { |
| 127 |
if (subscription == null) { // Technically this check is not needed, since we are expecting Publishers to conform to the spec |
| 128 |
(new IllegalStateException("Publisher violated the Reactive Streams rule 1.09 signalling onComplete prior to onSubscribe.")).printStackTrace(System.err); |
| 129 |
} else { |
| 130 |
// Here we are not allowed to call any methods on the `Subscription` or the `Publisher`, as per rule 2.3 |
| 131 |
// And anyway, the `Subscription` is considered to be cancelled if this method gets called, as per rule 2.4 |
| 132 |
} |
| 133 |
} |
| 134 |
} |