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