diff --git a/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java b/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java index 157d6ca6d6..e908335666 100644 --- a/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java +++ b/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java @@ -4,6 +4,7 @@ import org.junit.Test; import static org.junit.Assert.*; import java.util.concurrent.Callable; import java.util.LinkedList; +import java.lang.Iterable; import akka.japi.Function; import akka.japi.Function2; import akka.japi.Procedure; @@ -43,7 +44,7 @@ public class JavaFutureTests { })); } - Future> futureList = sequence(listFutures); + Future> futureList = sequence(listFutures); assertEquals(futureList.get(), listExpected); } @@ -102,7 +103,7 @@ public class JavaFutureTests { listStrings.add("test"); } - Future> result = traverse(listStrings, new Function>(){ + Future> result = traverse(listStrings, new Function>(){ public Future apply(final String r) { return future(new Callable() { public String call() { diff --git a/akka-actor/src/main/scala/akka/dispatch/Future.scala b/akka-actor/src/main/scala/akka/dispatch/Future.scala index 4ec877b8bb..7458ed9f51 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Future.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Future.scala @@ -159,10 +159,10 @@ object Futures { /** * Java API. - * Simple version of Futures.traverse. Transforms a java.lang.Iterable[Future[A]] into a Future[java.util.LinkedList[A]]. + * Simple version of Futures.traverse. Transforms a java.lang.Iterable[Future[A]] into a Future[java.lang.Iterable[A]]. * Useful for reducing many Futures into a single Future. */ - def sequence[A](in: JIterable[Future[A]], timeout: Long): Future[JLinkedList[A]] = + def sequence[A](in: JIterable[Future[A]], timeout: Long): Future[JIterable[A]] = scala.collection.JavaConversions.iterableAsScalaIterable(in).foldLeft(Future(new JLinkedList[A]()))((fr, fa) => for (r <- fr; a <- fa) yield { r add a @@ -171,18 +171,18 @@ object Futures { /** * Java API. - * Simple version of Futures.traverse. Transforms a java.lang.Iterable[Future[A]] into a Future[java.util.LinkedList[A]]. + * Simple version of Futures.traverse. Transforms a java.lang.Iterable[Future[A]] into a Future[java.lang.Iterable[A]]. * Useful for reducing many Futures into a single Future. */ - def sequence[A](in: JIterable[Future[A]]): Future[JLinkedList[A]] = sequence(in, Actor.TIMEOUT) + def sequence[A](in: JIterable[Future[A]]): Future[JIterable[A]] = sequence(in, Actor.TIMEOUT) /** * Java API. - * Transforms a java.lang.Iterable[A] into a Future[java.util.LinkedList[B]] using the provided Function A => Future[B]. + * Transforms a java.lang.Iterable[A] into a Future[java.lang.Iterable[B]] using the provided Function A => Future[B]. * This is useful for performing a parallel map. For example, to apply a function to all items of a list * in parallel. */ - def traverse[A, B](in: JIterable[A], timeout: Long, fn: JFunc[A,Future[B]]): Future[JLinkedList[B]] = + def traverse[A, B](in: JIterable[A], timeout: Long, fn: JFunc[A,Future[B]]): Future[JIterable[B]] = scala.collection.JavaConversions.iterableAsScalaIterable(in).foldLeft(Future(new JLinkedList[B]())){(fr, a) => val fb = fn(a) for (r <- fr; b <- fb) yield { @@ -193,11 +193,11 @@ object Futures { /** * Java API. - * Transforms a java.lang.Iterable[A] into a Future[java.util.LinkedList[B]] using the provided Function A => Future[B]. + * Transforms a java.lang.Iterable[A] into a Future[java.lang.Iterable[B]] using the provided Function A => Future[B]. * This is useful for performing a parallel map. For example, to apply a function to all items of a list * in parallel. */ - def traverse[A, B](in: JIterable[A], fn: JFunc[A,Future[B]]): Future[JLinkedList[B]] = traverse(in, Actor.TIMEOUT, fn) + def traverse[A, B](in: JIterable[A], fn: JFunc[A,Future[B]]): Future[JIterable[B]] = traverse(in, Actor.TIMEOUT, fn) // ===================================== // Deprecations diff --git a/akka-docs/java/futures.rst b/akka-docs/java/futures.rst index 8e66257443..660dc6bd05 100644 --- a/akka-docs/java/futures.rst +++ b/akka-docs/java/futures.rst @@ -172,12 +172,12 @@ It is very often desirable to be able to combine different Futures with eachothe import akka.dispatch.Future; import static akka.dispatch.Futures.sequence; import akka.japi.Function; - import java.util.LinkedList; + import java.lang.Iterable; - LinkedList> listOfFutureInts = ... //Some source generating a list of Future:s + Iterable> listOfFutureInts = ... //Some source generating a sequence of Future:s - // now we have a Future[List[Int]] - Future> futureListOfInts = sequence(listOfFutureInts); + // now we have a Future[Iterable[Int]] + Future> futureListOfInts = sequence(listOfFutureInts); // Find the sum of the odd numbers Long totalSum = futureListOfInts.map( @@ -190,21 +190,21 @@ It is very often desirable to be able to combine different Futures with eachothe } }).get(); -To better explain what happened in the example, ``Future.sequence`` is taking the ``LinkedList>`` and turning it into a ``Future>``. We can then use ``map`` to work with the ``LinkedList`` directly, and we aggregate the sum of the ``LinkedList``. +To better explain what happened in the example, ``Future.sequence`` is taking the ``Iterable>`` and turning it into a ``Future>``. We can then use ``map`` to work with the ``Iterable`` directly, and we aggregate the sum of the ``Iterable``. -The ``traverse`` method is similar to ``sequence``, but it takes a sequence of ``A``s and applies a function from ``A`` to ``Future`` and returns a ``Future>``, enabling parallel ``map`` over the sequence, if you use ``Futures.future`` to create the ``Future``. +The ``traverse`` method is similar to ``sequence``, but it takes a sequence of ``A``s and applies a function from ``A`` to ``Future`` and returns a ``Future>``, enabling parallel ``map`` over the sequence, if you use ``Futures.future`` to create the ``Future``. .. code-block:: java import akka.dispatch.Future; import static akka.dispatch.Futures.traverse; import static akka.dispatch.Futures.future; - import java.util.LinkedList; + import java.lang.Iterable; import akka.japi.Function; - LinkedList listStrings = ... //Just a list of Strings + Iterable listStrings = ... //Just a sequence of Strings - Future> result = traverse(listStrings, new Function>(){ + Future> result = traverse(listStrings, new Function>(){ public Future apply(final String r) { return future(new Callable() { public String call() { @@ -214,7 +214,7 @@ The ``traverse`` method is similar to ``sequence``, but it takes a sequence of ` } }); - result.get(); //Returns a the list of strings as upper case + result.get(); //Returns the sequence of strings as upper case It's as simple as that! @@ -224,7 +224,7 @@ Then there's a method that's called ``fold`` that takes a start-value, a sequenc import akka.dispatch.Future; import static akka.dispatch.Futures.fold; - import java.util.Iterable; + import java.lang.Iterable; import akka.japi.Function2; Iterable> futures = ... //A sequence of Futures, in this case Strings