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