The Artima Developer Community
Sponsored Link

Java Buzz Forum
Futures 3: Transparent Futures in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Futures 3: Transparent Futures in Java Posted: Jan 9, 2005 4:30 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Futures 3: Transparent Futures in Java
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

After looking at futures in Java, then transparent ones in Ruby, adding transparent futures in Java turned out to be pretty simple as well. Just added this method to the two evaluators (duplication bad, yes, but I don't feel like making a new factory dependent on an Evaluator, so =P):

public Object promise(Class k, Expression e) {
    final Future f = this.promise(e);
    return Proxy.newProxyInstance(k.getClassLoader(), new Class[] {k}, new InvocationHandler()
    {
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            return method.invoke(f.getResult(), objects);
        }
    });
}

Used like:

public void testSomething() throws Exception {
    final boolean called[] = { false };
    final Map map = (Map) lazy.promise(Map.class, new Expression() {
        public Object evaluate() throws Exception {
            called[0] = true;
            final Map map = new HashMap();
            map.put("a", "A");
            return map;
        }
    });

    assertEquals("A", map.get("a"));
    assertTrue(called[0]);
}

This only works by interfaces, but using AWProxy instead would be trivial =) If Apple would let me have a 1.5 JDK I might use generics instead, but alas...

Now, async and lazy futures are fun and all, but I think it is time to stop looking at how to implement and look at how to use.

Read: Futures 3: Transparent Futures in Java

Topic: A tale of a copycat site Previous Topic   Next Topic Topic: Autoboxing and NullPointerExceptions in Java 1.5

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use