Skip to content

Instantly share code, notes, and snippets.

@oehme
Last active December 15, 2015 01:38
Show Gist options
  • Save oehme/5180925 to your computer and use it in GitHub Desktop.
Save oehme/5180925 to your computer and use it in GitHub Desktop.
Memoized fibonacci method - Generated Java code
@Memoize
public BigInteger fibonacci(final int n) {
try {
return fibonacci_cache.get(n);
} catch(Throwable e) {
throw Exceptions.sneakyThrow(e.getCause());
}
}
private BigInteger fibonacci_init(final int n) {
-- fibonacci algorithm here --
}
private LoadingCache<Integer,BigInteger> fibonacci_cache = CacheBuilder.newBuilder()
.build(new CacheLoader<Integer, BigInteger>() {
@Override
public BigInteger load(Integer key) throws Exception {
return fibonacci_init(key);
}
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment