-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
347 additions
and
4 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
docs/src/main/paradox/stream/operators/Source-or-Flow/collectWhile.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# collectWhile | ||
|
||
Transform this stream by applying the given partial function to each of the elements on which the function is defined as they pass through this processing step, and cancel the upstream publisher after the partial function is not applied. | ||
|
||
@ref[Simple operators](../index.md#simple-operators) | ||
|
||
## Signature | ||
|
||
@apidoc[Source.collectWhile](Source) { scala="#collectWhile[T](pf:PartialFunction[Out,T]):FlowOps.this.Repr[T]" java="#collectWhile(scala.PartialFunction)" } | ||
@apidoc[Flow.collectWhile](Flow) { scala="#collectWhile[T](pf:PartialFunction[Out,T]):FlowOps.this.Repr[T]" java="#collectWhile(scala.PartialFunction)" } | ||
|
||
|
||
## Description | ||
|
||
Transform this stream by applying the given partial function to each of the elements on which the function is defined | ||
as they pass through this processing step, and cancel the upstream publisher after the partial function is not applied. | ||
|
||
## Example | ||
|
||
Scala | ||
: @@snip [Collect.scala](/docs/src/test/scala/docs/stream/operators/sourceorflow/Collect.scala) { #collectWhile } | ||
|
||
Java | ||
: @@snip [SourceOrFlow.java](/docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #collectWhile } | ||
|
||
## Reactive Streams semantics | ||
|
||
@@@div { .callout } | ||
|
||
**emits** when the provided partial function is defined for the element | ||
|
||
**backpressures** when the partial function is defined for the element and downstream backpressures | ||
|
||
**completes** when upstream completes or the partial function is not applied | ||
|
||
**cancels** when downstream cancels | ||
|
||
@@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/FlowCollectWhileSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.pekko.stream.scaladsl | ||
|
||
import org.apache.pekko | ||
import pekko.stream.ActorAttributes._ | ||
import pekko.stream.OverflowStrategy | ||
import pekko.stream.Supervision._ | ||
import pekko.stream.testkit.{ ScriptedTest, StreamSpec } | ||
import pekko.stream.testkit.Utils.TE | ||
import pekko.stream.testkit.scaladsl.TestSink | ||
|
||
class FlowCollectWhileSpec extends StreamSpec with ScriptedTest { | ||
|
||
"A CollectWhile" must { | ||
|
||
"collect in happy path" in { | ||
Source(List(1, 3, 5, 7, 8, 9, 10)) | ||
.collectWhile { | ||
case elem if elem % 2 != 0 => elem | ||
} | ||
.runWith(TestSink()) | ||
.request(7) | ||
.expectNextN(List(1, 3, 5, 7)) | ||
.expectComplete() | ||
} | ||
|
||
"complete with buffer even no explict request" in { | ||
Source(List(2, 3, 5)) | ||
.collectWhile { | ||
case elem if elem % 2 != 0 => elem | ||
} | ||
.buffer(1, overflowStrategy = OverflowStrategy.backpressure) | ||
.runWith(TestSink()) | ||
.ensureSubscription() | ||
.expectComplete() | ||
} | ||
|
||
"complete with empty Source" in { | ||
Source.empty[Int].collectWhile { | ||
case elem if elem % 2 != 0 => elem | ||
}.runWith(TestSink[Int]()) | ||
.ensureSubscription() | ||
.expectComplete() | ||
} | ||
|
||
"restart when pf throws" in { | ||
Source(1 to 6) | ||
.collect { case x: Int => if (x % 2 == 0) throw TE("") else x } | ||
.withAttributes(supervisionStrategy(restartingDecider)) | ||
.runWith(TestSink[Int]()) | ||
.request(1) | ||
.expectNext(1) | ||
.request(1) | ||
.expectNext(3) | ||
.request(1) | ||
.expectNext(5) | ||
.request(1) | ||
.expectComplete() | ||
} | ||
|
||
"resume when pf throws" in { | ||
Source(1 to 6) | ||
.collect { case x: Int => if (x % 2 == 0) throw TE("") else x } | ||
.withAttributes(supervisionStrategy(resumingDecider)) | ||
.runWith(TestSink[Int]()) | ||
.request(1) | ||
.expectNext(1) | ||
.request(1) | ||
.expectNext(3) | ||
.request(1) | ||
.expectNext(5) | ||
.request(1) | ||
.expectComplete() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
stream/src/main/scala/org/apache/pekko/stream/impl/fusing/CollectWhile.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.pekko.stream.impl.fusing | ||
|
||
import scala.util.control.NonFatal | ||
|
||
import org.apache.pekko | ||
import pekko.annotation.InternalApi | ||
import pekko.stream.{ Attributes, FlowShape, Inlet, Outlet, Supervision } | ||
import pekko.stream.ActorAttributes.SupervisionStrategy | ||
import pekko.stream.Attributes.SourceLocation | ||
import pekko.stream.impl.Stages.DefaultAttributes | ||
import pekko.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler } | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@InternalApi | ||
private[pekko] final class CollectWhile[In, Out](pf: PartialFunction[In, Out]) extends GraphStage[FlowShape[In, Out]] { | ||
private val in = Inlet[In]("CollectWhile.in") | ||
private val out = Outlet[Out]("CollectWhile.out") | ||
override val shape = FlowShape(in, out) | ||
|
||
override def initialAttributes: Attributes = DefaultAttributes.collectWhile and SourceLocation.forLambda(pf) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) with InHandler with OutHandler { | ||
private lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider | ||
import Collect.NotApplied | ||
|
||
override final def onPush(): Unit = | ||
try { | ||
pf.applyOrElse(grab(in), NotApplied) match { | ||
case NotApplied => completeStage() | ||
case result: Out @unchecked => push(out, result) | ||
case _ => throw new RuntimeException() // won't happen, compiler exhaustiveness check pleaser | ||
} | ||
} catch { | ||
case NonFatal(ex) => | ||
decider(ex) match { | ||
case Supervision.Stop => failStage(ex) | ||
case _ => pull(in) | ||
} | ||
} | ||
|
||
override final def onPull(): Unit = pull(in) | ||
|
||
setHandlers(in, out, this) | ||
} | ||
|
||
override def toString: String = "CollectWhile" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.