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