本文整理汇总了Java中java.util.OptionalDouble.of方法的典型用法代码示例。如果您正苦于以下问题:Java OptionalDouble.of方法的具体用法?Java OptionalDouble.of怎么用?Java OptionalDouble.of使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.OptionalDouble
的用法示例。
在下文中一共展示了OptionalDouble.of方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: average
import java.util.OptionalDouble; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*
* @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.
*/
@Override
public final OptionalDouble average() {
/*
* 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, index 2 holds the number of values seen, index 3
* holds the simple sum.
*/
double[] avg = collect(() -> new double[4],
(ll, d) -> {
ll[2]++;
Collectors.sumWithCompensation(ll, d);
ll[3] += d;
},
(ll, rr) -> {
Collectors.sumWithCompensation(ll, rr[0]);
Collectors.sumWithCompensation(ll, rr[1]);
ll[2] += rr[2];
ll[3] += rr[3];
});
return avg[2] > 0
? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2])
: OptionalDouble.empty();
}
示例2: testEmpty
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test(groups = "unit")
public void testEmpty() {
OptionalDouble empty = OptionalDouble.empty();
OptionalDouble present = OptionalDouble.of(1.0);
// empty
assertTrue(empty.equals(empty));
assertTrue(empty.equals(OptionalDouble.empty()));
assertTrue(!empty.equals(present));
assertTrue(0 == empty.hashCode());
assertTrue(!empty.toString().isEmpty());
assertTrue(!empty.isPresent());
empty.ifPresent(v -> { fail(); });
assertEquals(2.0, empty.orElse(2.0));
assertEquals(2.0, empty.orElseGet(()-> 2.0));
}
示例3: serializeOptionalPrimitive
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test
public void serializeOptionalPrimitive() {
final OptionalPrimitiveTestEntity entity = new OptionalPrimitiveTestEntity();
entity.d = OptionalDouble.of(69.5);
entity.i = OptionalInt.of(69);
entity.l = OptionalLong.of(69L);
final VPackSlice vpack = vp.serialize(entity);
assertThat(vpack, is(notNullValue()));
assertThat(vpack.isObject(), is(true));
assertThat(vpack.get("d").isDouble(), is(true));
assertThat(vpack.get("d").getAsDouble(), is(69.5));
assertThat(vpack.get("i").isInteger(), is(true));
assertThat(vpack.get("i").getAsInt(), is(69));
assertThat(vpack.get("l").isInteger(), is(true));
assertThat(vpack.get("l").getAsLong(), is(69L));
}
示例4: from
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Encoding.Of
static OptionalDouble from(Object ddd) {
if (Double.isNaN((Double) ddd)) {
return OptionalDouble.empty();
}
return OptionalDouble.of((Double) ddd);
}
示例5: average
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Override
public final OptionalDouble average() {
long[] avg = collect(() -> new long[2],
(ll, i) -> {
ll[0]++;
ll[1] += i;
},
(ll, rr) -> {
ll[0] += rr[0];
ll[1] += rr[1];
});
return avg[0] > 0
? OptionalDouble.of((double) avg[1] / avg[0])
: OptionalDouble.empty();
}
示例6: parseDouble
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Nonnull
public static OptionalDouble parseDouble(@Nonnull String s) {
try {
return OptionalDouble.of(Double.parseDouble(s));
} catch (NumberFormatException e) {
return OptionalDouble.empty();
}
}
示例7: getDoubleProperty
import java.util.OptionalDouble; //导入方法依赖的package包/类
default OptionalDouble getDoubleProperty(String name) {
Optional<String> optValue = getProperty(name);
if (optValue.isPresent()) {
return OptionalDouble.of(Double.parseDouble(optValue.get()));
} else {
return OptionalDouble.empty();
}
}
示例8: toOptionalDouble
import java.util.OptionalDouble; //导入方法依赖的package包/类
private static OptionalDouble toOptionalDouble(final String value) {
try {
return OptionalDouble.of(Double.parseDouble(value));
} catch (NumberFormatException e) {
return OptionalDouble.empty();
}
}
示例9: getValue
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Override
public OptionalDouble getValue(final JavaType type, final ResultSet rs, final int columnIndex,
final PropertyMapperManager mapperManager)
throws SQLException {
double value = rs.getDouble(columnIndex);
if (!rs.wasNull()) {
return OptionalDouble.of(value);
} else {
return OptionalDouble.empty();
}
}
示例10: testPresent
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test(groups = "unit")
public void testPresent() {
OptionalDouble empty = OptionalDouble.empty();
OptionalDouble present = OptionalDouble.of(1.0);
// present
assertTrue(present.equals(present));
assertFalse(present.equals(OptionalDouble.of(0.0)));
assertTrue(present.equals(OptionalDouble.of(1.0)));
assertTrue(!present.equals(empty));
assertTrue(Double.hashCode(1.0) == present.hashCode());
assertFalse(present.toString().isEmpty());
assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
assertEquals(1.0, present.getAsDouble());
try {
present.ifPresent(v -> { throw new ObscureException(); });
fail();
} catch(ObscureException expected) {
}
assertEquals(1.0, present.orElse(2.0));
assertEquals(1.0, present.orElseGet(null));
assertEquals(1.0, present.orElseGet(()-> 2.0));
assertEquals(1.0, present.orElseGet(()-> 3.0));
assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
}
示例11: get
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Override
public OptionalDouble get() {
return hasValue ? OptionalDouble.of(value) : null;
}
示例12: get
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Encoding.Expose
OptionalDouble get() {
return present
? OptionalDouble.of(value)
: OptionalDouble.empty();
}
示例13: makeDouble
import java.util.OptionalDouble; //导入方法依赖的package包/类
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values, producing an optional double result.
*
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
private boolean empty;
private double state;
public void begin(long size) {
empty = true;
state = 0;
}
@Override
public void accept(double t) {
if (empty) {
empty = false;
state = t;
}
else {
state = operator.applyAsDouble(state, t);
}
}
@Override
public OptionalDouble get() {
return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
}
@Override
public void combine(ReducingSink other) {
if (!other.empty)
accept(other.state);
}
}
return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
示例14: findLast
import java.util.OptionalDouble; //导入方法依赖的package包/类
/**
* Returns the last element of the specified stream, or {@link OptionalDouble#empty} if the stream
* is empty.
*
* <p>Equivalent to {@code stream.reduce((a, b) -> b)}, but may perform significantly better. This
* method's runtime will be between O(log n) and O(n), performing better on <a
* href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>
* streams.
*
* @see DoubleStream#findFirst()
* @throws NullPointerException if the last element of the stream is null
*/
public static OptionalDouble findLast(DoubleStream stream) {
// findLast(Stream) does some allocation, so we might as well box some more
java.util.Optional<Double> boxedLast = findLast(stream.boxed());
return boxedLast.isPresent() ? OptionalDouble.of(boxedLast.get()) : OptionalDouble.empty();
}