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