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