1 package org.djunits.value.vfloat.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 %Type% FloatScalar. Instead of:
16 *
17 * <pre>
18 * FloatScalar.Rel<DurationUnit> value = new FloatScalar.Rel<DurationUnit>(100.0, DurationUnit.SI);
19 * </pre>
20 *
21 * we can now write:
22 *
23 * <pre>
24 * FloatDuration value = new FloatDuration(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 FloatDuration extends AbstractFloatScalarRel<DurationUnit, FloatDuration>
40 {
41 /** */
42 private static final long serialVersionUID = 20150901L;
43
44 /** constant with value zero. */
45 public static final FloatDuration ZERO = new FloatDuration(0.0f, DurationUnit.SI);
46
47 /** constant with value NaN. */
48 @SuppressWarnings("checkstyle:constantname")
49 public static final FloatDuration NaN = new FloatDuration(Float.NaN, DurationUnit.SI);
50
51 /** constant with value POSITIVE_INFINITY. */
52 public static final FloatDuration POSITIVE_INFINITY = new FloatDuration(Float.POSITIVE_INFINITY, DurationUnit.SI);
53
54 /** constant with value NEGATIVE_INFINITY. */
55 public static final FloatDuration NEGATIVE_INFINITY = new FloatDuration(Float.NEGATIVE_INFINITY, DurationUnit.SI);
56
57 /** constant with value MAX_VALUE. */
58 public static final FloatDuration POS_MAXVALUE = new FloatDuration(Float.MAX_VALUE, DurationUnit.SI);
59
60 /** constant with value -MAX_VALUE. */
61 public static final FloatDuration NEG_MAXVALUE = new FloatDuration(-Float.MAX_VALUE, DurationUnit.SI);
62
63 /**
64 * Construct FloatDuration scalar.
65 * @param value float value
66 * @param unit unit for the float value
67 */
68 public FloatDuration(final float value, final DurationUnit unit)
69 {
70 super(value, unit);
71 }
72
73 /**
74 * Construct FloatDuration scalar.
75 * @param value Scalar from which to construct this instance
76 */
77 public FloatDuration(final FloatDuration value)
78 {
79 super(value);
80 }
81
82 /**
83 * Construct FloatDuration scalar using a double value.
84 * @param value double value
85 * @param unit unit for the resulting float value
86 */
87 public FloatDuration(final double value, final DurationUnit unit)
88 {
89 super((float) value, unit);
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public final FloatDuration instantiateRel(final float value, final DurationUnit unit)
95 {
96 return new FloatDuration(value, unit);
97 }
98
99 /**
100 * Construct FloatDuration scalar.
101 * @param value float value in SI units
102 * @return the new scalar with the SI value
103 */
104 public static final FloatDuration createSI(final float value)
105 {
106 return new FloatDuration(value, DurationUnit.SI);
107 }
108
109 /**
110 * Construct a new Absolute Immutable FloatScalar of the right type. Each extending class must implement this method.
111 * @param value the float value
112 * @param unit the unit
113 * @return A a new absolute instance of the FloatScalar of the right type
114 */
115 public final FloatTime instantiateAbs(final float value, final TimeUnit unit)
116 {
117 return new FloatTime(value, unit);
118 }
119
120 /**
121 * Interpolate between two values.
122 * @param zero the low value
123 * @param one the high value
124 * @param ratio the ratio between 0 and 1, inclusive
125 * @return a Scalar at the ratio between
126 */
127 public static FloatDuration interpolate(final FloatDuration zero, final FloatDuration one, final float ratio)
128 {
129 return new FloatDuration(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
130 }
131
132 /**
133 * Relative scalar plus Absolute scalar = Absolute scalar.
134 * @param v the value to add
135 * @return sum of this value and v as a new object
136 */
137 public final FloatTime plus(final FloatTime v)
138 {
139 TimeUnit targetUnit = v.getUnit();
140 return instantiateAbs(v.getInUnit() + getInUnit(targetUnit.getRelativeUnit()), targetUnit);
141 }
142
143 /**
144 * Return the maximum value of two relative scalars.
145 * @param r1 the first scalar
146 * @param r2 the second scalar
147 * @return the maximum value of two relative scalars
148 */
149 public static FloatDuration max(final FloatDuration r1, final FloatDuration r2)
150 {
151 return (r1.gt(r2)) ? r1 : r2;
152 }
153
154 /**
155 * Return the maximum value of more than two relative scalars.
156 * @param r1 the first scalar
157 * @param r2 the second scalar
158 * @param rn the other scalars
159 * @return the maximum value of more than two relative scalars
160 */
161 public static FloatDuration max(final FloatDuration r1, final FloatDuration r2, final FloatDuration... rn)
162 {
163 FloatDuration maxr = (r1.gt(r2)) ? r1 : r2;
164 for (FloatDuration r : rn)
165 {
166 if (r.gt(maxr))
167 {
168 maxr = r;
169 }
170 }
171 return maxr;
172 }
173
174 /**
175 * Return the minimum value of two relative scalars.
176 * @param r1 the first scalar
177 * @param r2 the second scalar
178 * @return the minimum value of two relative scalars
179 */
180 public static FloatDuration min(final FloatDuration r1, final FloatDuration r2)
181 {
182 return (r1.lt(r2)) ? r1 : r2;
183 }
184
185 /**
186 * Return the minimum value of more than two relative scalars.
187 * @param r1 the first scalar
188 * @param r2 the second scalar
189 * @param rn the other scalars
190 * @return the minimum value of more than two relative scalars
191 */
192 public static FloatDuration min(final FloatDuration r1, final FloatDuration r2, final FloatDuration... rn)
193 {
194 FloatDuration minr = (r1.lt(r2)) ? r1 : r2;
195 for (FloatDuration r : rn)
196 {
197 if (r.lt(minr))
198 {
199 minr = r;
200 }
201 }
202 return minr;
203 }
204
205 /**
206 * Calculate the division of FloatDuration and FloatDuration, which results in a FloatDimensionless scalar.
207 * @param v FloatDuration scalar
208 * @return FloatDimensionless scalar as a division of FloatDuration and FloatDuration
209 */
210 public final FloatDimensionless divideBy(final FloatDuration v)
211 {
212 return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
213 }
214
215 /**
216 * Calculate the multiplication of FloatDuration and FloatFrequency, which results in a FloatDimensionless scalar.
217 * @param v FloatDuration scalar
218 * @return FloatDimensionless scalar as a multiplication of FloatDuration and FloatFrequency
219 */
220 public final FloatDimensionless multiplyBy(final FloatFrequency v)
221 {
222 return new FloatDimensionless(this.si * v.si, DimensionlessUnit.SI);
223 }
224
225 /**
226 * Calculate the multiplication of FloatDuration and FloatElectricalCurrent, which results in a FloatElectricalCharge
227 * scalar.
228 * @param v FloatDuration scalar
229 * @return FloatElectricalCharge scalar as a multiplication of FloatDuration and FloatElectricalCurrent
230 */
231 public final FloatElectricalCharge multiplyBy(final FloatElectricalCurrent v)
232 {
233 return new FloatElectricalCharge(this.si * v.si, ElectricalChargeUnit.SI);
234 }
235
236 /**
237 * Calculate the multiplication of FloatDuration and FloatFlowMass, which results in a FloatMass scalar.
238 * @param v FloatDuration scalar
239 * @return FloatMass scalar as a multiplication of FloatDuration and FloatFlowMass
240 */
241 public final FloatMass multiplyBy(final FloatFlowMass v)
242 {
243 return new FloatMass(this.si * v.si, MassUnit.SI);
244 }
245
246 /**
247 * Calculate the multiplication of FloatDuration and FloatFlowVolume, which results in a FloatVolume scalar.
248 * @param v FloatDuration scalar
249 * @return FloatVolume scalar as a multiplication of FloatDuration and FloatFlowVolume
250 */
251 public final FloatVolume multiplyBy(final FloatFlowVolume v)
252 {
253 return new FloatVolume(this.si * v.si, VolumeUnit.SI);
254 }
255
256 /**
257 * Calculate the multiplication of FloatDuration and FloatAcceleration, which results in a FloatSpeed scalar.
258 * @param v FloatDuration scalar
259 * @return FloatSpeed scalar as a multiplication of FloatDuration and FloatAcceleration
260 */
261 public final FloatSpeed multiplyBy(final FloatAcceleration v)
262 {
263 return new FloatSpeed(this.si * v.si, SpeedUnit.SI);
264 }
265
266 /**
267 * Calculate the multiplication of FloatDuration and FloatPower, which results in a FloatEnergy scalar.
268 * @param v FloatDuration scalar
269 * @return FloatEnergy scalar as a multiplication of FloatDuration and FloatPower
270 */
271 public final FloatEnergy multiplyBy(final FloatPower v)
272 {
273 return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
274 }
275
276 /**
277 * Calculate the multiplication of FloatDuration and FloatSpeed, which results in a FloatLength scalar.
278 * @param v FloatDuration scalar
279 * @return FloatLength scalar as a multiplication of FloatDuration and FloatSpeed
280 */
281 public final FloatLength multiplyBy(final FloatSpeed v)
282 {
283 return new FloatLength(this.si * v.si, LengthUnit.SI);
284 }
285
286 /**
287 * Calculate the multiplication of FloatDuration and FloatMoneyPerDuration, which results in a FloatMoney scalar.
288 * @param v FloatDuration scalar
289 * @return FloatMoney scalar as a multiplication of FloatDuration and FloatMoneyPerDuration
290 */
291 public final FloatMoney multiplyBy(final FloatMoneyPerDuration v)
292 {
293 return new FloatMoney(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
294 }
295
296 }