1 package org.djunits.value.vfloat.scalar;
2
3 import org.djunits.unit.AreaUnit;
4 import org.djunits.unit.DimensionlessUnit;
5 import org.djunits.unit.DurationUnit;
6 import org.djunits.unit.EnergyUnit;
7 import org.djunits.unit.LengthUnit;
8 import org.djunits.unit.LinearDensityUnit;
9 import org.djunits.unit.MoneyUnit;
10 import org.djunits.unit.PositionUnit;
11 import org.djunits.unit.SpeedUnit;
12 import org.djunits.unit.VolumeUnit;
13
14 /**
15 * Easy access methods for the %Type% FloatScalar. Instead of:
16 *
17 * <pre>
18 * FloatScalar.Rel<LengthUnit> value = new FloatScalar.Rel<LengthUnit>(100.0, LengthUnit.SI);
19 * </pre>
20 *
21 * we can now write:
22 *
23 * <pre>
24 * FloatLength value = new FloatLength(100.0, LengthUnit.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: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, 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 FloatLength extends AbstractFloatScalarRel<LengthUnit, FloatLength>
40 {
41 /** */
42 private static final long serialVersionUID = 20150901L;
43
44 /** constant with value zero. */
45 public static final FloatLength ZERO = new FloatLength(0.0f, LengthUnit.SI);
46
47 /** constant with value NaN. */
48 @SuppressWarnings("checkstyle:constantname")
49 public static final FloatLength NaN = new FloatLength(Float.NaN, LengthUnit.SI);
50
51 /** constant with value POSITIVE_INFINITY. */
52 public static final FloatLength POSITIVE_INFINITY = new FloatLength(Float.POSITIVE_INFINITY, LengthUnit.SI);
53
54 /** constant with value NEGATIVE_INFINITY. */
55 public static final FloatLength NEGATIVE_INFINITY = new FloatLength(Float.NEGATIVE_INFINITY, LengthUnit.SI);
56
57 /** constant with value MAX_VALUE. */
58 public static final FloatLength POS_MAXVALUE = new FloatLength(Float.MAX_VALUE, LengthUnit.SI);
59
60 /** constant with value -MAX_VALUE. */
61 public static final FloatLength NEG_MAXVALUE = new FloatLength(-Float.MAX_VALUE, LengthUnit.SI);
62
63 /**
64 * Construct FloatLength scalar.
65 * @param value float value
66 * @param unit unit for the float value
67 */
68 public FloatLength(final float value, final LengthUnit unit)
69 {
70 super(value, unit);
71 }
72
73 /**
74 * Construct FloatLength scalar.
75 * @param value Scalar from which to construct this instance
76 */
77 public FloatLength(final FloatLength value)
78 {
79 super(value);
80 }
81
82 /**
83 * Construct FloatLength scalar using a double value.
84 * @param value double value
85 * @param unit unit for the resulting float value
86 */
87 public FloatLength(final double value, final LengthUnit unit)
88 {
89 super((float) value, unit);
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public final FloatLength instantiateRel(final float value, final LengthUnit unit)
95 {
96 return new FloatLength(value, unit);
97 }
98
99 /**
100 * Construct FloatLength scalar.
101 * @param value float value in SI units
102 * @return the new scalar with the SI value
103 */
104 public static final FloatLength createSI(final float value)
105 {
106 return new FloatLength(value, LengthUnit.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 FloatPosition instantiateAbs(final float value, final PositionUnit unit)
116 {
117 return new FloatPosition(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 FloatLength interpolate(final FloatLength zero, final FloatLength one, final float ratio)
128 {
129 return new FloatLength(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 FloatPosition plus(final FloatPosition v)
138 {
139 PositionUnit 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 FloatLength max(final FloatLength r1, final FloatLength 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 FloatLength max(final FloatLength r1, final FloatLength r2, final FloatLength... rn)
162 {
163 FloatLength maxr = (r1.gt(r2)) ? r1 : r2;
164 for (FloatLength 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 FloatLength min(final FloatLength r1, final FloatLength 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 FloatLength min(final FloatLength r1, final FloatLength r2, final FloatLength... rn)
193 {
194 FloatLength minr = (r1.lt(r2)) ? r1 : r2;
195 for (FloatLength 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 FloatLength and FloatLength, which results in a FloatDimensionless scalar.
207 * @param v FloatLength scalar
208 * @return FloatDimensionless scalar as a division of FloatLength and FloatLength
209 */
210 public final FloatDimensionless divideBy(final FloatLength v)
211 {
212 return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
213 }
214
215 /**
216 * Calculate the multiplication of FloatLength and FloatLength, which results in a FloatArea scalar.
217 * @param v FloatLength scalar
218 * @return FloatArea scalar as a multiplication of FloatLength and FloatLength
219 */
220 public final FloatArea multiplyBy(final FloatLength v)
221 {
222 return new FloatArea(this.si * v.si, AreaUnit.SI);
223 }
224
225 /**
226 * Calculate the division of FloatLength and FloatLinearDensity, which results in a FloatArea scalar.
227 * @param v FloatLength scalar
228 * @return FloatArea scalar as a division of FloatLength and FloatLinearDensity
229 */
230 public final FloatArea divideBy(final FloatLinearDensity v)
231 {
232 return new FloatArea(this.si / v.si, AreaUnit.SI);
233 }
234
235 /**
236 * Calculate the division of FloatLength and FloatArea, which results in a FloatLinearDensity scalar.
237 * @param v FloatLength scalar
238 * @return FloatLinearDensity scalar as a division of FloatLength and FloatArea
239 */
240 public final FloatLinearDensity divideBy(final FloatArea v)
241 {
242 return new FloatLinearDensity(this.si / v.si, LinearDensityUnit.SI);
243 }
244
245 /**
246 * Calculate the multiplication of FloatLength and FloatArea, which results in a FloatVolume scalar.
247 * @param v FloatLength scalar
248 * @return FloatVolume scalar as a multiplication of FloatLength and FloatArea
249 */
250 public final FloatVolume multiplyBy(final FloatArea v)
251 {
252 return new FloatVolume(this.si * v.si, VolumeUnit.SI);
253 }
254
255 /**
256 * Calculate the multiplication of FloatLength and FloatForce, which results in a FloatEnergy scalar.
257 * @param v FloatLength scalar
258 * @return FloatEnergy scalar as a multiplication of FloatLength and FloatForce
259 */
260 public final FloatEnergy multiplyBy(final FloatForce v)
261 {
262 return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
263 }
264
265 /**
266 * Calculate the multiplication of FloatLength and FloatFrequency, which results in a FloatSpeed scalar.
267 * @param v FloatLength scalar
268 * @return FloatSpeed scalar as a multiplication of FloatLength and FloatFrequency
269 */
270 public final FloatSpeed multiplyBy(final FloatFrequency v)
271 {
272 return new FloatSpeed(this.si * v.si, SpeedUnit.SI);
273 }
274
275 /**
276 * Calculate the division of FloatLength and FloatDuration, which results in a FloatSpeed scalar.
277 * @param v FloatLength scalar
278 * @return FloatSpeed scalar as a division of FloatLength and FloatDuration
279 */
280 public final FloatSpeed divideBy(final FloatDuration v)
281 {
282 return new FloatSpeed(this.si / v.si, SpeedUnit.SI);
283 }
284
285 /**
286 * Calculate the division of FloatLength and FloatSpeed, which results in a FloatDuration scalar.
287 * @param v FloatLength scalar
288 * @return FloatDuration scalar as a division of FloatLength and FloatSpeed
289 */
290 public final FloatDuration divideBy(final FloatSpeed v)
291 {
292 return new FloatDuration(this.si / v.si, DurationUnit.SI);
293 }
294
295 /**
296 * Calculate the multiplication of FloatLength and FloatMoneyPerLength, which results in a FloatMoney scalar.
297 * @param v FloatLength scalar
298 * @return FloatMoney scalar as a multiplication of FloatLength and FloatMoneyPerLength
299 */
300 public final FloatMoney multiplyBy(final FloatMoneyPerLength v)
301 {
302 return new FloatMoney(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
303 }
304
305 }