GuavaCacheBasedToLongFunctionMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Long> implements ToLongFunction<INPUT> { @Override public long applyAsLong(final INPUT value) { final KEY key = keyFunction.apply(value); return get(key, givenKey -> Long.valueOf(function.applyAsLong(value))).longValue(); } GuavaCacheBasedToLongFunctionMemoizer(
            final Cache<KEY, Long> cache,
            final Function<INPUT, KEY> keyFunction,
            final ToLongFunction<INPUT> function); @Override long applyAsLong(final INPUT value);  }
@Test public void shouldTransformInput() { final ToLongFunction<String> function = a -> 123; final Function<String, String> keyFunction = Function.identity(); final Cache<String, Long> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedToLongFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedToLongFunctionMemoizer<>( cache, keyFunction, function); Assert.assertEquals("Memoized value does not match expectation", 123, memoizer.applyAsLong("test")); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final Function<String, String> keyFunction = Function.identity(); final Cache<String, Long> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedToLongFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedToLongFunctionMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.applyAsLong("test"); }