GuavaCacheBasedLongConsumerMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Long> implements LongConsumer { @Override public void accept(final long value) { final KEY key = keyFunction.apply(value); get(key, givenKey -> { consumer.accept(value); return Long.valueOf(value); }); } GuavaCacheBasedLongConsumerMemoizer(
            final Cache<KEY, Long> cache,
            final LongFunction<KEY> keyFunction,
            final LongConsumer consumer); @Override void accept(final long value);  }
@Test public void shouldConsumeGivenValue() { final LongFunction<String> keyFunction = a -> "key"; final LongConsumer consumer = Mockito.mock(LongConsumer.class); final Cache<String, Long> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedLongConsumerMemoizer<String> memoizer = new GuavaCacheBasedLongConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept(123); Mockito.verify(consumer).accept(123); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final LongFunction<String> keyFunction = a -> "key"; final LongConsumer consumer = System.out::println; final Cache<String, Long> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedLongConsumerMemoizer<String> memoizer = new GuavaCacheBasedLongConsumerMemoizer<>( cache, keyFunction, consumer); thrown.expect(MemoizationException.class); memoizer.accept(123); }