@Override public final IntStream mapToInt(DoubleToIntFunction mapper) { Objects.requireNonNull(mapper); return new IntPipeline.StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Double> opWrapSink(int flags, Sink<Integer> sink) { return new Sink.ChainedDouble<Integer>(sink) { @Override public void accept(double t) { downstream.accept(mapper.applyAsInt(t)); } }; } }; }
/** * */ @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 shouldRequireNonNullFunction() { // given final ConcurrentMap<Double, Integer> cache = new ConcurrentHashMap<>(); final DoubleToIntFunction function = null; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL DoubleToIntFunction - provide an actual DoubleToIntFunction to fix this."); // then new ConcurrentMapBasedDoubleToIntFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @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 shouldUseCallWrappedFunction() { // given final ConcurrentMap<Double, Integer> cache = new ConcurrentHashMap<>(); final DoubleToIntFunction function = Mockito.mock(DoubleToIntFunction.class); final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleToIntFunctionMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleToIntFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsInt(123D); Mockito.verify(function).applyAsInt(123D); }
/** * Decodes the data into a multicolor image of * {@link BufferedImage#TYPE_INT_ARGB}. * <p> * This is similar to {@link #toImage()}, but is designed to allow greater * flexibility in the final image configuration. This processes data values * in parallel, providing the matrix value to the provided color function, * which responds with a packed integer in 0xAARRGGBB order that is applied * to the underlying image. This process is generally quite fast, even on * large images. * <p> * For a basic color picker implementation that hides some of the gory * details of this method, see {@link ColorChooser}. * * @param converter * the color generation function as described, which must be * non-interfering for parallel usage (as described). * @return the constructed image. */ public BufferedImage toImage(DoubleToIntFunction converter) { checkNotNull(converter, "converter cannot be null"); // generate an int based version of the input data, scaling as we go final int[] scaled = stream() .mapToInt(converter) .toArray(); // then make the image based on the streamed data // thanks to https://stackoverflow.com/questions/6319465#12062505 // for the idea of directly bypassing the Java weirdness around this // stuff and just copy the array contents directly BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); int[] imageData = ((DataBufferInt) image.getRaster().getDataBuffer()) .getData(); System.arraycopy(scaled, 0, imageData, 0, imageData.length); return image; }
public InterpolatingLongLongSampler(DoubleToIntFunction icdSource, int resolution, boolean hash) { this.f = icdSource; this.resolution = resolution; if (hash) { this.hash = new ThreadSafeHash(); } this.lut = precompute(); }
public InterpolatingIntLongSampler(DoubleToIntFunction icdSource, int resolution, boolean hash) { this.f = icdSource; this.resolution = resolution; if (hash) { this.hash = new ThreadSafeHash(); } this.lut = precompute(); }
public InterpolatingIntIntSampler(DoubleToIntFunction icdSource, int resolution, boolean hash) { this.f = icdSource; this.resolution = resolution; if (hash) { this.hash = new ThreadSafeHash(); } this.lut = precompute(); }
public InterpolatingLongIntSampler(DoubleToIntFunction icdSource, int resolution, boolean hash) { this.f = icdSource; this.resolution = resolution; if (hash) { this.hash = new ThreadSafeHash(); } this.lut = precompute(); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithKeyFunction() { // given final DoubleToIntFunction function = a -> 123; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleToIntFunction memoize = CaffeineMemoize.doubleToIntFunction(function, keyFunction); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunction() { // given final DoubleToIntFunction function = a -> 123; // when final DoubleToIntFunction memoize = CaffeineMemoize.doubleToIntFunction(function); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithLambda() { // given // when final DoubleToIntFunction memoize = CaffeineMemoize.doubleToIntFunction(a -> 123); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
GuavaCacheBasedDoubleToIntFunctionMemoizer( final Cache<KEY, Integer> cache, final DoubleFunction<KEY> keyFunction, final DoubleToIntFunction function) { super(cache); this.keyFunction = keyFunction; this.function = function; }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithKeyFunction() { // given final DoubleToIntFunction function = a -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final DoubleToIntFunction memoize = GuavaMemoize.doubleToIntFunction(function, keyFunction); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptLoadingCache() { // given final DoubleToIntFunction function = a -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; final Cache<Double, Integer> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedDoubleToIntFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToIntFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final DoubleToIntFunction function = a -> 123; final DoubleFunction<Double> keyFunction = Double::valueOf; final Cache<Double, Integer> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedDoubleToIntFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToIntFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123, memoizer.applyAsInt(789D)); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithLambda() { // given // when final DoubleToIntFunction memoize = GuavaMemoize.doubleToIntFunction(a -> 123); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunction() { // given final DoubleToIntFunction function = a -> 123; // when final DoubleToIntFunction memoize = GuavaMemoize.doubleToIntFunction(function); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
JCacheBasedDoubleToIntFunctionMemoizer( final Cache<KEY, Integer> cache, final DoubleFunction<KEY> keyFunction, final DoubleToIntFunction biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeBiFunction() { // given final DoubleToIntFunction function = first -> 123; final DoubleFunction<String> keyfunction = a -> "key"; try (final Cache<String, Integer> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { // when final JCacheBasedDoubleToIntFunctionMemoizer<String> loader = new JCacheBasedDoubleToIntFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsInt(123)); } }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithKeyFunction() { // given final DoubleToIntFunction function = a -> 123; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoubleToIntFunction memoize = JCacheMemoize.doubleToIntFunction(function, keyFunction); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithLambda() { // given // when final DoubleToIntFunction memoize = JCacheMemoize.doubleToIntFunction(a -> 123); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunction() { // given final DoubleToIntFunction function = a -> 123; // when final DoubleToIntFunction memoize = JCacheMemoize.doubleToIntFunction(function); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedDoubleToIntFunctionMemoizer( final ConcurrentMap<KEY, Integer> cache, final DoubleFunction<KEY> keyFunction, final DoubleToIntFunction function) { super(cache); this.keyFunction = keyFunction; this.function = requireNonNull(function, "Cannot memoize a NULL DoubleToIntFunction - provide an actual DoubleToIntFunction to fix this."); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunction() { // given final DoubleToIntFunction operator = input -> 123; // when final DoubleToIntFunction memoize = MapMemoize.doubleToIntFunction(operator); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoubleToIntFunctionWithLambda() { // given // when final DoubleToIntFunction memoize = MapMemoize.doubleToIntFunction(input -> 123); // then Assert.assertNotNull("Memoized DoubleToIntFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptCacheAndFunction() { // 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 Assert.assertNotNull("Memoizer is NULL", memoizer); }
/** * */ @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 shouldUseReturnFunctionResult() { // 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 Assert.assertEquals(123, memoizer.applyAsInt(123D)); }
/** * */ @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); }
/** * @param defaultReturnValue A value to return if any throwable is caught. * @return An interface that returns a default value if any exception occurs. */ default DoubleToIntFunction thatReturnsOnCatch(final int defaultReturnValue) { return (final double v1) -> { try { return applyAsIntWithThrowable(v1); } catch(final Throwable throwable) { return defaultReturnValue; } }; }