MATSIM
OptionalTime.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * project: org.matsim.*
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2020 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** *
19  */
20 
21 package org.matsim.core.utils.misc;
22 
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;
28 
29 import com.google.common.base.Preconditions;
30 
34 public final class OptionalTime {
35  //TODO we could store an array of all "reasonable" times, e.g. 0 to 100_000 ???
36 
37  //cached values:
38  private static final OptionalTime UNDEFINED = new OptionalTime(Time.UNDEFINED_TIME);
39  private static final OptionalTime TIME_0 = new OptionalTime(0);
40 
41  public static void assertDefined(double seconds) {
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");
46  }
47  }
48 
54  public static OptionalTime defined(double seconds) {
55  if (seconds == 0) {
56  return TIME_0;
57  }
58  assertDefined(seconds);
59  return new OptionalTime(seconds);
60  }
61 
62  public static OptionalTime undefined() {
63  return UNDEFINED;
64  }
65 
66  public static OptionalTime zeroSeconds() {
67  return TIME_0;
68  }
69 
70  private final double seconds;
71 
72  private OptionalTime(double seconds) {
73  this.seconds = seconds;
74  }
75 
76  public double seconds() {
77  if (seconds == Time.UNDEFINED_TIME) {
78  throw new NoSuchElementException("Undefined time");
79  }
80  return seconds;
81  }
82 
83  public boolean isDefined() {
84  return seconds != Time.UNDEFINED_TIME;
85  }
86 
87  public boolean isUndefined() {
88  return seconds == Time.UNDEFINED_TIME;
89  }
90 
91  public double orElse(double other) {
92  return seconds != Time.UNDEFINED_TIME ? seconds : other;
93  }
94 
95  public double orElseGet(DoubleSupplier supplier) {
96  return seconds != Time.UNDEFINED_TIME ? seconds : supplier.getAsDouble();
97  }
98 
99  public <X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
100  if (seconds == Time.UNDEFINED_TIME) {
101  throw exceptionSupplier.get();
102  }
103  return seconds;
104  }
105 
106  public void ifDefined(DoubleConsumer action) {
107  if (seconds != Time.UNDEFINED_TIME) {
108  action.accept(seconds);
109  }
110  }
111 
112  public void ifDefinedOrElse(DoubleConsumer action, Runnable undefinedAction) {
113  if (seconds != Time.UNDEFINED_TIME) {
114  action.accept(seconds);
115  } else {
116  undefinedAction.run();
117  }
118  }
119 
120  public DoubleStream stream() {
121  return seconds != Time.UNDEFINED_TIME ? DoubleStream.of(seconds) : DoubleStream.empty();
122  }
123 
124  public OptionalTime or(OptionalTime optionalTime) {
125  Preconditions.checkNotNull(optionalTime);
126  return seconds != Time.UNDEFINED_TIME ? this : optionalTime;
127  }
128 
129  public OptionalTime or(Supplier<OptionalTime> supplier) {
130  Preconditions.checkNotNull(supplier);
131  return seconds != Time.UNDEFINED_TIME ? this : Preconditions.checkNotNull(supplier.get());
132  }
133 
134  @Override
135  public boolean equals(Object o) {
136  if (this == o) {
137  return true;
138  }
139  if (!(o instanceof OptionalTime)) {
140  return false;
141  }
142  return seconds == ((OptionalTime)o).seconds; // none of them is NaN
143  }
144 
145  @Override
146  public int hashCode() {
147  return Double.hashCode(seconds);
148  }
149 
150  @Override
151  public String toString() {
152  return seconds != Time.UNDEFINED_TIME ? String.format("OptionalTime[%s]", seconds) : "OptionalTime[UNDEFINED]";
153  }
154 }
static final OptionalTime UNDEFINED
void ifDefined(DoubleConsumer action)
void ifDefinedOrElse(DoubleConsumer action, Runnable undefinedAction)
static final OptionalTime TIME_0
double orElseGet(DoubleSupplier supplier)
OptionalTime or(Supplier< OptionalTime > supplier)
static OptionalTime defined(double seconds)
static void assertDefined(double seconds)
OptionalTime or(OptionalTime optionalTime)