GuavaCacheBasedIntBinaryOperatorMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Integer> implements IntBinaryOperator { @Override public int applyAsInt(final int left, final int right) { final KEY key = keyFunction.apply(left, right); return get(key, givenKey -> Integer.valueOf(function.applyAsInt(left, right))).intValue(); } GuavaCacheBasedIntBinaryOperatorMemoizer(
            final Cache<KEY, Integer> cache,
            final IntBinaryFunction<KEY> keyFunction,
            final IntBinaryOperator function); @Override int applyAsInt(final int left, final int right);  }
@Test public void shouldTransformInput() { final IntBinaryOperator function = (a, b) -> 123; final IntBinaryFunction<String> keyFunction = (a, b) -> "key"; final Cache<String, Integer> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedIntBinaryOperatorMemoizer<String> memoizer = new GuavaCacheBasedIntBinaryOperatorMemoizer<>( cache, keyFunction, function); Assert.assertEquals(123, memoizer.applyAsInt(123, 789)); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final IntBinaryFunction<String> keyFunction = (a, b) -> "key"; final Cache<String, Integer> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedIntBinaryOperatorMemoizer<String> memoizer = new GuavaCacheBasedIntBinaryOperatorMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.applyAsInt(123, 789); }