He-Pin commented on code in PR #2057: URL: https://github.com/apache/pekko/pull/2057#discussion_r2295507835
########## actor/src/main/scala/org/apache/pekko/dispatch/CompletionStages.scala: ########## @@ -0,0 +1,217 @@ +/* + * 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.dispatch + +import org.apache.pekko + +import java.util.Optional +import java.util.concurrent.atomic.AtomicReference +import java.util.concurrent.{ CompletableFuture, CompletionStage, Executor } +import java.util.function.{ BiConsumer, BiFunction } + +/** + * CompletionStages provides utilities for working with `CompletionStage`s. + */ +object CompletionStages { + + /** + * Convert a `CompletionStage` to a Scala `Future`. + */ + def asScala[T](stage: CompletionStage[T]): scala.concurrent.Future[T] = { + import org.apache.pekko.util.FutureConverters._ + stage.asScala + } + + /** + * Return the first `CompletionStage` in the given `Iterable` that matches the given predicate. + * If no stage matches the predicate, an empty `Optional` is returned. + * + * @param stages the stages to search through + * @param predicate the predicate to apply to each completed stage + * @return a `CompletionStage` that, when completed, will contain an `Optional` with the first matching element, + * or an empty `Optional` if none matched + * @since 1.2.0 + */ + def find[T <: AnyRef]( + stages: java.lang.Iterable[_ <: CompletionStage[_ <: T]], + predicate: pekko.japi.function.Function[T, java.lang.Boolean] + ): CompletionStage[Optional[T]] = { + def search(iterator: java.util.Iterator[_ <: CompletionStage[_ <: T]]): CompletionStage[Optional[T]] = { + if (iterator.hasNext) { + iterator.next().thenCompose { t => + if (predicate.apply(t)) { + CompletableFuture.completedFuture(Optional.of(t)) + } else { + search(iterator) + } + } + } else { + CompletableFuture.completedFuture(Optional.empty()) + } + } + search(stages.iterator()) + } + + /** + * Returns a new `CompletionStage` that, when completed, will contain the result of the first + * of the given stages to complete, with the same value or exception. + * + * If the given iterable is empty, the returned stage never completes. + * @param stages the stages + * @return a `CompletionStage` that, when completed, will contain the result of the first stage to complete + * with the same value or exception. + * @since 1.2.0 + */ + def firstCompletedOf[T <: AnyRef](stages: java.lang.Iterable[_ <: CompletionStage[_ <: T]]): CompletionStage[T] = { + val iterator = stages.iterator() + if (!iterator.hasNext) { + new CompletableFuture[T]() // never completes + } else { + val promise = new CompletableFuture[T]() + val holder = new AtomicReference[CompletableFuture[T]](promise) with BiConsumer[T, Throwable] { + override def accept(t: T, ex: Throwable): Unit = { + val current = getAndSet(null) + if (current ne null) { + if (ex != null) current.completeExceptionally(ex) + else current.complete(t) + } + } + } + while (iterator.hasNext && (holder.get() ne null)) { + iterator.next().whenComplete(holder) + } + promise + } + } + + private def foldWithNext[T, R]( + iterator: java.util.Iterator[_ <: CompletionStage[_ <: T]], + previous: R, + function: pekko.japi.function.Function2[R, T, R]): CompletionStage[R] = { + if (iterator.hasNext) { + iterator.next().thenCompose { t => + val next = function.apply(previous, t) + foldWithNext[T, R](iterator, next, function) + } + } else { + CompletableFuture.completedFuture(previous) + } + } + + /** + * Aggregate the results of the given stages using the given associative function and a zero value. + * The stages are processed in the order they are given. + * + * @param zero the zero value + * @param stages the stages to aggregate + * @param function the associative function to use for aggregation + * @return a `CompletionStage` that, when completed, will contain the aggregated result + * @since 1.2.0 + */ + def fold[T <: AnyRef, R]( + zero: R, + stages: java.lang.Iterable[_ <: CompletionStage[_ <: T]], + function: pekko.japi.function.Function2[R, T, R]): CompletionStage[R] = { + foldWithNext[T, R](stages.iterator(), zero, function) Review Comment: Scala 2.12 needs these `[T,R]` :( -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
