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