How to map a Future (in Java)

2,394 views
Skip to first unread message

Stikkos

unread,
Oct 28, 2012, 7:33:45 AM10/28/12
to akka...@googlegroups.com
Hello,

I am trying to map a Future as defined on http://doc.akka.io/docs/akka/2.1-M2/java/futures.html

Basically, I just want to 'cast' my Future<Object> to a Future<Response> using the following code:

import akka.dispatch.Future;
import akka.japi.Function;
import akka.pattern.Patterns;

final Future<Object> wrappedFuture = Patterns.ask(matchedHandler, message, handleCommandTimeout);

final Future<Response> responseFuture = wrappedFuture.map(new Function<Object,Response>() {
public Response apply(Object source) {
Response response = null;
if(source instanceof Response){
response = (Response) source;
}
return response; 
});


The problem is that this doesn't compile.

The method map(Function1<Object,A>) in the type Future<Object> is not applicable for the arguments (new Function<Object,Response>(){})

Any idea how I should use the Future.map function?

Thanks!

√iktor Ҡlang

unread,
Oct 28, 2012, 10:09:38 AM10/28/12
to akka...@googlegroups.com
Hi Stikkos,

From the link you posted:

  1. Future<Integer> f2 = f1.map(new Mapper<String, Integer>() {
  2. public Integer apply(String s) {
  3. return s.length();
  4. }
  5. }, ec);

However, if you just want to make a typesafe cast, use "mapTo".

Cheers,

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user?hl=en.
 
 



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

√iktor Ҡlang

unread,
Oct 28, 2012, 10:16:39 AM10/28/12
to akka...@googlegroups.com
I also recommend upgrading from 2.1-M2 to 2.1.0-RC1 :-)

Cheers,

Stikkos

unread,
Oct 28, 2012, 11:00:37 AM10/28/12
to akka...@googlegroups.com
Thank you!

Could you give me a quick example of how to use the "mapTo" for a type-safe cast in Java or a link to the documentation for this method please?

Stikkos

unread,
Oct 28, 2012, 11:07:49 AM10/28/12
to akka...@googlegroups.com
I've found it ;-)

final Future<Response> responseFuture = wrappedFuture.mapTo(Util.manifest(Response.class));

√iktor Ҡlang

unread,
Oct 28, 2012, 11:10:19 AM10/28/12
to akka...@googlegroups.com
On Sun, Oct 28, 2012 at 4:07 PM, Stikkos <sti...@gmail.com> wrote:
I've found it ;-)

final Future<Response> responseFuture = wrappedFuture.mapTo(Util.manifest(Response.class));



Great, (in 2.1.0-RC1 it's Util.classTag(Response.class)) You can use a static import to make it look nicer:

import static akka.japi.Util.manifest;

final Future<Response> responseFuture = wrappedFuture.mapTo(manifest(Response.class));


Happy hAkking!

Reply all
Reply to author
Forward
0 new messages