question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cache.getUnchecked(K, Supplier), like get(K, Callable) but without the checked exception

See original GitHub issue

Original 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:open
  • Created 9 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
gissuebotcommented, Nov 1, 2014

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.

0reactions
gissuebotcommented, Nov 1, 2014

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guava cache and preserving checked exceptions
In order not to break something I need to preserve any thrown exception as is, without wrapping it. Current solution appears somewhat ugly:...
Read more >
LoadingCache (Guava: Google Core Libraries for Java 15.0 API)
If the cache loader associated with this cache is known not to throw checked exceptions, then prefer getUnchecked(K) over this method. Throws: ...
Read more >
Diff - 6d2ff1d^! - platform/external/guava - Git at Google
exceptionLoader ; -import static com.google.common.cache. ... alternates between calling getUnchecked and calling get, but an unchecked - // exception thrown ...
Read more >
Cache (Guava: Google Core Libraries for Java 11.0 API)
Cache entries are manually added using #get(K, Callable) or #put(K, V) , and ... Callable) , this method does not throw a checked...
Read more >
Java example - LocalCache.java - entry, override, timestamped
This example Java source code file (LocalCache.java) is included in the ... the fact that K is almost always passed to * Map.get(),...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found