@Override public final DoubleStream mapToDouble(LongToDoubleFunction mapper) { Objects.requireNonNull(mapper); return new DoublePipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Long> opWrapSink(int flags, Sink<Double> sink) { return new Sink.ChainedLong<Double>(sink) { @Override public void accept(long t) { downstream.accept(mapper.applyAsDouble(t)); } }; } }; }
@Override public double[] call() throws Exception { double[] output = new double[size]; LongToDoubleFunction mapper = RealDistributions.forSpec(mapperSpec); // System.out.println("resolved:" + mapper); // System.out.flush(); synchronized (signal) { signal.wait(10000); } for (int i = 0; i < output.length; i++) { output[i] = mapper.applyAsDouble(i); // if ((i % 100) == 0) { // System.out.println("wrote t:" + slot + ", iter:" + i + ", val:" + output[i]); // } } return output; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Long, Double> cache = null; final LongToDoubleFunction 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 ConcurrentMapBasedLongToDoubleFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullFunction() { // given final ConcurrentMap<Long, Double> cache = new ConcurrentHashMap<>(); final LongToDoubleFunction function = null; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL LongToDoubleFunction - provide an actual LongToDoubleFunction to fix this."); // then new ConcurrentMapBasedLongToDoubleFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Long, Double> cache = new ConcurrentHashMap<>(); final LongToDoubleFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToDoubleFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsDouble(123); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123, memoizer.viewCacheForTest().keySet().iterator().next().intValue()); Assert.assertEquals("Memoization value does not match expectations", 123D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
/** * */ @Test public void shouldUseCallWrappedFunction() { // given final ConcurrentMap<Long, Double> cache = new ConcurrentHashMap<>(); final LongToDoubleFunction function = Mockito.mock(LongToDoubleFunction.class); final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToDoubleFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsDouble(123); Mockito.verify(function).applyAsDouble(123); }
public static LongToDoubleFunction forSpec(String spec) { Optional<ResolvedFunction> resolvedFunction = new RealDistributions().resolveFunction(spec); return resolvedFunction .map(ResolvedFunction::getFunctionObject) .map(f -> ((LongToDoubleFunction) f)) .orElseThrow(() -> new RuntimeException("Invalid spec: " + spec)); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithKeyFunction() { // given final LongToDoubleFunction function = a -> 123.456D; final LongFunction<String> keyFunction = a -> "key"; // when final LongToDoubleFunction memoize = CaffeineMemoize.longToDoubleFunction(function, keyFunction); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunction() { // given final LongToDoubleFunction function = a -> 123.456D; // when final LongToDoubleFunction memoize = CaffeineMemoize.longToDoubleFunction(function); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithLambda() { // given // when final LongToDoubleFunction memoize = CaffeineMemoize.longToDoubleFunction(a -> 123.456D); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
GuavaCacheBasedLongToDoubleFunctionMemoizer( final Cache<KEY, Double> cache, final LongFunction<KEY> keyFunction, final LongToDoubleFunction function) { super(cache); this.keyFunction = keyFunction; this.function = function; }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithKeyFunction() { // given final LongToDoubleFunction function = a -> 123D; final LongFunction<String> keyFunction = a -> "key"; // when final LongToDoubleFunction memoize = GuavaMemoize.longToDoubleFunction(function, keyFunction); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithLambda() { // given // when final LongToDoubleFunction memoize = GuavaMemoize.longToDoubleFunction(a -> 123D); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptLoadingCache() { // given final LongToDoubleFunction function = a -> 123; final LongFunction<Long> keyFunction = Long::valueOf; final Cache<Long, Double> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedLongToDoubleFunctionMemoizer<Long> memoizer = new GuavaCacheBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final LongToDoubleFunction function = a -> 123.456D; final LongFunction<Long> keyFunction = Long::valueOf; final Cache<Long, Double> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedLongToDoubleFunctionMemoizer<Long> memoizer = new GuavaCacheBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123.456D, memoizer.applyAsDouble(789), 0.0D); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunction() { // given final LongToDoubleFunction function = a -> 123D; // when final LongToDoubleFunction memoize = GuavaMemoize.longToDoubleFunction(function); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
JCacheBasedLongToDoubleFunctionMemoizer( final Cache<KEY, Double> cache, final LongFunction<KEY> keyFunction, final LongToDoubleFunction biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithKeyFunction() { // given final LongToDoubleFunction function = a -> 123; final LongFunction<String> keyFunction = a -> "key"; // when final LongToDoubleFunction memoize = JCacheMemoize.longToDoubleFunction(function, keyFunction); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithLambda() { // given // when final LongToDoubleFunction memoize = JCacheMemoize.longToDoubleFunction(a -> 123); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeBiFunction() { // given final LongToDoubleFunction function = first -> 123; final LongFunction<String> keyfunction = a -> "key"; try (final Cache<String, Double> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { // when final JCacheBasedLongToDoubleFunctionMemoizer<String> loader = new JCacheBasedLongToDoubleFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123D, loader.applyAsDouble(123), 0.0D); } }
/** * */ @Test public void shouldMemoizeLongToDoubleFunction() { // given final LongToDoubleFunction function = a -> 123; // when final LongToDoubleFunction memoize = JCacheMemoize.longToDoubleFunction(function); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedLongToDoubleFunctionMemoizer( final ConcurrentMap<KEY, Double> cache, final LongFunction<KEY> keyFunction, final LongToDoubleFunction function) { super(cache); this.keyFunction = keyFunction; this.function = requireNonNull(function, "Cannot memoize a NULL LongToDoubleFunction - provide an actual LongToDoubleFunction to fix this."); }
/** * */ @Test public void shouldAcceptCacheAndFunction() { // given final ConcurrentMap<Long, Double> cache = new ConcurrentHashMap<>(); final LongToDoubleFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToDoubleFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull("Memoizer is NULL", memoizer); }
/** * */ @Test public void shouldMemoizeFunction() { // given final ConcurrentMap<Long, Double> cache = new ConcurrentHashMap<>(); final LongToDoubleFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToDoubleFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsDouble(123); }
/** * */ @Test public void shouldUseReturnFunctionResult() { // given final ConcurrentMap<Long, Double> cache = new ConcurrentHashMap<>(); final LongToDoubleFunction function = input -> 123; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongToDoubleFunctionMemoizer<Long> memoizer = new ConcurrentMapBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals(123D, memoizer.applyAsDouble(123), 0.0D); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunction() { // given final LongToDoubleFunction operator = input -> 123D; // when final LongToDoubleFunction memoize = MapMemoize.longToDoubleFunction(operator); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithLambda() { // given // when final LongToDoubleFunction memoize = MapMemoize.longToDoubleFunction(input -> 123D); // then Assert.assertNotNull("Memoized LongToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongToDoubleFunctionWithKeyFunction() { // given final LongToDoubleFunction operator = input -> 123D; final LongFunction<String> keyFunction = a -> "key"; // when final LongToDoubleFunction memoize = MapMemoize.longToDoubleFunction(operator, keyFunction); // then Assert.assertNotNull("Memoized LongToDoubleFunction 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 LongToDoubleFunction thatReturnsOnCatch(final double defaultReturnValue) { return (final long v1) -> { try { return applyAsDoubleWithThrowable(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 LongToDoubleFunction thatUnsafelyThrowsUnchecked() throws E { return (final long v1) -> { try { return applyAsDoubleWithThrowable(v1); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); return 0; } }; }
@Test public void mapToDouble() { LongToDoubleFunction f = i -> 1.0; DoubleStream stream = this.parallelStreamSupportMock.mapToDouble(f); verify(this.delegateMock).mapToDouble(f); assertThat(stream, instanceOf(ParallelDoubleStreamSupport.class)); assertSame(ParallelDoubleStreamSupport.class.cast(stream).delegate, this.mappedDoubleDelegateMock); assertSame(ParallelDoubleStreamSupport.class.cast(stream).workerPool, this.workerPool); }