Align lazy and future operators #26446

This commit is contained in:
Johan Andrén 2019-10-16 17:02:12 +02:00 committed by GitHub
parent 4a72985e48
commit 74adecb4e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
59 changed files with 2091 additions and 384 deletions

View file

@ -10,7 +10,9 @@ import java.util.concurrent.{ CompletableFuture, CompletionStage }
import java.util.function.{ BiFunction, Supplier }
import akka.actor.{ ActorRef, Cancellable, ClassicActorSystemProvider }
import akka.dispatch.ExecutionContexts
import akka.event.LoggingAdapter
import akka.japi.function.Creator
import akka.japi.{ function, JavaPartialFunction, Pair, Util }
import akka.stream._
import akka.stream.impl.LinearTraversalBuilder
@ -171,8 +173,9 @@ object Source {
* may happen before or after materializing the `Flow`.
* The stream terminates with a failure if the `Future` is completed with a failure.
*/
@deprecated("Use 'Source.future' instead", "2.6.0")
def fromFuture[O](future: Future[O]): javadsl.Source[O, NotUsed] =
new Source(scaladsl.Source.fromFuture(future))
new Source(scaladsl.Source.future(future))
/**
* Starts a new `Source` from the given `CompletionStage`. The stream will consist of
@ -180,14 +183,16 @@ object Source {
* may happen before or after materializing the `Flow`.
* The stream terminates with a failure if the `CompletionStage` is completed with a failure.
*/
@deprecated("Use 'Source.completionStage' instead", "2.6.0")
def fromCompletionStage[O](future: CompletionStage[O]): javadsl.Source[O, NotUsed] =
new Source(scaladsl.Source.fromCompletionStage(future))
new Source(scaladsl.Source.completionStage(future))
/**
* Streams the elements of the given future source once it successfully completes.
* If the [[Future]] fails the stream is failed with the exception from the future. If downstream cancels before the
* stream completes the materialized [[Future]] will be failed with a [[StreamDetachedException]].
*/
@deprecated("Use 'Source.futureSource' (potentially together with `Source.fromGraph`) instead", "2.6.0")
def fromFutureSource[T, M](future: Future[_ <: Graph[SourceShape[T], M]]): javadsl.Source[T, Future[M]] =
new Source(scaladsl.Source.fromFutureSource(future))
@ -197,9 +202,10 @@ object Source {
* If downstream cancels before the stream completes the materialized [[CompletionStage]] will be failed
* with a [[StreamDetachedException]]
*/
@deprecated("Use 'Source.completionStageSource' (potentially together with `Source.fromGraph`) instead", "2.6.0")
def fromSourceCompletionStage[T, M](
completion: CompletionStage[_ <: Graph[SourceShape[T], M]]): javadsl.Source[T, CompletionStage[M]] =
new Source(scaladsl.Source.fromSourceCompletionStage(completion))
completionStageSource(completion.thenApply(fromGraph[T, M]))
/**
* Elements are emitted periodically with the specified interval.
@ -262,6 +268,7 @@ object Source {
* the materialized future is completed with its value, if downstream cancels or fails without any demand the
* `create` factory is never called and the materialized `CompletionStage` is failed.
*/
@deprecated("Use 'Source.lazySource' instead", "2.6.0")
def lazily[T, M](create: function.Creator[Source[T, M]]): Source[T, CompletionStage[M]] =
scaladsl.Source.lazily[T, M](() => create.create().asScala).mapMaterializedValue(_.toJava).asJava
@ -272,9 +279,115 @@ object Source {
*
* @see [[Source.lazily]]
*/
@deprecated("Use 'Source.lazyCompletionStage' instead", "2.6.0")
def lazilyAsync[T](create: function.Creator[CompletionStage[T]]): Source[T, Future[NotUsed]] =
scaladsl.Source.lazilyAsync[T](() => create.create().toScala).asJava
/**
* Emits a single value when the given Scala `Future` is successfully completed and then completes the stream.
* The stream fails if the `Future` is completed with a failure.
*
* Here for Java interoperability, the normal use from Java should be [[Source.completionStage]]
*/
def future[T](futureElement: Future[T]): Source[T, NotUsed] =
scaladsl.Source.future(futureElement).asJava
/**
* Emits a single value when the given `CompletionStage` is successfully completed and then completes the stream.
* If the `CompletionStage` is completed with a failure the stream is failed.
*/
def completionStage[T](completionStage: CompletionStage[T]): Source[T, NotUsed] =
future(completionStage.toScala)
/**
* Turn a `CompletionStage[Source]` into a source that will emit the values of the source when the future completes successfully.
* If the `CompletionStage` is completed with a failure the stream is failed.
*/
def completionStageSource[T, M](completionStageSource: CompletionStage[Source[T, M]]): Source[T, CompletionStage[M]] =
scaladsl.Source
.futureSource(completionStageSource.toScala.map(_.asScala)(ExecutionContexts.sameThreadExecutionContext))
.mapMaterializedValue(_.toJava)
.asJava
/**
* Defers invoking the `create` function to create a single element until there is downstream demand.
*
* If the `create` function fails when invoked the stream is failed.
*
* Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts
* the laziness and will trigger the factory immediately.
*
* The materialized future `Done` value is completed when the `create` function has successfully been invoked,
* if the function throws the future materialized value is failed with that exception.
* If downstream cancels or fails before the function is invoked the materialized value
* is failed with a [[akka.stream.NeverMaterializedException]]
*/
def lazySingle[T](create: Creator[T]): Source[T, NotUsed] =
lazySource(() => single(create.create())).mapMaterializedValue(_ => NotUsed)
/**
* Defers invoking the `create` function to create a future element until there is downstream demand.
*
* The returned future element will be emitted downstream when it completes, or fail the stream if the future
* is failed or the `create` function itself fails.
*
* Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts
* the laziness and will trigger the factory immediately.
*
* The materialized future `Done` value is completed when the `create` function has successfully been invoked and the future completes,
* if the function throws or the future fails the future materialized value is failed with that exception.
* If downstream cancels or fails before the function is invoked the materialized value
* is failed with a [[akka.stream.NeverMaterializedException]]
*/
def lazyCompletionStage[T](create: Creator[CompletionStage[T]]): Source[T, NotUsed] =
scaladsl.Source
.lazySource { () =>
val f = create.create().toScala
scaladsl.Source.future(f)
}
.mapMaterializedValue(_ => NotUsed.notUsed())
.asJava
/**
* Defers invoking the `create` function to create a future source until there is downstream demand.
*
* The returned source will emit downstream and behave just like it was the outer source. Downstream completes
* when the created source completes and fails when the created source fails.
*
* Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts
* the laziness and will trigger the factory immediately.
*
* The materialized future value is completed with the materialized value of the created source when
* it has been materialized. If the function throws or the source materialization fails the future materialized value
* is failed with the thrown exception.
*
* If downstream cancels or fails before the function is invoked the materialized value
* is failed with a [[akka.stream.NeverMaterializedException]]
*/
def lazySource[T, M](create: Creator[Source[T, M]]): Source[T, CompletionStage[M]] =
scaladsl.Source.lazySource(() => create.create().asScala).mapMaterializedValue(_.toJava).asJava
/**
* Defers invoking the `create` function to create a future source until there is downstream demand.
*
* The returned future source will emit downstream and behave just like it was the outer source when the `CompletionStage` completes
* successfully. Downstream completes when the created source completes and fails when the created source fails.
* If the `CompletionStage` or the `create` function fails the stream is failed.
*
* Note that asynchronous boundaries (and other operators) in the stream may do pre-fetching which counter acts
* the laziness and triggers the factory immediately.
*
* The materialized `CompletionStage` value is completed with the materialized value of the created source when
* it has been materialized. If the function throws or the source materialization fails the future materialized value
* is failed with the thrown exception.
*
* If downstream cancels or fails before the function is invoked the materialized value
* is failed with a [[akka.stream.NeverMaterializedException]]
*/
def lazyCompletionStageSource[T, M](create: Creator[CompletionStage[Source[T, M]]]): Source[T, CompletionStage[M]] =
lazySource[T, CompletionStage[M]](() => completionStageSource(create.create()))
.mapMaterializedValue(_.thenCompose(csm => csm))
/**
* Creates a `Source` that is materialized as a [[org.reactivestreams.Subscriber]]
*/