2014-10-03 17:33:14 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.stream.javadsl
|
|
|
|
|
|
2014-10-20 14:09:24 +02:00
|
|
|
import akka.actor.ActorRef
|
|
|
|
|
import akka.actor.Props
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.stream.javadsl
|
2014-10-27 14:35:41 +01:00
|
|
|
import akka.stream.scaladsl
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream._
|
2014-10-20 14:09:24 +02:00
|
|
|
import org.reactivestreams.Publisher
|
|
|
|
|
import org.reactivestreams.Subscriber
|
2014-10-17 14:05:50 +02:00
|
|
|
import scala.concurrent.Future
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.impl.StreamLayout
|
2015-03-05 12:21:17 +01:00
|
|
|
import scala.util.Try
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2014-10-20 14:09:24 +02:00
|
|
|
/** Java API */
|
2014-10-03 17:33:14 +02:00
|
|
|
object Sink {
|
2014-10-20 14:09:24 +02:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
val factory: SinkCreate = new SinkCreate {}
|
|
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
/** Adapt [[scaladsl.Sink]] for use within Java DSL */
|
2015-01-28 14:19:50 +01:00
|
|
|
def adapt[O, M](sink: scaladsl.Sink[O, M]): javadsl.Sink[O, M] =
|
2014-10-20 14:09:24 +02:00
|
|
|
new Sink(sink)
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will invoke the given function for every received element, giving it its previous
|
|
|
|
|
* output (or the given `zero` value) and the element as input.
|
|
|
|
|
* The returned [[scala.concurrent.Future]] will be completed with value of the final
|
|
|
|
|
* function evaluation when the input stream ends, or completed with `Failure`
|
2015-01-30 10:30:56 +01:00
|
|
|
* if there is a failure is signaled in the stream.
|
2014-10-17 14:05:50 +02:00
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def fold[U, In](zero: U, f: japi.Function2[U, In, U]): javadsl.Sink[In, Future[U]] =
|
|
|
|
|
new Sink(scaladsl.Sink.fold[U, In](zero)(f.apply))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
2014-10-20 14:09:24 +02:00
|
|
|
* Helper to create [[Sink]] from `Subscriber`.
|
2014-10-17 14:05:50 +02:00
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def create[In](subs: Subscriber[In]): Sink[In, Unit] =
|
|
|
|
|
new Sink(scaladsl.Sink(subs))
|
2014-10-20 14:09:24 +02:00
|
|
|
|
2014-10-20 14:09:24 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a `Sink` that is materialized to an [[akka.actor.ActorRef]] which points to an Actor
|
|
|
|
|
* created according to the passed in [[akka.actor.Props]]. Actor created by the `props` should
|
|
|
|
|
* be [[akka.stream.actor.ActorSubscriber]].
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def create[T](props: Props): Sink[T, ActorRef] =
|
|
|
|
|
new Sink(scaladsl.Sink.apply(props))
|
2014-10-20 14:09:24 +02:00
|
|
|
|
2014-10-20 14:09:24 +02:00
|
|
|
/**
|
|
|
|
|
* A `Sink` that immediately cancels its upstream after materialization.
|
|
|
|
|
*/
|
2015-03-05 12:21:17 +01:00
|
|
|
def cancelled[T](): Sink[T, Unit] =
|
2014-10-27 14:35:41 +01:00
|
|
|
new Sink(scaladsl.Sink.cancelled)
|
2014-10-20 14:09:24 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will consume the stream and discard the elements.
|
2014-10-17 14:05:50 +02:00
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def ignore[T](): Sink[T, Unit] =
|
2014-10-27 14:35:41 +01:00
|
|
|
new Sink(scaladsl.Sink.ignore)
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a [[org.reactivestreams.Publisher]].
|
|
|
|
|
* that can handle one [[org.reactivestreams.Subscriber]].
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def publisher[In](): Sink[In, Publisher[In]] =
|
2015-03-05 12:21:17 +01:00
|
|
|
new Sink(scaladsl.Sink.publisher)
|
2014-10-20 14:09:24 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will invoke the given procedure for each received element. The sink is materialized
|
|
|
|
|
* into a [[scala.concurrent.Future]] will be completed with `Success` when reaching the
|
2015-01-30 10:30:56 +01:00
|
|
|
* normal end of the stream, or completed with `Failure` if there is a failure is signaled in
|
2014-10-20 14:09:24 +02:00
|
|
|
* the stream..
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def foreach[T](f: japi.Procedure[T]): Sink[T, Future[Unit]] =
|
|
|
|
|
new Sink(scaladsl.Sink.foreach(f.apply))
|
2014-10-20 14:09:24 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a [[org.reactivestreams.Publisher]]
|
|
|
|
|
* that can handle more than one [[org.reactivestreams.Subscriber]].
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def fanoutPublisher[T](initialBufferSize: Int, maximumBufferSize: Int): Sink[T, Publisher[T]] =
|
|
|
|
|
new Sink(scaladsl.Sink.fanoutPublisher(initialBufferSize, maximumBufferSize))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
2015-01-30 10:30:56 +01:00
|
|
|
* A `Sink` that when the flow is completed, either through a failure or normal
|
2014-10-17 14:05:50 +02:00
|
|
|
* completion, apply the provided function with [[scala.util.Success]]
|
|
|
|
|
* or [[scala.util.Failure]].
|
|
|
|
|
*/
|
2015-03-05 12:21:17 +01:00
|
|
|
def onComplete[In](callback: japi.Procedure[Try[Unit]]): Sink[In, Unit] =
|
|
|
|
|
new Sink(scaladsl.Sink.onComplete[In](x ⇒ callback.apply(x)))
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a `Future` of the first value received.
|
|
|
|
|
*/
|
2015-03-05 12:21:17 +01:00
|
|
|
def head[In](): Sink[In, Future[In]] =
|
2015-01-28 14:19:50 +01:00
|
|
|
new Sink(scaladsl.Sink.head[In])
|
2014-10-20 14:09:24 +02:00
|
|
|
|
2015-03-04 15:22:33 +01:00
|
|
|
/**
|
|
|
|
|
* A graph with the shape of a sink logically is a sink, this method makes
|
|
|
|
|
* it so also in type.
|
|
|
|
|
*/
|
|
|
|
|
def wrap[T, M](g: Graph[SinkShape[T], M]): Sink[T, M] = new Sink(scaladsl.Sink.wrap(g))
|
2014-10-03 17:33:14 +02:00
|
|
|
}
|
2014-10-17 14:05:50 +02:00
|
|
|
|
2014-10-03 17:33:14 +02:00
|
|
|
/**
|
2014-10-20 14:09:24 +02:00
|
|
|
* Java API
|
|
|
|
|
*
|
2014-10-03 17:33:14 +02:00
|
|
|
* A `Sink` is a set of stream processing steps that has one open input and an attached output.
|
|
|
|
|
* Can be used as a `Subscriber`
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
class Sink[-In, +Mat](delegate: scaladsl.Sink[In, Mat]) extends Graph[SinkShape[In], Mat] {
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
override def shape: SinkShape[In] = delegate.shape
|
|
|
|
|
private[stream] def module: StreamLayout.Module = delegate.module
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2015-03-06 12:22:14 +01:00
|
|
|
/** Converts this Sink to its Scala DSL counterpart */
|
2015-01-28 14:19:50 +01:00
|
|
|
def asScala: scaladsl.Sink[In, Mat] = delegate
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2014-10-20 14:09:24 +02:00
|
|
|
/**
|
2014-10-20 14:09:24 +02:00
|
|
|
* Connect this `Sink` to a `Source` and run it.
|
2014-10-20 14:09:24 +02:00
|
|
|
*/
|
2015-02-26 22:42:34 +01:00
|
|
|
def runWith[M](source: javadsl.Source[In, M], materializer: FlowMaterializer): M =
|
2014-10-20 14:09:24 +02:00
|
|
|
asScala.runWith(source.asScala)(materializer)
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
/**
|
|
|
|
|
* Transform only the materialized value of this Sink, leaving all other properties as they were.
|
|
|
|
|
*/
|
|
|
|
|
def mapMaterialized[Mat2](f: japi.Function[Mat, Mat2]): Sink[In, Mat2] =
|
|
|
|
|
new Sink(delegate.mapMaterialized(f.apply _))
|
2015-03-05 12:21:17 +01:00
|
|
|
|
|
|
|
|
def withAttributes(attr: OperationAttributes): javadsl.Sink[In, Mat] =
|
|
|
|
|
new Sink(delegate.withAttributes(attr.asScala))
|
|
|
|
|
|
|
|
|
|
def named(name: String): javadsl.Sink[In, Mat] =
|
|
|
|
|
new Sink(delegate.named(name))
|
2014-10-17 14:05:50 +02:00
|
|
|
}
|