JCacheBasedLongBinaryOperatorMemoizer extends AbstractJCacheBasedMemoizer<KEY, Long> implements LongBinaryOperator { @Override public long applyAsLong(final long left, final long right) { final KEY key = keyFunction.apply(left, right); return invoke(key, givenKey -> Long.valueOf(function.applyAsLong(left, right))).longValue(); } JCacheBasedLongBinaryOperatorMemoizer(
            final Cache<KEY, Long> cache,
            final LongBinaryFunction<KEY> keyFunction,
            final LongBinaryOperator function); @Override long applyAsLong(final long left, final long right);  }
@Test public void shouldMemoizeFunction() { final LongBinaryOperator function = (a, b) -> 123; final LongBinaryFunction<String> keyFunction = (a, b) -> "key"; try (final Cache<String, Long> cache = JCacheMemoize.createCache(DoubleFunction.class)) { final JCacheBasedLongBinaryOperatorMemoizer<String> loader = new JCacheBasedLongBinaryOperatorMemoizer<>( cache, keyFunction, function); Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsLong(123, 789)); } }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { final LongBinaryFunction<String> keyFunction = (a, b) -> "key"; try (final Cache<String, Long> cache = Mockito.mock(Cache.class)) { final JCacheBasedLongBinaryOperatorMemoizer<String> loader = new JCacheBasedLongBinaryOperatorMemoizer<>( cache, keyFunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.applyAsLong(123, 789); } }