JCacheBasedObjDoubleConsumerMemoizer extends AbstractJCacheBasedMemoizer<KEY, KEY> implements ObjDoubleConsumer<FIRST> { @Override public void accept(final FIRST first, final double value) { final KEY key = keyFunction.apply(first, value); invoke(key, givenKey -> { biConsumer.accept(first, value); return givenKey; }); } JCacheBasedObjDoubleConsumerMemoizer(
            final Cache<KEY, KEY> cache,
            final ObjDoubleFunction<FIRST, KEY> keyFunction,
            final ObjDoubleConsumer<FIRST> biConsumer); @Override void accept(final FIRST first, final double value);  }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldAcceptInput() { final ObjDoubleConsumer<String> biConsumer = Mockito.mock(ObjDoubleConsumer.class); final ObjDoubleFunction<String, String> keyfunction = (a, b) -> "key"; try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) { final JCacheBasedObjDoubleConsumerMemoizer<String, String> memoizer = new JCacheBasedObjDoubleConsumerMemoizer<>( cache, keyfunction, biConsumer); memoizer.accept("first", 123); Mockito.verify(biConsumer).accept("first", 123); } }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { final ObjDoubleFunction<String, String> keyfunction = (a, b) -> "key"; try (final Cache<String, String> cache = Mockito.mock(Cache.class)) { final JCacheBasedObjDoubleConsumerMemoizer<String, String> loader = new JCacheBasedObjDoubleConsumerMemoizer<>( cache, keyfunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.accept("first", 123); } }