Skip to content

Instantly share code, notes, and snippets.

@baconpat
Created March 18, 2010 01:18
Show Gist options
  • Save baconpat/335940 to your computer and use it in GitHub Desktop.
Save baconpat/335940 to your computer and use it in GitHub Desktop.
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