1 package org.djunits.value.vdouble.scalar;
2
3 import java.util.Locale;
4
5 import org.djunits.unit.DurationUnit;
6 import org.djunits.unit.TimeUnit;
7 import org.djunits.value.vdouble.scalar.base.DoubleScalarAbs;
8 import org.djutils.base.NumberParser;
9 import org.djutils.exceptions.Throw;
10
11 import jakarta.annotation.Generated;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2023-07-23T14:06:38.224104100Z")
31 public class Time extends DoubleScalarAbs<TimeUnit, Time, DurationUnit, Duration>
32 {
33
34 private static final long serialVersionUID = 20150901L;
35
36
37 public static final Time ZERO = new Time(0.0, TimeUnit.DEFAULT);
38
39
40
41
42
43
44 public Time(final double value, final TimeUnit unit)
45 {
46 super(value, unit);
47 }
48
49
50
51
52
53 public Time(final Time value)
54 {
55 super(value);
56 }
57
58 @Override
59 public final Time instantiateAbs(final double value, final TimeUnit unit)
60 {
61 return new Time(value, unit);
62 }
63
64 @Override
65 public final Duration instantiateRel(final double value, final DurationUnit unit)
66 {
67 return new Duration(value, unit);
68 }
69
70
71
72
73
74
75 public static final Time instantiateSI(final double value)
76 {
77 return new Time(value, TimeUnit.DEFAULT);
78 }
79
80
81
82
83
84
85
86
87 public static Time interpolate(final Time zero, final Time one, final double ratio)
88 {
89 return new Time(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
90 }
91
92
93
94
95
96
97
98 public static Time max(final Time a1, final Time a2)
99 {
100 return a1.gt(a2) ? a1 : a2;
101 }
102
103
104
105
106
107
108
109
110 public static Time max(final Time a1, final Time a2, final Time... an)
111 {
112 Time maxa = a1.gt(a2) ? a1 : a2;
113 for (Time a : an)
114 {
115 if (a.gt(maxa))
116 {
117 maxa = a;
118 }
119 }
120 return maxa;
121 }
122
123
124
125
126
127
128
129 public static Time min(final Time a1, final Time a2)
130 {
131 return a1.lt(a2) ? a1 : a2;
132 }
133
134
135
136
137
138
139
140
141 public static Time min(final Time a1, final Time a2, final Time... an)
142 {
143 Time mina = a1.lt(a2) ? a1 : a2;
144 for (Time a : an)
145 {
146 if (a.lt(mina))
147 {
148 mina = a;
149 }
150 }
151 return mina;
152 }
153
154
155
156
157
158
159
160
161
162
163 public static Time valueOf(final String text)
164 {
165 Throw.whenNull(text, "Error parsing Time: text to parse is null");
166 Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Time: empty text to parse");
167 try
168 {
169 NumberParser numberParser = new NumberParser().lenient().trailing();
170 double d = numberParser.parseDouble(text);
171 String unitString = text.substring(numberParser.getTrailingPosition()).trim();
172 TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
173 if (unit == null)
174 throw new IllegalArgumentException("Unit " + unitString + " not found");
175 return new Time(d, unit);
176 }
177 catch (Exception exception)
178 {
179 throw new IllegalArgumentException(
180 "Error parsing Time from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
181 exception);
182 }
183 }
184
185
186
187
188
189
190
191
192
193 public static Time of(final double value, final String unitString)
194 {
195 Throw.whenNull(unitString, "Error parsing Time: unitString is null");
196 Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Time: empty unitString");
197 TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
198 if (unit != null)
199 {
200 return new Time(value, unit);
201 }
202 throw new IllegalArgumentException("Error parsing Time with unit " + unitString);
203 }
204
205 }