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