1 package org.djunits.value.vdouble.scalar;
2
3 import org.djunits.unit.DimensionlessUnit;
4 import org.djunits.unit.DurationUnit;
5 import org.djunits.unit.ElectricalChargeUnit;
6 import org.djunits.unit.EnergyUnit;
7 import org.djunits.unit.LengthUnit;
8 import org.djunits.unit.MassUnit;
9 import org.djunits.unit.MoneyUnit;
10 import org.djunits.unit.SpeedUnit;
11 import org.djunits.unit.TimeUnit;
12 import org.djunits.unit.VolumeUnit;
13
14 /**
15 * Easy access methods for the Relative Duration DoubleScalar. Instead of:
16 *
17 * <pre>
18 * DoubleScalar<DurationUnit> value = new DoubleScalar<DurationUnit>(100.0, DurationUnit.SI);
19 * </pre>
20 *
21 * we can now write:
22 *
23 * <pre>
24 * Duration value = new Duration(100.0, DurationUnit.SI);
25 * </pre>
26 *
27 * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
28 * used are compatible.
29 * <p>
30 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
31 * All rights reserved. <br>
32 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
33 * <p>
34 * $LastChangedDate: 2015-12-22 04:32:39 +0100 (Tue, 22 Dec 2015) $, @version $Revision: 180 $, by $Author: averbraeck $,
35 * initial version Sep 1, 2015 <br>
36 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
37 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
38 */
39 public class Duration extends AbstractDoubleScalarRel<DurationUnit, Duration>
40 {
41 /** */
42 private static final long serialVersionUID = 20150901L;
43
44 /** constant with value zero. */
45 public static final Duration ZERO = new Duration(0.0, DurationUnit.SI);
46
47 /** constant with value NaN. */
48 @SuppressWarnings("checkstyle:constantname")
49 public static final Duration NaN = new Duration(Double.NaN, DurationUnit.SI);
50
51 /** constant with value POSITIVE_INFINITY. */
52 public static final Duration POSITIVE_INFINITY = new Duration(Double.POSITIVE_INFINITY, DurationUnit.SI);
53
54 /** constant with value NEGATIVE_INFINITY. */
55 public static final Duration NEGATIVE_INFINITY = new Duration(Double.NEGATIVE_INFINITY, DurationUnit.SI);
56
57 /** constant with value MAX_VALUE. */
58 public static final Duration POS_MAXVALUE = new Duration(Double.MAX_VALUE, DurationUnit.SI);
59
60 /** constant with value -MAX_VALUE. */
61 public static final Duration NEG_MAXVALUE = new Duration(-Double.MAX_VALUE, DurationUnit.SI);
62
63 /**
64 * Construct Duration scalar.
65 * @param value double value
66 * @param unit unit for the double value
67 */
68 public Duration(final double value, final DurationUnit unit)
69 {
70 super(value, unit);
71 }
72
73 /**
74 * Construct Duration scalar.
75 * @param value Scalar from which to construct this instance
76 */
77 public Duration(final Duration value)
78 {
79 super(value);
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 public final Duration instantiateRel(final double value, final DurationUnit unit)
85 {
86 return new Duration(value, unit);
87 }
88
89 /**
90 * Construct a new Absolute Immutable DoubleScalar of the right type. Each extending class must implement this method.
91 * @param value the double value
92 * @param unit the unit
93 * @return A a new absolute instance of the DoubleScalar of the right type
94 */
95 public final Time instantiateAbs(final double value, final TimeUnit unit)
96 {
97 return new Time(value, unit);
98 }
99
100 /**
101 * Construct Duration scalar.
102 * @param value double value in SI units
103 * @return the new scalar with the SI value
104 */
105 public static final Duration createSI(final double value)
106 {
107 return new Duration(value, DurationUnit.SI);
108 }
109
110 /**
111 * Interpolate between two values.
112 * @param zero the low value
113 * @param one the high value
114 * @param ratio the ratio between 0 and 1, inclusive
115 * @return a Scalar at the ratio between
116 */
117 public static Duration interpolate(final Duration zero, final Duration one, final double ratio)
118 {
119 return new Duration(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
120 }
121
122 /**
123 * Relative scalar plus Absolute scalar = Absolute scalar.
124 * @param v the value to add
125 * @return sum of this value and v as a new object
126 */
127 public final Time plus(final Time v)
128 {
129 TimeUnit targetUnit = v.getUnit();
130 return instantiateAbs(v.getInUnit() + getInUnit(targetUnit.getRelativeUnit()), targetUnit);
131 }
132
133 /**
134 * Return the maximum value of two relative scalars.
135 * @param r1 the first scalar
136 * @param r2 the second scalar
137 * @return the maximum value of two relative scalars
138 */
139 public static Duration max(final Duration r1, final Duration r2)
140 {
141 return (r1.gt(r2)) ? r1 : r2;
142 }
143
144 /**
145 * Return the maximum value of more than two relative scalars.
146 * @param r1 the first scalar
147 * @param r2 the second scalar
148 * @param rn the other scalars
149 * @return the maximum value of more than two relative scalars
150 */
151 public static Duration max(final Duration r1, final Duration r2, final Duration... rn)
152 {
153 Duration maxr = (r1.gt(r2)) ? r1 : r2;
154 for (Duration r : rn)
155 {
156 if (r.gt(maxr))
157 {
158 maxr = r;
159 }
160 }
161 return maxr;
162 }
163
164 /**
165 * Return the minimum value of two relative scalars.
166 * @param r1 the first scalar
167 * @param r2 the second scalar
168 * @return the minimum value of two relative scalars
169 */
170 public static Duration min(final Duration r1, final Duration r2)
171 {
172 return (r1.lt(r2)) ? r1 : r2;
173 }
174
175 /**
176 * Return the minimum value of more than two relative scalars.
177 * @param r1 the first scalar
178 * @param r2 the second scalar
179 * @param rn the other scalars
180 * @return the minimum value of more than two relative scalars
181 */
182 public static Duration min(final Duration r1, final Duration r2, final Duration... rn)
183 {
184 Duration minr = (r1.lt(r2)) ? r1 : r2;
185 for (Duration r : rn)
186 {
187 if (r.lt(minr))
188 {
189 minr = r;
190 }
191 }
192 return minr;
193 }
194
195 /**
196 * Calculate the division of Duration and Duration, which results in a Dimensionless scalar.
197 * @param v Duration scalar
198 * @return Dimensionless scalar as a division of Duration and Duration
199 */
200 public final Dimensionless divideBy(final Duration v)
201 {
202 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
203 }
204
205 /**
206 * Calculate the multiplication of Duration and Frequency, which results in a Dimensionless scalar.
207 * @param v Duration scalar
208 * @return Dimensionless scalar as a multiplication of Duration and Frequency
209 */
210 public final Dimensionless multiplyBy(final Frequency v)
211 {
212 return new Dimensionless(this.si * v.si, DimensionlessUnit.SI);
213 }
214
215 /**
216 * Calculate the multiplication of Duration and ElectricalCurrent, which results in a ElectricalCharge scalar.
217 * @param v Duration scalar
218 * @return ElectricalCharge scalar as a multiplication of Duration and ElectricalCurrent
219 */
220 public final ElectricalCharge multiplyBy(final ElectricalCurrent v)
221 {
222 return new ElectricalCharge(this.si * v.si, ElectricalChargeUnit.SI);
223 }
224
225 /**
226 * Calculate the multiplication of Duration and FlowMass, which results in a Mass scalar.
227 * @param v Duration scalar
228 * @return Mass scalar as a multiplication of Duration and FlowMass
229 */
230 public final Mass multiplyBy(final FlowMass v)
231 {
232 return new Mass(this.si * v.si, MassUnit.SI);
233 }
234
235 /**
236 * Calculate the multiplication of Duration and FlowVolume, which results in a Volume scalar.
237 * @param v Duration scalar
238 * @return Volume scalar as a multiplication of Duration and FlowVolume
239 */
240 public final Volume multiplyBy(final FlowVolume v)
241 {
242 return new Volume(this.si * v.si, VolumeUnit.SI);
243 }
244
245 /**
246 * Calculate the multiplication of Duration and Acceleration, which results in a Speed scalar.
247 * @param v Duration scalar
248 * @return Speed scalar as a multiplication of Duration and Acceleration
249 */
250 public final Speed multiplyBy(final Acceleration v)
251 {
252 return new Speed(this.si * v.si, SpeedUnit.SI);
253 }
254
255 /**
256 * Calculate the multiplication of Duration and Power, which results in a Energy scalar.
257 * @param v Duration scalar
258 * @return Energy scalar as a multiplication of Duration and Power
259 */
260 public final Energy multiplyBy(final Power v)
261 {
262 return new Energy(this.si * v.si, EnergyUnit.SI);
263 }
264
265 /**
266 * Calculate the multiplication of Duration and Speed, which results in a Length scalar.
267 * @param v Duration scalar
268 * @return Length scalar as a multiplication of Duration and Speed
269 */
270 public final Length multiplyBy(final Speed v)
271 {
272 return new Length(this.si * v.si, LengthUnit.SI);
273 }
274
275 /**
276 * Calculate the multiplication of Duration and MoneyPerDuration, which results in a Money scalar.
277 * @param v Duration scalar
278 * @return Money scalar as a multiplication of Duration and MoneyPerDuration
279 */
280 public final Money multiplyBy(final MoneyPerDuration v)
281 {
282 return new Money(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
283 }
284
285 }