@Override public final LongStream mapToLong(DoubleToLongFunction mapper) { Objects.requireNonNull(mapper); return new LongPipeline.StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Double> opWrapSink(int flags, Sink<Long> sink) { return new Sink.ChainedDouble<Long>(sink) { @Override public void accept(double t) { downstream.accept(mapper.applyAsLong(t)); } }; } }; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Double, Long> cache = null; final DoubleToLongFunction function = input -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedDoubleToLongFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullFunction() { // given final ConcurrentMap<Double, Long> cache = new ConcurrentHashMap<>(); final DoubleToLongFunction function = null; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL DoubleToLongFunction - provide an actual DoubleToLongFunction to fix this."); // then new ConcurrentMapBasedDoubleToLongFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Double, Long> cache = new ConcurrentHashMap<>(); final DoubleToLongFunction function = input -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToLongFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToLongFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsLong(123D); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123D, memoizer.viewCacheForTest().keySet().iterator().next().doubleValue(), 0.0D); Assert.assertEquals("Memoization value does not match expectations", 123L, memoizer.viewCacheForTest().values().iterator().next().longValue()); }
/** * */ @Test public void shouldUseCallWrappedFunction() { // given final ConcurrentMap<Double, Long> cache = new ConcurrentHashMap<>(); final DoubleToLongFunction function = Mockito.mock(DoubleToLongFunction.class); final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToLongFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToLongFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsLong(123D); Mockito.verify(function).applyAsLong(123D); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithKeyFunction() { // given final DoubleToLongFunction function = a -> 123L; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleToLongFunction memoize = CaffeineMemoize.doubleToLongFunction(function, keyFunction); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunction() { // given final DoubleToLongFunction function = a -> 123L; // when final DoubleToLongFunction memoize = CaffeineMemoize.doubleToLongFunction(function); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithLambda() { // given // when final DoubleToLongFunction memoize = CaffeineMemoize.doubleToLongFunction(a -> 123L); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
GuavaCacheBasedDoubleToLongFunctionMemoizer( final Cache<KEY, Long> cache, final DoubleFunction<KEY> keyFunction, final DoubleToLongFunction function) { super(cache); this.keyFunction = keyFunction; this.function = function; }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithKeyFunction() { // given final DoubleToLongFunction function = a -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final DoubleToLongFunction memoize = GuavaMemoize.doubleToLongFunction(function, keyFunction); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptLoadingCache() { // given final DoubleToLongFunction function = a -> 123L; final DoubleFunction<Double> keyFunction = Double::valueOf; final Cache<Double, Long> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedDoubleToLongFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToLongFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final DoubleToLongFunction function = a -> 123L; final DoubleFunction<Double> keyFunction = Double::valueOf; final Cache<Double, Long> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedDoubleToLongFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToLongFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123L, memoizer.applyAsLong(789D)); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithLambda() { // given // when final DoubleToLongFunction memoize = GuavaMemoize.doubleToLongFunction(a -> 123); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunction() { // given final DoubleToLongFunction function = a -> 123; // when final DoubleToLongFunction memoize = GuavaMemoize.doubleToLongFunction(function); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
JCacheBasedDoubleToLongFunctionMemoizer( final Cache<KEY, Long> cache, final DoubleFunction<KEY> keyFunction, final DoubleToLongFunction biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeBiFunction() { // given final DoubleToLongFunction function = first -> 123; final DoubleFunction<String> keyfunction = a -> "key"; try (final Cache<String, Long> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { // when final JCacheBasedDoubleToLongFunctionMemoizer<String> loader = new JCacheBasedDoubleToLongFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsLong(123)); } }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithKeyFunction() { // given final DoubleToLongFunction function = a -> 123; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleToLongFunction memoize = JCacheMemoize.doubleToLongFunction(function, keyFunction); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithLambda() { // given // when final DoubleToLongFunction memoize = JCacheMemoize.doubleToLongFunction(a -> 123); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunction() { // given final DoubleToLongFunction function = a -> 123; // when final DoubleToLongFunction memoize = JCacheMemoize.doubleToLongFunction(function); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedDoubleToLongFunctionMemoizer( final ConcurrentMap<KEY, Long> cache, final DoubleFunction<KEY> keyFunction, final DoubleToLongFunction function) { super(cache); this.keyFunction = keyFunction; this.function = requireNonNull(function, "Cannot memoize a NULL DoubleToLongFunction - provide an actual DoubleToLongFunction to fix this."); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunction() { // given final DoubleToLongFunction operator = input -> 123L; // when final DoubleToLongFunction memoize = MapMemoize.doubleToLongFunction(operator); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithLambda() { // given // when final DoubleToLongFunction memoize = MapMemoize.doubleToLongFunction(input -> 123L); // then Assert.assertNotNull("Memoized DoubleToLongFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptCacheAndFunction() { // given final ConcurrentMap<Double, Long> cache = new ConcurrentHashMap<>(); final DoubleToLongFunction function = input -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToLongFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToLongFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull("Memoizer is NULL", memoizer); }
/** * */ @Test public void shouldMemoizeFunction() { // given final ConcurrentMap<Double, Long> cache = new ConcurrentHashMap<>(); final DoubleToLongFunction function = input -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToLongFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToLongFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsLong(123.456D); }
/** * */ @Test public void shouldUseReturnFunctionResult() { // given final ConcurrentMap<Double, Long> cache = new ConcurrentHashMap<>(); final DoubleToLongFunction function = input -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToLongFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToLongFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals(123L, memoizer.applyAsLong(123D)); }
/** * */ @Test public void shouldMemoizeDoubleToLongFunctionWithKeyFunction() { // given final DoubleToLongFunction operator = input -> 123L; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleToLongFunction memoize = MapMemoize.doubleToLongFunction(operator, keyFunction); // then Assert.assertNotNull("Memoized DoubleToLongFunction 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 DoubleToLongFunction thatReturnsOnCatch(final long defaultReturnValue) { return (final double v1) -> { try { return applyAsLongWithThrowable(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 DoubleToLongFunction thatUnsafelyThrowsUnchecked() throws E { return (final double v1) -> { try { return applyAsLongWithThrowable(v1); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); return 0; } }; }
@Test public void mapToLong() { DoubleToLongFunction f = d -> 1L; LongStream stream = this.parallelStreamSupportMock.mapToLong(f); verify(this.delegateMock).mapToLong(f); assertThat(stream, instanceOf(ParallelLongStreamSupport.class)); assertSame(ParallelLongStreamSupport.class.cast(stream).delegate, this.mappedLongDelegateMock); assertSame(ParallelLongStreamSupport.class.cast(stream).workerPool, this.workerPool); }
/** * @param function * @return */ public static DoubleToLongFunction asDoubleToLongFunction(final ThrowableDoubleToLongFunction function) { return t -> { long result; try { result = function.applyAsLong(t); } catch (Exception e) { throw new MonadicException(e); } return result; }; }
F(DoubleToLongFunction doubleToLongFunction) { this((Function<T, R>) new Function<Double, Long>() { @Override public Long apply(Double t) { return doubleToLongFunction.applyAsLong(t); } }); }
@Override public LongStream mapToLong(DoubleToLongFunction mapper) { return s.mapToLong(mapper); }