21 package org.matsim.core.utils.misc;
23 import java.util.NoSuchElementException;
24 import java.util.function.DoubleConsumer;
25 import java.util.function.DoubleSupplier;
26 import java.util.function.Supplier;
27 import java.util.stream.DoubleStream;
29 import com.google.common.base.Preconditions;
42 if (seconds ==
Time.UNDEFINED_TIME) {
43 throw new IllegalArgumentException(
"Undefined time is not allowed");
44 }
else if (Double.isNaN(seconds)) {
45 throw new IllegalArgumentException(
"NaN time is not allowed");
77 if (seconds ==
Time.UNDEFINED_TIME) {
78 throw new NoSuchElementException(
"Undefined time");
84 return seconds !=
Time.UNDEFINED_TIME;
88 return seconds ==
Time.UNDEFINED_TIME;
92 return seconds !=
Time.UNDEFINED_TIME ?
seconds : other;
96 return seconds !=
Time.UNDEFINED_TIME ?
seconds : supplier.getAsDouble();
99 public <X extends Throwable>
double orElseThrow(Supplier<? extends X> exceptionSupplier)
throws X {
100 if (seconds ==
Time.UNDEFINED_TIME) {
101 throw exceptionSupplier.get();
107 if (seconds !=
Time.UNDEFINED_TIME) {
108 action.accept(seconds);
113 if (seconds !=
Time.UNDEFINED_TIME) {
114 action.accept(seconds);
116 undefinedAction.run();
121 return seconds !=
Time.UNDEFINED_TIME ? DoubleStream.of(seconds) : DoubleStream.empty();
125 Preconditions.checkNotNull(optionalTime);
126 return seconds !=
Time.UNDEFINED_TIME ? this : optionalTime;
130 Preconditions.checkNotNull(supplier);
131 return seconds !=
Time.UNDEFINED_TIME ? this : Preconditions.checkNotNull(supplier.get());
147 return Double.hashCode(seconds);
152 return seconds !=
Time.UNDEFINED_TIME ? String.format(
"OptionalTime[%s]", seconds) :
"OptionalTime[UNDEFINED]";
double orElse(double other)
static final OptionalTime UNDEFINED
void ifDefined(DoubleConsumer action)
void ifDefinedOrElse(DoubleConsumer action, Runnable undefinedAction)
static OptionalTime zeroSeconds()
static final OptionalTime TIME_0
double orElseGet(DoubleSupplier supplier)
OptionalTime or(Supplier< OptionalTime > supplier)
static OptionalTime defined(double seconds)
static void assertDefined(double seconds)
static OptionalTime undefined()
OptionalTime(double seconds)
OptionalTime or(OptionalTime optionalTime)