GuavaCacheBasedIntFunctionMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, OUTPUT> implements IntFunction<OUTPUT> { @Override public OUTPUT apply(final int value) { final KEY key = keyFunction.apply(value); return get(key, givenKey -> function.apply(value)); } GuavaCacheBasedIntFunctionMemoizer(
            final Cache<KEY, OUTPUT> cache,
            final IntFunction<KEY> keyFunction,
            final IntFunction<OUTPUT> function); @Override OUTPUT apply(final int value);  }
@Test public void shouldTransformInput() { final IntFunction<String> function = a -> "test"; final IntFunction<String> keyFunction = a -> "key"; final Cache<String, String> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedIntFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedIntFunctionMemoizer<>( cache, keyFunction, function); Assert.assertEquals("test", memoizer.apply(123)); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final IntFunction<String> keyFunction = a -> "key"; final Cache<String, String> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedIntFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedIntFunctionMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.apply(123); }