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