Add fluent API for 2-stream merging (#27020)

* fluent API for 2-stream mergeLatest, mergePreferred and mergePrioritized
* Add javadocs and 'eagerClose' paramter to 'mergeLatest'
* fluent API's for
This commit is contained in:
Arnout Engelen 2019-07-16 10:54:18 +02:00 committed by Johan Andrén
parent 0f7dbf6fcb
commit d3170a56ea
9 changed files with 404 additions and 1 deletions

View file

@ -1091,6 +1091,98 @@ final class Source[Out, Mat](delegate: scaladsl.Source[Out, Mat]) extends Graph[
eagerComplete: Boolean): javadsl.Source[Out, M2] =
new Source(delegate.mergeMat(that, eagerComplete)(combinerToScala(matF)))
/**
* MergeLatest joins elements from N input streams into stream of lists of size N.
* i-th element in list is the latest emitted element from i-th input stream.
* MergeLatest emits list for each element emitted from some input stream,
* but only after each input stream emitted at least one element.
*
* '''Emits when''' an element is available from some input and each input emits at least one element from stream start
*
* '''Completes when''' all upstreams complete (eagerClose=false) or one upstream completes (eagerClose=true)
*/
def mergeLatest[M](
that: Graph[SourceShape[Out], M],
eagerComplete: Boolean): javadsl.Source[java.util.List[Out], Mat] =
new Source(delegate.mergeLatest(that, eagerComplete).map(_.asJava))
/**
* MergeLatest joins elements from N input streams into stream of lists of size N.
* i-th element in list is the latest emitted element from i-th input stream.
* MergeLatest emits list for each element emitted from some input stream,
* but only after each input stream emitted at least one element.
*
* @see [[#mergeLatest]].
*
* It is recommended to use the internally optimized `Keep.left` and `Keep.right` combiners
* where appropriate instead of manually writing functions that pass through one of the values.
*/
def mergeLatestMat[Mat2, Mat3](
that: Graph[SourceShape[Out], Mat2],
eagerComplete: Boolean,
matF: function.Function2[Mat, Mat2, Mat3]): javadsl.Source[java.util.List[Out], Mat3] =
new Source(delegate.mergeLatestMat(that, eagerComplete)(combinerToScala(matF))).map(_.asJava)
/**
* Merge two sources. Prefer one source if both sources have elements ready.
*
* '''emits''' when one of the inputs has an element available. If multiple have elements available, prefer the 'right' one when 'preferred' is 'true', or the 'left' one when 'preferred' is 'false'.
*
* '''backpressures''' when downstream backpressures
*
* '''completes''' when all upstreams complete (This behavior is changeable to completing when any upstream completes by setting `eagerComplete=true`.)
*/
def mergePreferred[M](
that: Graph[SourceShape[Out], M],
preferred: Boolean,
eagerComplete: Boolean): javadsl.Source[Out, Mat] =
new Source(delegate.mergePreferred(that, preferred, eagerComplete))
/**
* Merge two sources. Prefer one source if both sources have elements ready.
*
* @see [[#mergePreferred]]
*
* It is recommended to use the internally optimized `Keep.left` and `Keep.right` combiners
* where appropriate instead of manually writing functions that pass through one of the values.
*/
def mergePreferredMat[Mat2, Mat3](
that: Graph[SourceShape[Out], Mat2],
preferred: Boolean,
eagerComplete: Boolean,
matF: function.Function2[Mat, Mat2, Mat3]): javadsl.Source[Out, Mat3] =
new Source(delegate.mergePreferredMat(that, preferred, eagerComplete)(combinerToScala(matF)))
/**
* Merge two sources. Prefer the sources depending on the 'priority' parameters.
*
* '''emits''' when one of the inputs has an element available, preferring inputs based on the 'priority' parameters if both have elements available
*
* '''backpressures''' when downstream backpressures
*
* '''completes''' when both upstreams complete (This behavior is changeable to completing when any upstream completes by setting `eagerComplete=true`.)
*/
def mergePrioritized[M](
that: Graph[SourceShape[Out], M],
leftPriority: Int,
rightPriority: Int,
eagerComplete: Boolean): javadsl.Source[Out, Mat] =
new Source(delegate.mergePrioritized(that, leftPriority, rightPriority, eagerComplete))
/**
* Merge multiple sources. Prefer the sources depending on the 'priority' parameters.
*
* It is recommended to use the internally optimized `Keep.left` and `Keep.right` combiners
* where appropriate instead of manually writing functions that pass through one of the values.
*/
def mergePrioritizedMat[Mat2, Mat3](
that: Graph[SourceShape[Out], Mat2],
leftPriority: Int,
rightPriority: Int,
eagerComplete: Boolean,
matF: function.Function2[Mat, Mat2, Mat3]): javadsl.Source[Out, Mat3] =
new Source(delegate.mergePrioritizedMat(that, leftPriority, rightPriority, eagerComplete)(combinerToScala(matF)))
/**
* Merge the given [[Source]] to this [[Source]], taking elements as they arrive from input streams,
* picking always the smallest of the available elements (waiting for one element from each side