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