@Override public final IntStream mapToInt(LongToIntFunction mapper) { Objects.requireNonNull(mapper); return new IntPipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Long> opWrapSink(int flags, Sink<Integer> sink) { return new Sink.ChainedLong<Integer>(sink) { @Override public void accept(long t) { downstream.accept(mapper.applyAsInt(t)); } }; } }; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Long, Integer> cache = null; final LongToIntFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedLongToIntFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullFunction() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = null; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL LongToIntFunction - provide an actual LongToIntFunction to fix this."); // then new ConcurrentMapBasedLongToIntFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToIntFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsInt(123L); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123L, memoizer.viewCacheForTest().keySet().iterator().next().longValue()); Assert.assertEquals("Memoization value does not match expectations", 123, memoizer.viewCacheForTest().values().iterator().next().intValue()); }
/** * */ @Test public void shouldUseCallWrappedFunction() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = Mockito.mock(LongToIntFunction.class); final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToIntFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsInt(123L); Mockito.verify(function).applyAsInt(123L); }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithKeyFunction() { // given final LongToIntFunction function = a -> 123; final LongFunction<String> keyFunction = a -> "key"; // when final LongToIntFunction memoize = CaffeineMemoize.longToIntFunction(function, keyFunction); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToIntFunction() { // given final LongToIntFunction function = a -> 123; // when final LongToIntFunction memoize = CaffeineMemoize.longToIntFunction(function); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithLambda() { // given // when final LongToIntFunction memoize = CaffeineMemoize.longToIntFunction(a -> 123); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
GuavaCacheBasedLongToIntFunctionMemoizer( final Cache<KEY, Integer> cache, final LongFunction<KEY> keyFunction, final LongToIntFunction function) { super(cache); this.keyFunction = keyFunction; this.function = function; }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithKeyFunction() { // given final LongToIntFunction function = a -> 123; final LongFunction<String> keyFunction = a -> "key"; // when final LongToIntFunction memoize = GuavaMemoize.longToIntFunction(function, keyFunction); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptLoadingCache() { // given final LongToIntFunction function = a -> 123; final LongFunction<Long> keyFunction = Long::valueOf; final Cache<Long, Integer> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedLongToIntFunctionMemoizer<Long> memoizer = new GuavaCacheBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final LongToIntFunction function = a -> 123; final LongFunction<Long> keyFunction = Long::valueOf; final Cache<Long, Integer> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedLongToIntFunctionMemoizer<Long> memoizer = new GuavaCacheBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123, memoizer.applyAsInt(789)); }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithLambda() { // given // when final LongToIntFunction memoize = GuavaMemoize.longToIntFunction(a -> 123); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToIntFunction() { // given final LongToIntFunction function = a -> 123; // when final LongToIntFunction memoize = GuavaMemoize.longToIntFunction(function); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
JCacheBasedLongToIntFunctionMemoizer( final Cache<KEY, Integer> cache, final LongFunction<KEY> keyFunction, final LongToIntFunction biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeBiFunction() { // given final LongToIntFunction function = first -> 123; final LongFunction<String> keyfunction = a -> "key"; try (final Cache<String, Integer> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { // when final JCacheBasedLongToIntFunctionMemoizer<String> loader = new JCacheBasedLongToIntFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsInt(123)); } }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithKeyFunction() { // given final LongToIntFunction function = a -> 123; final LongFunction<String> keyFunction = a -> "key"; // when final LongToIntFunction memoize = JCacheMemoize.longToIntFunction(function, keyFunction); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithLambda() { // given // when final LongToIntFunction memoize = JCacheMemoize.longToIntFunction(a -> 123); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToIntFunction() { // given final LongToIntFunction function = a -> 123; // when final LongToIntFunction memoize = JCacheMemoize.longToIntFunction(function); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedLongToIntFunctionMemoizer( final ConcurrentMap<KEY, Integer> cache, final LongFunction<KEY> keyFunction, final LongToIntFunction function) { super(cache); this.keyFunction = keyFunction; this.function = requireNonNull(function, "Cannot memoize a NULL LongToIntFunction - provide an actual LongToIntFunction to fix this."); }
/** * */ @Test public void shouldAcceptCacheAndFunction() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToIntFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull("Memoizer is NULL", memoizer); }
/** * */ @Test public void shouldMemoizeFunction() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToIntFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsInt(123L); }
/** * */ @Test public void shouldUseReturnFunctionResult() { // given final ConcurrentMap<Long, Integer> cache = new ConcurrentHashMap<>(); final LongToIntFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToIntFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToIntFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals(123, memoizer.applyAsInt(123L)); }
/** * */ @Test public void shouldMemoizeLongToIntFunction() { // given final LongToIntFunction operator = input -> 123; // when final LongToIntFunction memoize = MapMemoize.longToIntFunction(operator); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithLambda() { // given // when final LongToIntFunction memoize = MapMemoize.longToIntFunction(input -> 123); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToIntFunctionWithKeyFunction() { // given final LongToIntFunction operator = input -> 123; final LongFunction<String> keyFunction = a -> "key"; // when final LongToIntFunction memoize = MapMemoize.longToIntFunction(operator, keyFunction); // then Assert.assertNotNull("Memoized LongToIntFunction is NULL", memoize); }
/** * @param defaultReturnValue A value to return if any throwable is caught. * @return An interface that returns a default value if any exception occurs. */ default LongToIntFunction thatReturnsOnCatch(final int defaultReturnValue) { return (final long v1) -> { try { return applyAsIntWithThrowable(v1); } catch(final Throwable throwable) { return defaultReturnValue; } }; }
/** * @throws E if an exception E has been thrown, it is rethrown by this method * @return An interface that is only returned if no exception has been thrown. */ default LongToIntFunction thatUnsafelyThrowsUnchecked() throws E { return (final long v1) -> { try { return applyAsIntWithThrowable(v1); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); return 0; } }; }
@Test public void mapToInt() { LongToIntFunction f = i -> 1; IntStream stream = this.parallelStreamSupportMock.mapToInt(f); verify(this.delegateMock).mapToInt(f); assertThat(stream, instanceOf(ParallelIntStreamSupport.class)); assertSame(ParallelIntStreamSupport.class.cast(stream).delegate, this.mappedIntDelegateMock); assertSame(ParallelIntStreamSupport.class.cast(stream).workerPool, this.workerPool); }
/** * @param function * @return */ public static LongToIntFunction asLongToIntFunction(final ThrowableLongToIntFunction function) { return t -> { int result; try { result = function.applyAsInt(t); } catch (Exception e) { throw new MonadicException(e); } return result; }; }
@Override public IntStream mapToInt(LongToIntFunction mapper) { return s.mapToInt(mapper); }