|
1 | 1 | package reactor.core.scala.publisher |
2 | 2 |
|
3 | | -import java.lang.{Iterable => JIterable, Long => JLong} |
| 3 | +import java.lang.{Iterable => JIterable, Long => JLong, Boolean => JBoolean} |
4 | 4 | import java.util |
5 | 5 | import java.util.concurrent.Callable |
6 | 6 | import java.util.function.{BiFunction, Consumer, Function, Supplier} |
@@ -1231,6 +1231,52 @@ class Flux[T] private[publisher](private[publisher] val jFlux: JFlux[T]) extends |
1231 | 1231 | */ |
1232 | 1232 | final def filter(p: T => Boolean) = Flux(jFlux.filter(p)) |
1233 | 1233 |
|
| 1234 | + /** |
| 1235 | + * Test each value emitted by this [[Flux]] asynchronously using a generated |
| 1236 | + * [[Publisher[Boolean]]] test. A value is replayed if the first item emitted |
| 1237 | + * by its corresponding test is `true`. It is dropped if its test is either |
| 1238 | + * empty or its first emitted value is `false`. |
| 1239 | + * <p> |
| 1240 | + * Note that only the first value of the test publisher is considered, and unless it |
| 1241 | + * is a [[Mono]], test will be cancelled after receiving that first value. Test |
| 1242 | + * publishers are generated and subscribed to in sequence. |
| 1243 | + * |
| 1244 | + * @param asyncPredicate the function generating a [[Publisher]] of [[Boolean]] |
| 1245 | + * for each value, to filter the Flux with |
| 1246 | + * @return a filtered [[Flux]] |
| 1247 | + */ |
| 1248 | + final def filterWhen(asyncPredicate: T => _ <: Publisher[Boolean] with MapablePublisher[Boolean]): Flux[T] = { |
| 1249 | + val asyncPredicateFunction = new Function[T, Publisher[JBoolean]] { |
| 1250 | + override def apply(t: T): Publisher[JBoolean] = asyncPredicate(t).map(Boolean2boolean(_)) |
| 1251 | + } |
| 1252 | + Flux(jFlux.filterWhen(asyncPredicateFunction)) |
| 1253 | + } |
| 1254 | + |
| 1255 | + /** |
| 1256 | + * Test each value emitted by this [[Flux]] asynchronously using a generated |
| 1257 | + * [[Publisher[Boolean]]] test. A value is replayed if the first item emitted |
| 1258 | + * by its corresponding test is `true`. It is dropped if its test is either |
| 1259 | + * empty or its first emitted value is `false`. |
| 1260 | + * <p> |
| 1261 | + * Note that only the first value of the test publisher is considered, and unless it |
| 1262 | + * is a [[Mono]], test will be cancelled after receiving that first value. Test |
| 1263 | + * publishers are generated and subscribed to in sequence. |
| 1264 | + * |
| 1265 | + * @param asyncPredicate the function generating a [[Publisher]] of [[Boolean]] |
| 1266 | + * for each value, to filter the Flux with |
| 1267 | + * @param bufferSize the maximum expected number of values to hold pending a result of |
| 1268 | + * their respective asynchronous predicates, rounded to the next power of two. This is |
| 1269 | + * capped depending on the size of the heap and the JVM limits, so be careful with |
| 1270 | + * large values (although eg. { @literal 65536} should still be fine). Also serves as |
| 1271 | + * the initial request size for the source. |
| 1272 | + * @return a filtered [[Flux]] |
| 1273 | + */ |
| 1274 | + final def filterWhen(asyncPredicate: T => _ <: Publisher[Boolean] with MapablePublisher[Boolean], bufferSize: Int): Flux[T] = { |
| 1275 | + val asyncPredicateFunction = new Function[T, Publisher[JBoolean]] { |
| 1276 | + override def apply(t: T): Publisher[JBoolean] = asyncPredicate(t).map(Boolean2boolean(_)) |
| 1277 | + } |
| 1278 | + Flux(jFlux.filterWhen(asyncPredicateFunction, bufferSize)) |
| 1279 | + } |
1234 | 1280 | /** |
1235 | 1281 | * Emit from the fastest first sequence between this publisher and the given publisher |
1236 | 1282 | * |
|
0 commit comments