@Override public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) { Objects.requireNonNull(mapper); return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Double> opWrapSink(int flags, Sink<U> sink) { return new Sink.ChainedDouble<U>(sink) { @Override public void accept(double t) { downstream.accept(mapper.apply(t)); } }; } }; }
@Override public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) { return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override Sink<Double> opWrapSink(int flags, Sink<Double> sink) { return new Sink.ChainedDouble<Double>(sink) { @Override public void begin(long size) { downstream.begin(-1); } @Override public void accept(double t) { try (DoubleStream result = mapper.apply(t)) { // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it if (result != null) result.sequential().forEach(i -> downstream.accept(i)); } } }; } }; }
@Override public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) { Objects.requireNonNull(mapper); return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override Sink<Double> opWrapSink(int flags, Sink<Double> sink) { return new Sink.ChainedDouble<Double>(sink) { @Override public void begin(long size) { downstream.begin(-1); } @Override public void accept(double t) { try (DoubleStream result = mapper.apply(t)) { // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it if (result != null) result.sequential().forEach(i -> downstream.accept(i)); } } }; } }; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { // given final DoubleFunction<String> keyfunction = a -> "key"; try (final Cache<String, Integer> cache = Mockito.mock(Cache.class)) { final JCacheBasedDoubleToIntFunctionMemoizer<String> loader = new JCacheBasedDoubleToIntFunctionMemoizer<>( cache, keyfunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); // when thrown.expect(MemoizationException.class); // then loader.applyAsInt(123); } }
/** * */ @Test public void shouldMemoizeFunction() { // given final DoubleBinaryOperator function = (a, b) -> 123.456D; final DoubleBinaryFunction<String> keyFunction = (a, b) -> "key"; try (final Cache<String, Double> cache = JCacheMemoize.createCache(DoubleFunction.class)) { // when final JCacheBasedDoubleBinaryOperatorMemoizer<String> loader = new JCacheBasedDoubleBinaryOperatorMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123.456D, loader.applyAsDouble(123.456D, 789.123D), 0.0D); } }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Double, Double> cache = null; final DoubleConsumer consumer = System.out::println; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedDoubleConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * Generates a {@code Shape} to represent the data using the specified {@code interpolationLevel} and {@code colormap}. * * @param interpolationLevel an integer specifying the interpolation level to use. * @param colorMap an {@code IColorMap} to configure the surface. * @param colorMapRange a {@code Range} to create the surface color mapper. * @param scale a {@code DoubleFunction<Double>} to scale the data. * @return a new {@code Shape} with the surface for this data. */ public Shape generateSurface(int interpolationLevel, IColorMap colorMap, Range colorMapRange, DoubleFunction<Double> scale ) { final List<Coord3d> coords = dataToCoord3d( scale( scale, interpolate(data, interpolationLevel) ) ); final Shape surface = Builder.buildDelaunay(coords); surface.setColorMapper(new ColorMapper(colorMap, colorMapRange)); surface.setFaceDisplayed(true); surface.setWireframeDisplayed(false); return surface; }
/** * */ @Test public void shouldMemoizeConsumer() { // given final DoubleConsumer consumer = Mockito.mock(DoubleConsumer.class); final DoubleFunction<String> keyFunction = a -> "key"; try (final Cache<String, Double> cache = JCacheMemoize.createCache(DoubleConsumer.class)) { // when final JCacheBasedDoubleConsumerMemoizer<String> loader = new JCacheBasedDoubleConsumerMemoizer<>(cache, keyFunction, consumer); // then loader.accept(123.456D); Mockito.verify(consumer).accept(123.456D); } }
/** * */ @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 shouldRequireNonNullCache() { // given final ConcurrentMap<Double, Integer> cache = null; final DoubleToIntFunction 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 ConcurrentMapBasedDoubleToIntFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<String, String> cache = null; final DoubleFunction<String> function = input -> "output"; final DoubleFunction<String> keyFunction = input -> "key"; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedDoubleFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = input -> input; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsDouble(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", 123D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Double, Integer> cache = new ConcurrentHashMap<>(); final DoubleToIntFunction function = input -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToIntFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToIntFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsInt(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", 123, memoizer.viewCacheForTest().values().iterator().next().intValue()); }
/** * */ @Test public void shouldTestGivenValue() { // given final DoublePredicate predicate = Mockito.mock(DoublePredicate.class); final DoubleFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedDoublePredicateMemoizer<String> memoizer = new GuavaCacheBasedDoublePredicateMemoizer<>( cache, keyFunction, predicate); // then memoizer.test(123.456D); Mockito.verify(predicate).test(123.456D); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final DoublePredicate predicate = a -> true; final DoubleFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedDoublePredicateMemoizer<String> memoizer = new GuavaCacheBasedDoublePredicateMemoizer<>( cache, keyFunction, predicate); // when thrown.expect(MemoizationException.class); // then memoizer.test(123.456D); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Double, Boolean> cache = null; final DoublePredicate predicate = input -> true; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedDoublePredicateMemoizer<>(cache, keyFunction, predicate); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final DoubleFunction<Double> keyFunction = Double::valueOf; final Cache<Double, Double> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new GuavaCacheBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, null); // when thrown.expect(MemoizationException.class); // then memoizer.applyAsDouble(789D); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullConsumer() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleConsumer consumer = null; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Cannot memoize a NULL Consumer - provide an actual Consumer to fix this."); // then new ConcurrentMapBasedDoubleConsumerMemoizer<>(cache, keyFunction, consumer); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final DoubleFunction<Double> keyFunction = Double::valueOf; final Cache<Double, Integer> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedDoubleToIntFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToIntFunctionMemoizer<>( cache, keyFunction, null); // when thrown.expect(MemoizationException.class); // then memoizer.applyAsInt(789D); }
/** * */ @Test public void shouldUseCallWrappedOperator() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = Mockito.mock(DoubleUnaryOperator.class); final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsDouble(123D); Mockito.verify(operator).applyAsDouble(123D); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { // given final DoubleFunction<String> keyfunction = a -> "key"; try (final Cache<String, Double> cache = Mockito.mock(Cache.class)) { final JCacheBasedDoubleUnaryOperatorMemoizer<String> loader = new JCacheBasedDoubleUnaryOperatorMemoizer<>( cache, keyfunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); // when thrown.expect(MemoizationException.class); // then loader.applyAsDouble(123); } }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final DoubleFunction<String> keyFunction = a -> "key"; final DoubleConsumer consumer = System.out::println; final Cache<String, Double> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedDoubleConsumerMemoizer<String> memoizer = new GuavaCacheBasedDoubleConsumerMemoizer<>( cache, keyFunction, consumer); // when thrown.expect(MemoizationException.class); // then memoizer.accept(123.456D); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { // given final DoubleFunction<String> keyFunction = a -> "key"; try (final Cache<String, String> cache = Mockito.mock(Cache.class)) { final JCacheBasedDoubleFunctionMemoizer<String, String> loader = new JCacheBasedDoubleFunctionMemoizer<>( cache, keyFunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); // when thrown.expect(MemoizationException.class); // then loader.apply(123); } }
private <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper, int opFlags) { return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE, opFlags) { @Override Sink<Double> opWrapSink(int flags, Sink<U> sink) { return new Sink.ChainedDouble<U>(sink) { @Override public void accept(double t) { downstream.accept(mapper.apply(t)); } }; } }; }
/** * Creates an DOUBLE Printer that wraps the function provided * @param function the function to wrap * @return the newly created function Printer */ public static Printer<Double> forDouble(DoubleFunction<String> function) { return new Printer<Double>(FunctionStyle.DOUBLE, DEFAULT_NULL) { @Override public final String apply(double input) { return function.apply(input); } }; }
/** * Creates an LONG function that wraps to function provided * @param function the function to wrap * @param <O> the output type * @return the newly created function wrapper */ public static <O> Function2<Double,O> fromDouble(DoubleFunction<O> function) { return new Function2<Double,O>(FunctionStyle.DOUBLE) { @Override public final O apply(double input) { return function.apply(input); } }; }
JCacheBasedDoubleConsumerMemoizer( final Cache<KEY, Double> cache, final DoubleFunction<KEY> keyFunction, final DoubleConsumer consumer) { super(cache); this.keyFunction = keyFunction; this.consumer = consumer; }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndConsumer() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleConsumer consumer = System.out::println; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleConsumerMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleConsumerMemoizer<>( cache, keyFunction, consumer); // then Assert.assertNotNull("Memoizer is NULL", memoizer); }
/** * */ @Test public void shouldMemoizeDoubleUnaryOperatorWithKeyFunction() { // given final DoubleUnaryOperator operator = input -> 123.456D; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleUnaryOperator memoize = MapMemoize.doubleUnaryOperator(operator, keyFunction); // then Assert.assertNotNull("Memoized DoubleUnaryOperator is NULL", memoize); }
/** * */ @Test public void shouldMemoizeFunction() { // given final ConcurrentMap<Double, Integer> cache = new ConcurrentHashMap<>(); final DoubleToIntFunction function = input -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToIntFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToIntFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsInt(123.456D); }
/** * */ @Test public void shouldMemoizeOperator() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = input -> input; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsDouble(123.456D); }
/** * */ @Test public void shouldMemoizeDoubleFunction() { // given final DoubleFunction<String> function = a -> "test"; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleFunction<String> memoize = MapMemoize.doubleFunction(function, keyFunction); // then Assert.assertNotNull("Memoized DoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleFunction() { // given final DoubleFunction<String> function = a -> "test"; // when final DoubleFunction<String> memoize = MapMemoize.doubleFunction(function); // then Assert.assertNotNull("Memoized DoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithKeyFunction() { // given final DoubleToIntFunction operator = input -> 123; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleToIntFunction memoize = MapMemoize.doubleToIntFunction(operator, keyFunction); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }