Java Code Examples for java.util.function.DoubleFunction#apply()

The following examples show how to use java.util.function.DoubleFunction#apply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DoublePipeline.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
@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));
                    }
                }
            };
        }
    };
}
 
Example 2
Source File: DoublePipeline.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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));
                    }
                }
            };
        }
    };
}
 
Example 3
Source File: DoublePipeline.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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));
                    }
                }
            };
        }
    };
}
 
Example 4
Source File: DoublePipeline.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@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));
                    }
                }
            };
        }
    };
}
 
Example 5
Source File: DoublePipeline.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
@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));
                    }
                }
            };
        }
    };
}
 
Example 6
Source File: BinarySearch.java    From Algorithms with MIT License 6 votes vote down vote up
public static double binarySearch(
    double lo, double hi, double target, DoubleFunction<Double> function) {

  if (hi <= lo) throw new IllegalArgumentException("hi should be greater than lo");

  double mid;
  do {

    // Find the middle point
    mid = (hi + lo) / 2.0;

    // Compute the value of our function for the middle point
    // Note that f can be any function not just the square root function
    double value = function.apply(mid);

    if (value > target) {
      hi = mid;
    } else {
      lo = mid;
    }

  } while ((hi - lo) > EPS);

  return mid;
}
 
Example 7
Source File: DoublePipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@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));
                    }
                }
            };
        }
    };
}
 
Example 8
Source File: OrcBatchReader.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <T> void readDoubleColumn(Object[] vals, int fieldIdx, DoubleColumnVector vector,
											int childCount, DoubleFunction<T> reader) {

	if (vector.isRepeating) { // fill complete column with first value
		if (vector.isNull[0]) {
			// fill vals with null values
			fillColumnWithRepeatingValue(vals, fieldIdx, null, childCount);
		} else {
			// read repeating non-null value by forwarding call
			readNonNullDoubleColumn(vals, fieldIdx, vector, childCount, reader);
		}
	} else {
		boolean[] isNullVector = vector.isNull;
		if (fieldIdx == -1) { // set as an object
			for (int i = 0; i < childCount; i++) {
				if (isNullVector[i]) {
					vals[i] = null;
				} else {
					vals[i] = reader.apply(vector.vector[i]);
				}
			}
		} else { // set as a field of Row
			Row[] rows = (Row[]) vals;
			for (int i = 0; i < childCount; i++) {
				if (isNullVector[i]) {
					rows[i].setField(fieldIdx, null);
				} else {
					rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
				}
			}
		}
	}
}
 
Example 9
Source File: Function2.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example 10
Source File: Printer.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example 11
Source File: Rotation2DTest.java    From commons-geometry with Apache License 2.0 5 votes vote down vote up
/** Check a rotation transform for consistency against a variety of points and rotation angles.
 * @param factory function used to create a rotation transform from an input angle
 * @param transformFn function that accepts the transform and a point and returns
 *      the transformed point
 */
private static <T extends EuclideanTransform<Vector2D>> void checkRotate(
        DoubleFunction<T> factory, BiFunction<T, Vector2D, Vector2D> transformFn) {

    // check zero
    T transform = factory.apply(0);
    EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, transformFn.apply(transform, Vector2D.ZERO), TEST_EPS);

    // check a variety of non-zero points
    EuclideanTestUtils.permuteSkipZero(-2, -2, 1, (x, y) -> {
        checkRotatePoint(Vector2D.of(x, y), factory, transformFn);
    });
}
 
Example 12
Source File: Rotation2DTest.java    From commons-geometry with Apache License 2.0 5 votes vote down vote up
/** Check a rotation transform for consistency when transforming a single point against a
 * variety of rotation angles.
 * @param pt point to transform
 * @param factory function used to create a rotation transform from an input angle
 * @param transformFn function that accepts the transform and a point and returns
 *      the transformed point
 */
private static <T extends EuclideanTransform<Vector2D>> void checkRotatePoint(
        Vector2D pt, DoubleFunction<T> factory, BiFunction<T, Vector2D, Vector2D> transformFn) {

    // arrange
    double limit = 4 * Math.PI;
    double inc = 0.25;

    Line line = Lines.fromPointAndDirection(Vector2D.ZERO, pt, TEST_PRECISION);

    T transform;
    Vector2D resultPt;
    Line resultLine;
    for (double angle = -limit; angle < limit; angle += inc) {
        transform = factory.apply(angle);

        // act
        resultPt = transformFn.apply(transform, pt);

        // assert
        // check that the norm is unchanged
        Assert.assertEquals(pt.norm(), resultPt.norm(), TEST_EPS);

        resultLine = Lines.fromPointAndDirection(Vector2D.ZERO, resultPt, TEST_PRECISION);
        double lineAngle = line.angle(resultLine);

        // check that the angle is what we expect
        Assert.assertEquals(PlaneAngleRadians.normalizeBetweenMinusPiAndPi(angle), lineAngle, TEST_EPS);
    }
}
 
Example 13
Source File: HistogramChartFactory.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
private static void addDPToList(List<DataPoint> list, int[] bins, int i, double binwidth,
    double min, double max, DoubleFunction<Double> function) {
  // adds a data point to the series
  double x = min + (binwidth / 2.0) + i * binwidth;
  if (function != null)
    x = function.apply(x);
  list.add(new SimpleDataPoint(x, bins[i]));
}
 
Example 14
Source File: HistogramChartFactory.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
private static void addDPToSeries(XYSeries series, int[] bins, int i, double binwidth, double min,
    double max, DoubleFunction<Double> function) {
  // adds a data point to the series
  double x = min + (binwidth / 2.0) + i * binwidth;
  if (function != null)
    x = function.apply(x);
  series.add(x, bins[i]);
}
 
Example 15
Source File: OrcBatchReader.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static <T> void readDoubleColumn(Object[] vals, int fieldIdx, DoubleColumnVector vector,
											int childCount, DoubleFunction<T> reader) {

	if (vector.isRepeating) { // fill complete column with first value
		if (vector.isNull[0]) {
			// fill vals with null values
			fillColumnWithRepeatingValue(vals, fieldIdx, null, childCount);
		} else {
			// read repeating non-null value by forwarding call
			readNonNullDoubleColumn(vals, fieldIdx, vector, childCount, reader);
		}
	} else {
		boolean[] isNullVector = vector.isNull;
		if (fieldIdx == -1) { // set as an object
			for (int i = 0; i < childCount; i++) {
				if (isNullVector[i]) {
					vals[i] = null;
				} else {
					vals[i] = reader.apply(vector.vector[i]);
				}
			}
		} else { // set as a field of Row
			Row[] rows = (Row[]) vals;
			for (int i = 0; i < childCount; i++) {
				if (isNullVector[i]) {
					rows[i].setField(fieldIdx, null);
				} else {
					rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
				}
			}
		}
	}
}
 
Example 16
Source File: DoublePipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                DoubleConsumer downstreamAsDouble = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsDouble);
                            }
                            else {
                                Spliterator.OfDouble s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}
 
Example 17
Source File: DoublePipeline.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@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) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                DoubleConsumer downstreamAsDouble = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsDouble);
                            }
                            else {
                                Spliterator.OfDouble s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}
 
Example 18
Source File: LinearlyScaledFunction.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
public LinearlyScaledFunction(final DoubleFunction<Double> baseFunction, final double x1, final double y1, final double x2, final double y2) {
	super();
	this.baseFunction = baseFunction;
	this.mapping = new AffineFunction(baseFunction.apply(x1), y1, baseFunction.apply(x2), y2);
}
 
Example 19
Source File: DoublePipeline.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@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) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                DoubleConsumer downstreamAsDouble = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsDouble);
                            }
                            else {
                                Spliterator.OfDouble s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}
 
Example 20
Source File: DoublePipeline.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@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) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                // cache the consumer to avoid creation on every accepted element
                DoubleConsumer downstreamAsDouble = downstream::accept;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstreamAsDouble);
                            }
                            else {
                                var s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}