Skip to content

Instantly share code, notes, and snippets.

@akaigoro
Created December 19, 2019 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaigoro/9506659d7d87a85a2a58a647405d85d6 to your computer and use it in GitHub Desktop.
Save akaigoro/9506659d7d87a85a2a58a647405d85d6 to your computer and use it in GitHub Desktop.
Describes flow of messages from Subscribers to Publishers , that is, in opposed direction compared to the flow of messages from Publishers to Subscribers in java.util.concurrent.Flow.
import java.util.concurrent.Flow;
/**
* Describes flow of messages from Subscribers to Publishers
* (as opposed to the flow of messages from Publishers to Subscribers in {{@link Flow}}).
* Subribers here act as masters/clients, and Publishes as slaves/servers, just like in {{@link Flow}}).
*/
public class ReverseFlow {
private ReverseFlow() {}
/**
* Consumes messages.
*
* A {@link Publisher} can serve multiple {@link Subscriber}s subscribed dynamically
* at various points in time.
*
* @param <T> the type of messages.
*/
public interface Publisher<T> {
/**
* @param producer the {@link Subscriber} which offers messages for this {@link Publisher}
*/
void subscribe(Subscriber<T> producer);
}
/**
* Produces messages.
*
* A {@link Subscriber} is a provider of a series of tokens, transmitting them to a {@link Publisher}(s).
* <p>
* @param <T> type of messages.
*/
public interface Subscriber<T> {
/**
* Method invoked prior to invoking any other Subscriber
* methods for the given Subscription. If this method throws
* an exception, resulting behavior is not guaranteed, but may
* cause the Subscription not to be established or to be cancelled.
*
* <p>Typically, implementations of this method invoke {@code
* subscription.request} to enable sending items.
*
* @param subscription a new subscription
*/
void onSubscribe(Flow.Subscription subscription);
/**
*
* @return true if producer has completed the flow of items
*/
default boolean isCompleted() {
return false;
}
/**
*
* @return the cause of exceptional completion, if any
* - null otherwise
*/
default Throwable getCompletionException() {
return null;
}
/**
* {@link Publisher} gets data from {@link Subscriber}
* when it has room to save the data.
* @return the data from {@link Subscriber}
*/
T remove();
/**
* called by {@link Publisher} when it is completed and asks to not disturb.
*
*/
void onComplete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment