-
Notifications
You must be signed in to change notification settings - Fork 154
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
8 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
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,47 @@ | ||
# Sink.none | ||
|
||
A `Sink` that will test the given predicate `p` for every received element and completes with the result. | ||
|
||
@ref[Sink operators](../index.md#sink-operators) | ||
|
||
## Signature | ||
|
||
@apidoc[Sink.none](Sink$) { scala="#none[T](p:T=%3EBoolean):org.apache.pekko.stream.scaladsl.Sink[T,scala.concurrent.Future[Boolean]]" java="#none(org.apache.pekko.japi.function.Predicate)" } | ||
|
||
## Description | ||
none operator applies a predicate function to assert each element received, it returns false if any element satisfy the assertion, otherwise it returns true. | ||
|
||
It materializes into a `Future` (in Scala) or a `CompletionStage` (in Java) that completes with the last state when the stream has finished. | ||
|
||
Notes that if source is empty, it will return true | ||
|
||
A `Sink` that will test the given predicate `p` for every received element and | ||
|
||
- completes and returns @scala[`Future`] @java[`CompletionStage`] of `true` if the predicate is false for all elements; | ||
- completes and returns @scala[`Future`] @java[`CompletionStage`] of `true` if the stream is empty (i.e. completes before signalling any elements); | ||
- completes and returns @scala[`Future`] @java[`CompletionStage`] of `false` if the predicate is true for any element. | ||
|
||
The materialized value @scala[`Future`] @java[`CompletionStage`] will be completed with the value `true` or `false` | ||
when the input stream ends, or completed with `Failure` if there is a failure signaled in the stream. | ||
|
||
## Example | ||
|
||
This example tests all elements in the stream is `<=` 100. | ||
|
||
Scala | ||
: @@snip [ForAll.scala](/docs/src/test/scala/docs/stream/operators/sink/NoneMatch.scala) { #none } | ||
|
||
Java | ||
: @@snip [ForAll.java](/docs/src/test/java/jdocs/stream/operators/sink/NoneMatch.java) { #none } | ||
|
||
## Reactive Streams Semantics | ||
|
||
@@@div { .callout } | ||
|
||
***Completes*** when upstream completes or the predicate `p` returns `true` | ||
|
||
**cancels** when predicate `p` returns `true` | ||
|
||
**backpressures** when the invocation of predicate `p` has not yet completed | ||
|
||
@@@ |
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
41 changes: 41 additions & 0 deletions
41
docs/src/test/java/jdocs/stream/operators/sink/NoneMatch.java
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,41 @@ | ||
/* | ||
* 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 jdocs.stream.operators.sink; | ||
|
||
import org.apache.pekko.actor.ActorSystem; | ||
import org.apache.pekko.stream.javadsl.Sink; | ||
import org.apache.pekko.stream.javadsl.Source; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class NoneMatch { | ||
private ActorSystem system = null; | ||
|
||
public void noneUsage() throws Exception { | ||
// #none | ||
final boolean noneMatch = | ||
Source.range(1, 100) | ||
.runWith(Sink.none(elem -> elem > 100), system) | ||
.toCompletableFuture() | ||
.get(3, TimeUnit.SECONDS); | ||
System.out.println(noneMatch); | ||
// Expect prints: | ||
// true | ||
// #none | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
docs/src/test/scala/docs/stream/operators/sink/NoneMatch.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,40 @@ | ||
/* | ||
* 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 docs.stream.operators.sink | ||
|
||
import org.apache.pekko.actor.ActorSystem | ||
import org.apache.pekko.stream.scaladsl.{ Sink, Source } | ||
|
||
import scala.concurrent.duration.DurationInt | ||
import scala.concurrent.{ Await, ExecutionContextExecutor, Future } | ||
|
||
object NoneMatch { | ||
implicit val system: ActorSystem = ??? | ||
implicit val ec: ExecutionContextExecutor = system.dispatcher | ||
def noneExample(): Unit = { | ||
// #none | ||
val result: Future[Boolean] = | ||
Source(1 to 100) | ||
.runWith(Sink.none(_ > 100)) | ||
val noneMatch = Await.result(result, 3.seconds) | ||
println(noneMatch) | ||
// Expect prints: | ||
// true | ||
// #none | ||
} | ||
} |
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