Created
March 18, 2010 01:18
-
-
Save baconpat/335940 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LazyImmutableMap<K,V> extends ForwardingMap<K,V> { | |
public static class AccessException extends RuntimeException { | |
public AccessException(Throwable cause) { | |
super(cause); | |
} | |
} | |
private final FutureTask<Map<K, V>> task; | |
public LazyImmutableMap(final Callable<Map<K,V>> eval) { | |
task = new FutureTask<Map<K,V>>(new Callable<Map<K,V>>() { | |
public Map<K, V> call() throws Exception { | |
return ImmutableMap.copyOf(eval.call()); | |
} | |
}); | |
} | |
@Override | |
protected Map<K, V> delegate() { | |
task.run(); | |
try { | |
return task.get(); | |
} catch (InterruptedException e) { | |
throw new AccessException(e); | |
} catch (ExecutionException e) { | |
throw new AccessException(e.getCause()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment