JCacheBasedBiFunctionMemoizer extends AbstractJCacheBasedMemoizer<KEY, OUTPUT> implements BiFunction<FIRST, SECOND, OUTPUT> { @Override public OUTPUT apply(final FIRST first, final SECOND second) { final KEY key = keyFunction.apply(first, second); return invoke(key, givenKey -> biFunction.apply(first, second)); } JCacheBasedBiFunctionMemoizer(
            final Cache<KEY, OUTPUT> cache,
            final BiFunction<FIRST, SECOND, KEY> keyFunction,
            final BiFunction<FIRST, SECOND, OUTPUT> biFunction); @Override OUTPUT apply(final FIRST first, final SECOND second);  }
@Test public void shouldMemoizeBiFunction() { final BiFunction<String, String, String> function = (first, second) -> "test"; final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction(); try (final Cache<String, String> cache = JCacheMemoize.createCache(BiFunction.class)) { final JCacheBasedBiFunctionMemoizer<String, String, String, String> loader = new JCacheBasedBiFunctionMemoizer<>( cache, keyfunction, function); Assert.assertEquals("Memoized value does not match expectation", "test", loader.apply("first", "second")); } }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction(); try (final Cache<String, String> cache = Mockito.mock(Cache.class)) { final JCacheBasedBiFunctionMemoizer<String, String, String, String> loader = new JCacheBasedBiFunctionMemoizer<>( cache, keyfunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.apply("first", "second"); } }