本文整理汇总了Java中java.util.function.ToDoubleFunction.applyAsDouble方法的典型用法代码示例。如果您正苦于以下问题:Java ToDoubleFunction.applyAsDouble方法的具体用法?Java ToDoubleFunction.applyAsDouble怎么用?Java ToDoubleFunction.applyAsDouble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.function.ToDoubleFunction
的用法示例。
在下文中一共展示了ToDoubleFunction.applyAsDouble方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: summingDouble
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
/**
* Returns a {@code Collector} that produces the sum of a double-valued
* function applied to the input elements. If no elements are present,
* the result is 0.
*
* <p>The sum returned can vary depending upon the order in which
* values are recorded, due to accumulated rounding error in
* addition of values of differing magnitudes. Values sorted by increasing
* absolute magnitude tend to yield more accurate results. If any recorded
* value is a {@code NaN} or the sum is at any point a {@code NaN} then the
* sum will be {@code NaN}.
*
* @param <T> the type of the input elements
* @param mapper a function extracting the property to be summed
* @return a {@code Collector} that produces the sum of a derived property
*/
public static <T> Collector<T, ?, Double>
summingDouble(ToDoubleFunction<? super T> mapper) {
/*
* In the arrays allocated for the collect operation, index 0
* holds the high-order bits of the running sum, index 1 holds
* the low-order bits of the sum computed via compensated
* summation, and index 2 holds the simple sum used to compute
* the proper result if the stream contains infinite values of
* the same sign.
*/
return new CollectorImpl<>(
() -> new double[3],
(a, t) -> { sumWithCompensation(a, mapper.applyAsDouble(t));
a[2] += mapper.applyAsDouble(t);},
(a, b) -> { sumWithCompensation(a, b[0]);
a[2] += b[2];
return sumWithCompensation(a, b[1]); },
a -> computeFinalSum(a),
CH_NOID);
}
示例2: summingDouble
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
/**
* Returns a {@code Collector} that produces the sum of a double-valued
* function applied to the input elements. If no elements are present,
* the result is 0.
*
* <p>The sum returned can vary depending upon the order in which
* values are recorded, due to accumulated rounding error in
* addition of values of differing magnitudes. Values sorted by increasing
* absolute magnitude tend to yield more accurate results. If any recorded
* value is a {@code NaN} or the sum is at any point a {@code NaN} then the
* sum will be {@code NaN}.
*
* @param <T> the type of the input elements
* @param mapper a function extracting the property to be summed
* @return a {@code Collector} that produces the sum of a derived property
*/
public static <T> Collector<T, ?, Double>
summingDouble(ToDoubleFunction<? super T> mapper) {
/*
* In the arrays allocated for the collect operation, index 0
* holds the high-order bits of the running sum, index 1 holds
* the low-order bits of the sum computed via compensated
* summation, and index 2 holds the simple sum used to compute
* the proper result if the stream contains infinite values of
* the same sign.
*/
return new CollectorImpl<>(
() -> new double[3],
(a, t) -> { double val = mapper.applyAsDouble(t);
sumWithCompensation(a, val);
a[2] += val;},
(a, b) -> { sumWithCompensation(a, b[0]);
a[2] += b[2];
return sumWithCompensation(a, b[1]); },
a -> computeFinalSum(a),
CH_NOID);
}
示例3: newGauge
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
@Override
protected <T> io.micrometer.core.instrument.Gauge newGauge(Meter.Id id, T obj, ToDoubleFunction<T> f) {
final WeakReference<T> ref = new WeakReference<>(obj);
Gauge<Double> gauge = () -> {
T obj2 = ref.get();
return obj2 != null ? f.applyAsDouble(ref.get()) : Double.NaN;
};
registry.register(hierarchicalName(id), gauge);
return new DropwizardGauge(id, gauge);
}
示例4: motorSource
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
/**
* Creates a subsource for a specific motor in a drive base.
*
* @param source the source for the drive base data
* @param motorName the motor name (eg "Left Motor" or "Right Motor")
* @param getter the getter (eg {@code DifferentialDriveData::getLeftSpeed})
* @param setter the setter (eg {@code DifferentialDriveData::withLeftSpeed})
*/
protected DataSource<SpeedControllerData> motorSource(DataSource<T> source,
String motorName,
ToDoubleFunction<T> getter,
BiFunction<T, Double, T> setter) {
return new SubSource<>(
SpeedControllerType.Instance,
source,
d -> setter.apply(dataOrDefault.get(), d == null ? 0.0 : d.getValue()),
d -> new SpeedControllerData(motorName, d == null ? 0.0 : getter.applyAsDouble(d))
);
}
示例5: toDouble
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
/**
* Creates an DOUBLE mapper that wraps to function provided
* @param function the function to wrap
* @param <I> the input type
* @return the newly created mapper
*/
public static <I,O> Function1<I,Double> toDouble(ToDoubleFunction<I> function) {
return new Function1<I,Double>(FunctionStyle.DOUBLE) {
@Override
public final double applyAsDouble(I value) {
return function.applyAsDouble(value);
}
};
}
示例6: getDisembarkGoalDecider
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
/**
* Goal decider to find the best land tile to disembark a unit that
* is planning to attack a given target.
*
* The result must be:
* - Unoccupied
* - Have at least one unoccupied high-seas-connected neighbour
* - Favour the best natural defence of the alternatives
* - Favour a short journey to the target
* - Prioritize not landing next to a hostile fort/fortress.
*
* @param target The target {@code Tile}.
* @return A suitable {@code GoalDecider}.
*/
public static GoalDecider getDisembarkGoalDecider(final Tile target) {
final double NO_DANGER_BONUS = 1000.0;
return new GoalDecider() {
private double bestScore = -1.0;
private PathNode goal = null;
@Override
public PathNode getGoal() { return goal; }
@Override
public boolean hasSubGoals() { return true; }
@Override
public boolean check(Unit u, PathNode pathNode) {
final Tile tile = pathNode.getTile();
if (tile == null || !tile.isLand() || !tile.isEmpty()
|| tile.hasSettlement()) return false;
final Player owner = u.getOwner();
final Map map = u.getGame().getMap();
final Predicate<Tile> dockPred = t ->
t.isHighSeasConnected() && !t.isLand();
final Predicate<Tile> dangerPred = t -> {
Settlement settlement = t.getSettlement();
return (settlement != null
&& !owner.owns(settlement)
&& settlement.hasAbility(Ability.BOMBARD_SHIPS)
&& (owner.atWarWith(settlement.getOwner())
|| u.hasAbility(Ability.PIRACY)));
};
final ToDoubleFunction<Tile> tileScorer = cacheDouble(t ->
(t.getDefenceValue() / (1.0 + map.getDistance(target, t))
+ ((none(t.getSurroundingTiles(1, 1), dangerPred))
? NO_DANGER_BONUS : 0.0)));
Tile best = maximize(tile.getSurroundingTiles(1, 1), dockPred,
Comparator.comparingDouble(tileScorer));
double score;
if (best != null
&& (score = tileScorer.applyAsDouble(best)) > bestScore) {
bestScore = score;
goal = pathNode;
return true;
}
return false;
}
};
}
示例7: averagingDouble
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
/**
* Returns a {@code Collector} that produces the arithmetic mean of a double-valued
* function applied to the input elements. If no elements are present,
* the result is 0.
*
* <p>The average returned can vary depending upon the order in which
* values are recorded, due to accumulated rounding error in
* addition of values of differing magnitudes. Values sorted by increasing
* absolute magnitude tend to yield more accurate results. If any recorded
* value is a {@code NaN} or the sum is at any point a {@code NaN} then the
* average will be {@code NaN}.
*
* @implNote The {@code double} format can represent all
* consecutive integers in the range -2<sup>53</sup> to
* 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
* values, the divisor in the average computation will saturate at
* 2<sup>53</sup>, leading to additional numerical errors.
*
* @param <T> the type of the input elements
* @param mapper a function extracting the property to be summed
* @return a {@code Collector} that produces the sum of a derived property
*/
public static <T> Collector<T, ?, Double>
averagingDouble(ToDoubleFunction<? super T> mapper) {
/*
* In the arrays allocated for the collect operation, index 0
* holds the high-order bits of the running sum, index 1 holds
* the low-order bits of the sum computed via compensated
* summation, and index 2 holds the number of values seen.
*/
return new CollectorImpl<>(
() -> new double[4],
(a, t) -> { sumWithCompensation(a, mapper.applyAsDouble(t)); a[2]++; a[3]+= mapper.applyAsDouble(t);},
(a, b) -> { sumWithCompensation(a, b[0]); sumWithCompensation(a, b[1]); a[2] += b[2]; a[3] += b[3]; return a; },
a -> (a[2] == 0) ? 0.0d : (computeFinalSum(a) / a[2]),
CH_NOID);
}
示例8: averagingDouble
import java.util.function.ToDoubleFunction; //导入方法依赖的package包/类
/**
* Returns a {@code Collector} that produces the arithmetic mean of a double-valued
* function applied to the input elements. If no elements are present,
* the result is 0.
*
* <p>The average returned can vary depending upon the order in which
* values are recorded, due to accumulated rounding error in
* addition of values of differing magnitudes. Values sorted by increasing
* absolute magnitude tend to yield more accurate results. If any recorded
* value is a {@code NaN} or the sum is at any point a {@code NaN} then the
* average will be {@code NaN}.
*
* @implNote The {@code double} format can represent all
* consecutive integers in the range -2<sup>53</sup> to
* 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
* values, the divisor in the average computation will saturate at
* 2<sup>53</sup>, leading to additional numerical errors.
*
* @param <T> the type of the input elements
* @param mapper a function extracting the property to be averaged
* @return a {@code Collector} that produces the arithmetic mean of a
* derived property
*/
public static <T> Collector<T, ?, Double>
averagingDouble(ToDoubleFunction<? super T> mapper) {
/*
* In the arrays allocated for the collect operation, index 0
* holds the high-order bits of the running sum, index 1 holds
* the low-order bits of the sum computed via compensated
* summation, and index 2 holds the number of values seen.
*/
return new CollectorImpl<>(
() -> new double[4],
(a, t) -> { double val = mapper.applyAsDouble(t); sumWithCompensation(a, val); a[2]++; a[3]+= val;},
(a, b) -> { sumWithCompensation(a, b[0]); sumWithCompensation(a, b[1]); a[2] += b[2]; a[3] += b[3]; return a; },
a -> (a[2] == 0) ? 0.0d : (computeFinalSum(a) / a[2]),
CH_NOID);
}