本文整理汇总了Java中java.util.OptionalDouble.empty方法的典型用法代码示例。如果您正苦于以下问题:Java OptionalDouble.empty方法的具体用法?Java OptionalDouble.empty怎么用?Java OptionalDouble.empty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.OptionalDouble
的用法示例。
在下文中一共展示了OptionalDouble.empty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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();
}
示例3: 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(); });
AtomicBoolean emptyCheck = new AtomicBoolean();
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
assertTrue(emptyCheck.get());
try {
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
fail();
} catch (ObscureException expected) {
} catch (AssertionError e) {
throw e;
} catch (Throwable t) {
fail();
}
assertEquals(2.0, empty.orElse(2.0));
assertEquals(2.0, empty.orElseGet(()-> 2.0));
}
示例4: 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();
}
}
示例5: 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();
}
}
示例6: testEmptyGet
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
OptionalDouble empty = OptionalDouble.empty();
double got = empty.getAsDouble();
}
示例7: get
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Encoding.Expose
OptionalDouble get() {
return present
? OptionalDouble.of(value)
: OptionalDouble.empty();
}
示例8: testEmptyOrElseThrowNull
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void testEmptyOrElseThrowNull() throws Throwable {
OptionalDouble empty = OptionalDouble.empty();
double got = empty.orElseThrow(null);
}
示例9: PointProviderLocation
import java.util.OptionalDouble; //导入方法依赖的package包/类
public PointProviderLocation(World world, Vec3 position) {
super(world, position);
this.yaw = this.pitch = OptionalDouble.empty();
}
示例10: testEmptyOrElseThrow
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test(expectedExceptions=ObscureException.class)
public void testEmptyOrElseThrow() throws Exception {
OptionalDouble empty = OptionalDouble.empty();
double got = empty.orElseThrow(ObscureException::new);
}
示例11: testEmptyOrElseGetNull
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void testEmptyOrElseGetNull() {
OptionalDouble empty = OptionalDouble.empty();
double got = empty.orElseGet(null);
}
示例12: testEmpty
import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test
public void testEmpty() {
OptionalDoubleParameterMapper mapper = new OptionalDoubleParameterMapper();
OptionalDouble optional = OptionalDouble.empty();
assertThat(mapper.toJdbc(optional, null, null), is(nullValue()));
}
示例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();
}
示例15: makeDouble
import java.util.OptionalDouble; //导入方法依赖的package包/类
/**
* Constructs a {@code FindOp} for streams of doubles.
*
* @param mustFindFirst whether the {@code TerminalOp} must produce the
* first element in the encounter order
* @return a {@code TerminalOp} implementing the find operation
*/
public static TerminalOp<Double, OptionalDouble> makeDouble(boolean mustFindFirst) {
return new FindOp<>(mustFindFirst, StreamShape.DOUBLE_VALUE, OptionalDouble.empty(),
OptionalDouble::isPresent, FindSink.OfDouble::new);
}