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