fix #64
[memoization.java.git] / memoization-guava / src / main / java / de / xn__ho_hia / memoization / guava / AbstractGuavaCacheBasedMemoizer.java
blobb5275ff1d572b34eabf627b2828190efd806f156
1 /*
2 * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
7 package de.xn__ho_hia.memoization.guava;
9 import java.util.concurrent.ExecutionException;
10 import java.util.function.Function;
12 import com.google.common.cache.Cache;
14 import de.xn__ho_hia.memoization.shared.MemoizationException;
16 abstract class AbstractGuavaCacheBasedMemoizer<KEY, VALUE> {
18 private final Cache<KEY, VALUE> cache;
20 protected AbstractGuavaCacheBasedMemoizer(final Cache<KEY, VALUE> cache) {
21 this.cache = cache;
24 protected final VALUE get(final KEY key, final Function<KEY, VALUE> function) {
25 try {
26 return cache.get(key, () -> function.apply(key));
27 } catch (final ExecutionException exception) {
28 throw new MemoizationException(exception);