1 package org.djunits.value.vdouble.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 Relative Length DoubleScalar. Instead of:
16 *
17 * <pre>
18 * DoubleScalar<LengthUnit> value = new DoubleScalar<LengthUnit>(100.0, LengthUnit.SI);
19 * </pre>
20 *
21 * we can now write:
22 *
23 * <pre>
24 * Length value = new Length(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 Length extends AbstractDoubleScalarRel<LengthUnit, Length>
40 {
41 /** */
42 private static final long serialVersionUID = 20150901L;
43
44 /** constant with value zero. */
45 public static final Length ZERO = new Length(0.0, LengthUnit.SI);
46
47 /** constant with value NaN. */
48 @SuppressWarnings("checkstyle:constantname")
49 public static final Length NaN = new Length(Double.NaN, LengthUnit.SI);
50
51 /** constant with value POSITIVE_INFINITY. */
52 public static final Length POSITIVE_INFINITY = new Length(Double.POSITIVE_INFINITY, LengthUnit.SI);
53
54 /** constant with value NEGATIVE_INFINITY. */
55 public static final Length NEGATIVE_INFINITY = new Length(Double.NEGATIVE_INFINITY, LengthUnit.SI);
56
57 /** constant with value MAX_VALUE. */
58 public static final Length POS_MAXVALUE = new Length(Double.MAX_VALUE, LengthUnit.SI);
59
60 /** constant with value -MAX_VALUE. */
61 public static final Length NEG_MAXVALUE = new Length(-Double.MAX_VALUE, LengthUnit.SI);
62
63 /**
64 * Construct Length scalar.
65 * @param value double value
66 * @param unit unit for the double value
67 */
68 public Length(final double value, final LengthUnit unit)
69 {
70 super(value, unit);
71 }
72
73 /**
74 * Construct Length scalar.
75 * @param value Scalar from which to construct this instance
76 */
77 public Length(final Length value)
78 {
79 super(value);
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 public final Length instantiateRel(final double value, final LengthUnit unit)
85 {
86 return new Length(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 Position instantiateAbs(final double value, final PositionUnit unit)
96 {
97 return new Position(value, unit);
98 }
99
100 /**
101 * Construct Length scalar.
102 * @param value double value in SI units
103 * @return the new scalar with the SI value
104 */
105 public static final Length createSI(final double value)
106 {
107 return new Length(value, LengthUnit.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 Length interpolate(final Length zero, final Length one, final double ratio)
118 {
119 return new Length(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 Position plus(final Position v)
128 {
129 PositionUnit 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 Length max(final Length r1, final Length 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 Length max(final Length r1, final Length r2, final Length... rn)
152 {
153 Length maxr = (r1.gt(r2)) ? r1 : r2;
154 for (Length 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 Length min(final Length r1, final Length 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 Length min(final Length r1, final Length r2, final Length... rn)
183 {
184 Length minr = (r1.lt(r2)) ? r1 : r2;
185 for (Length 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 Length and Length, which results in a Dimensionless scalar.
197 * @param v Length scalar
198 * @return Dimensionless scalar as a division of Length and Length
199 */
200 public final Dimensionless divideBy(final Length v)
201 {
202 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
203 }
204
205 /**
206 * Calculate the multiplication of Length and Length, which results in a Area scalar.
207 * @param v Length scalar
208 * @return Area scalar as a multiplication of Length and Length
209 */
210 public final Area multiplyBy(final Length v)
211 {
212 return new Area(this.si * v.si, AreaUnit.SI);
213 }
214
215 /**
216 * Calculate the division of Length and LinearDensity, which results in a Area scalar.
217 * @param v Length scalar
218 * @return Area scalar as a division of Length and LinearDensity
219 */
220 public final Area divideBy(final LinearDensity v)
221 {
222 return new Area(this.si / v.si, AreaUnit.SI);
223 }
224
225 /**
226 * Calculate the division of Length and Area, which results in a LinearDensity scalar.
227 * @param v Length scalar
228 * @return LinearDensity scalar as a division of Length and Area
229 */
230 public final LinearDensity divideBy(final Area v)
231 {
232 return new LinearDensity(this.si / v.si, LinearDensityUnit.SI);
233 }
234
235 /**
236 * Calculate the multiplication of Length and Area, which results in a Volume scalar.
237 * @param v Length scalar
238 * @return Volume scalar as a multiplication of Length and Area
239 */
240 public final Volume multiplyBy(final Area v)
241 {
242 return new Volume(this.si * v.si, VolumeUnit.SI);
243 }
244
245 /**
246 * Calculate the multiplication of Length and Force, which results in a Energy scalar.
247 * @param v Length scalar
248 * @return Energy scalar as a multiplication of Length and Force
249 */
250 public final Energy multiplyBy(final Force v)
251 {
252 return new Energy(this.si * v.si, EnergyUnit.SI);
253 }
254
255 /**
256 * Calculate the multiplication of Length and Frequency, which results in a Speed scalar.
257 * @param v Length scalar
258 * @return Speed scalar as a multiplication of Length and Frequency
259 */
260 public final Speed multiplyBy(final Frequency v)
261 {
262 return new Speed(this.si * v.si, SpeedUnit.SI);
263 }
264
265 /**
266 * Calculate the division of Length and Duration, which results in a Speed scalar.
267 * @param v Length scalar
268 * @return Speed scalar as a division of Length and Duration
269 */
270 public final Speed divideBy(final Duration v)
271 {
272 return new Speed(this.si / v.si, SpeedUnit.SI);
273 }
274
275 /**
276 * Calculate the division of Length and Speed, which results in a Duration scalar.
277 * @param v Length scalar
278 * @return Duration scalar as a division of Length and Speed
279 */
280 public final Duration divideBy(final Speed v)
281 {
282 return new Duration(this.si / v.si, DurationUnit.SI);
283 }
284
285 /**
286 * Calculate the multiplication of Length and MoneyPerLength, which results in a Money scalar.
287 * @param v Length scalar
288 * @return Money scalar as a multiplication of Length and MoneyPerLength
289 */
290 public final Money multiplyBy(final MoneyPerLength v)
291 {
292 return new Money(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
293 }
294
295 }