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