GuavaCacheBasedToIntFunctionMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Integer> implements ToIntFunction<INPUT> { @Override public int applyAsInt(final INPUT value) { final KEY key = keyFunction.apply(value); return get(key, givenKey -> Integer.valueOf(function.applyAsInt(value))).intValue(); } GuavaCacheBasedToIntFunctionMemoizer(
            final Cache<KEY, Integer> cache,
            final Function<INPUT, KEY> keyFunction,
            final ToIntFunction<INPUT> function); @Override int applyAsInt(final INPUT value);  }
@Test public void shouldTransformInput() { final ToIntFunction<String> function = a -> 123; final Function<String, String> keyFunction = Function.identity(); final Cache<String, Integer> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedToIntFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedToIntFunctionMemoizer<>( cache, keyFunction, function); Assert.assertEquals("Memoized value does not match expectation", 123, memoizer.applyAsInt("test")); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final Function<String, String> keyFunction = Function.identity(); final Cache<String, Integer> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedToIntFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedToIntFunctionMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.applyAsInt("test"); }