I try to make a cache using MapMaker/CacheBuilder but I don't understand how to properly handle null values.
ConcurrentMap<Key, Graph> graphs = new MapMaker()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.makeComputingMap(
new Function<Key, Graph>() {
public Graph apply(Key key) {
return createExpensiveGraph(key);
}
});
If the method createExpensiveGraph returns a null value, then a NullpointerException is thrown.
I don't understand why the ComputingConcurrentHashMap throws a NPE instead of just returning a null value.
How to properly handle this ? Just catch the NPE and return null instead ?
Am I missing something ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…