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