Cache.getUnchecked(K, Supplier), like get(K, Callable) but without the checked exception
See original GitHub issueOriginal issue created by 0xabadea on 2013-01-08 at 11:45 AM
Since get() now only takes a Callable, get() must rethrow call()'s exception as an ExecutionException. But my implementation of call() does not throw a checked exception. The ExecutionException will never occur, yet I must catch it, making the code less elegant:
-----%<----- private static BigDecimal getMaxValue(final int precision, final int decimals) { String key = precision + “;” + decimals; try { return BIGEST_VALUE_CACHE.get(key, new Callable<BigDecimal>() {
@Override
public BigDecimal call() {
return ...;
}
});
} catch (ExecutionException e) {
// Should never occur.
throw new AssertionError(e);
}
}
-----%<-----
Perhaps a new method be introduced, such as (preferably)
get(K, Supplier<? extends V valueLoader);
or
get(K, Function<K, ? extends V> valueLoader);
Yes, I know about CacheLoader and getUnchecked(), but that would require me to introduce a new class for the cache key, which I was trying to avoid.
Issue Analytics
- State:
- Created 9 years ago
- Comments:7
Top GitHub Comments
Original comment posted by GuiSim on 2013-12-02 at 09:03 PM
Has a decision been reached for this feature request? I am currently facing a situation where I’d need a getUnchecked with a valueLoader parameter.
Original comment posted by kevinb@google.com on 2013-03-12 at 06:57 PM
Our next step is to review our existing callers of get(K, Callable) to see how many would noticeably benefit from a new getUnchecked(K, Supplier) method.
Status:
Research