JCacheBasedLongToDoubleFunctionMemoizer extends AbstractJCacheBasedMemoizer<KEY, Double> implements LongToDoubleFunction { @Override public double applyAsDouble(final long value) { final KEY key = keyFunction.apply(value); return invoke(key, givenKey -> Double.valueOf(biFunction.applyAsDouble(value))).doubleValue(); } JCacheBasedLongToDoubleFunctionMemoizer(
            final Cache<KEY, Double> cache,
            final LongFunction<KEY> keyFunction,
            final LongToDoubleFunction biFunction); @Override double applyAsDouble(final long value);  }
@Test public void shouldMemoizeBiFunction() { final LongToDoubleFunction function = first -> 123; final LongFunction<String> keyfunction = a -> "key"; try (final Cache<String, Double> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { final JCacheBasedLongToDoubleFunctionMemoizer<String> loader = new JCacheBasedLongToDoubleFunctionMemoizer<>( 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 LongFunction<String> keyfunction = a -> "key"; try (final Cache<String, Double> cache = Mockito.mock(Cache.class)) { final JCacheBasedLongToDoubleFunctionMemoizer<String> loader = new JCacheBasedLongToDoubleFunctionMemoizer<>( cache, keyfunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.applyAsDouble(123); } }