GuavaCacheBasedIntToLongFunctionMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Long> implements IntToLongFunction { @Override public long applyAsLong(final int value) { final KEY key = keyFunction.apply(value); return get(key, givenKey -> Long.valueOf(function.applyAsLong(value))).longValue(); } GuavaCacheBasedIntToLongFunctionMemoizer(
            final Cache<KEY, Long> cache,
            final IntFunction<KEY> keyFunction,
            final IntToLongFunction function); @Override long applyAsLong(final int value);  }
@Test public void shouldTransformInput() { final IntToLongFunction function = a -> 123L; final IntFunction<Integer> keyFunction = Integer::valueOf; final Cache<Integer, Long> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedIntToLongFunctionMemoizer<Integer> memoizer = new GuavaCacheBasedIntToLongFunctionMemoizer<>( cache, keyFunction, function); Assert.assertEquals("Memoized value does not match expectation", 123L, memoizer.applyAsLong(789)); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final IntFunction<Integer> keyFunction = Integer::valueOf; final Cache<Integer, Long> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedIntToLongFunctionMemoizer<Integer> memoizer = new GuavaCacheBasedIntToLongFunctionMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.applyAsLong(789); }