1 package org.djunits.quantity;
2
3 import org.djunits.quantity.def.Quantity;
4 import org.djunits.unit.AbstractUnit;
5 import org.djunits.unit.UnitRuntimeException;
6 import org.djunits.unit.Unitless;
7 import org.djunits.unit.Units;
8 import org.djunits.unit.scale.IdentityScale;
9 import org.djunits.unit.scale.LinearScale;
10 import org.djunits.unit.scale.Scale;
11 import org.djunits.unit.si.SIUnit;
12 import org.djunits.unit.system.UnitSystem;
13
14 /**
15 * Speed is the rate of change of position over time.
16 * <p>
17 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18 * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
19 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
20 * @author Alexander Verbraeck
21 */
22 public class Speed extends Quantity<Speed, Speed.Unit>
23 {
24 /** Constant with value zero. */
25 public static final Speed ZERO = Speed.ofSi(0.0);
26
27 /** Constant with value one. */
28 public static final Speed ONE = Speed.ofSi(1.0);
29
30 /** Constant with value NaN. */
31 @SuppressWarnings("checkstyle:constantname")
32 public static final Speed NaN = Speed.ofSi(Double.NaN);
33
34 /** Constant with value POSITIVE_INFINITY. */
35 public static final Speed POSITIVE_INFINITY = Speed.ofSi(Double.POSITIVE_INFINITY);
36
37 /** Constant with value NEGATIVE_INFINITY. */
38 public static final Speed NEGATIVE_INFINITY = Speed.ofSi(Double.NEGATIVE_INFINITY);
39
40 /** Constant with value MAX_VALUE. */
41 public static final Speed POS_MAXVALUE = Speed.ofSi(Double.MAX_VALUE);
42
43 /** Constant with value -MAX_VALUE. */
44 public static final Speed NEG_MAXVALUE = Speed.ofSi(-Double.MAX_VALUE);
45
46 /** */
47 private static final long serialVersionUID = 600L;
48
49 /**
50 * Instantiate a Speed quantity with a unit.
51 * @param value the value, expressed in the unit
52 * @param unit the unit in which the value is expressed
53 */
54 public Speed(final double value, final Speed.Unit unit)
55 {
56 super(value, unit);
57 }
58
59 /**
60 * Instantiate a Speed quantity with a unit, expressed as a String.
61 * @param value the value, expressed in the unit
62 * @param abbreviation the String abbreviation of the unit in which the value is expressed
63 */
64 public Speed(final double value, final String abbreviation)
65 {
66 this(value, Units.resolve(Speed.Unit.class, abbreviation));
67 }
68
69 /**
70 * Construct Speed quantity.
71 * @param value Scalar from which to construct this instance
72 */
73 public Speed(final Speed value)
74 {
75 super(value.si(), Speed.Unit.SI);
76 setDisplayUnit(value.getDisplayUnit());
77 }
78
79 /**
80 * Return a Speed instance based on an SI value.
81 * @param si the si value
82 * @return the Speed instance based on an SI value
83 */
84 public static Speed ofSi(final double si)
85 {
86 return new Speed(si, Speed.Unit.SI);
87 }
88
89 @Override
90 public Speed instantiate(final double si)
91 {
92 return ofSi(si);
93 }
94
95 @Override
96 public SIUnit siUnit()
97 {
98 return Speed.Unit.SI_UNIT;
99 }
100
101 /**
102 * Returns a Speed representation of a textual representation of a value with a unit. The String representation that can be
103 * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
104 * but not required, between the value and the unit.
105 * @param text the textual representation to parse into a Speed
106 * @return the Scalar representation of the value in its unit
107 * @throws IllegalArgumentException when the text cannot be parsed
108 * @throws NullPointerException when the text argument is null
109 */
110 public static Speed valueOf(final String text)
111 {
112 return Quantity.valueOf(text, ZERO);
113 }
114
115 /**
116 * Returns a Speed based on a value and the textual representation of the unit, which can be localized.
117 * @param value the value to use
118 * @param unitString the textual representation of the unit
119 * @return the Scalar representation of the value in its unit
120 * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
121 * @throws NullPointerException when the unitString argument is null
122 */
123 public static Speed of(final double value, final String unitString)
124 {
125 return Quantity.of(value, unitString, ZERO);
126 }
127
128 /**
129 * Calculate the division of Speed and Speed, which results in a Dimensionless scalar.
130 * @param v scalar
131 * @return scalar as a division of Speed and Speed
132 */
133 public final Dimensionless divide(final Speed v)
134 {
135 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
136 }
137
138 /**
139 * Calculate the multiplication of Speed and Area, which results in a FlowVolume scalar.
140 * @param v scalar
141 * @return scalar as a multiplication of Speed and Area
142 */
143 public final FlowVolume multiply(final Area v)
144 {
145 return new FlowVolume(this.si() * v.si(), FlowVolume.Unit.SI);
146 }
147
148 /**
149 * Calculate the multiplication of Speed and Force, which results in a Power scalar.
150 * @param v scalar
151 * @return scalar as a multiplication of Speed and Force
152 */
153 public final Power multiply(final Force v)
154 {
155 return new Power(this.si() * v.si(), Power.Unit.SI);
156 }
157
158 /**
159 * Calculate the multiplication of Speed and Frequency, which results in a Acceleration scalar.
160 * @param v scalar
161 * @return scalar as a multiplication of Speed and Frequency
162 */
163 public final Acceleration multiply(final Frequency v)
164 {
165 return new Acceleration(this.si() * v.si(), Acceleration.Unit.SI);
166 }
167
168 /**
169 * Calculate the division of Speed and Length, which results in a Frequency scalar.
170 * @param v scalar
171 * @return scalar as a division of Speed and Length
172 */
173 public final Frequency divide(final Length v)
174 {
175 return new Frequency(this.si() / v.si(), Frequency.Unit.SI);
176 }
177
178 /**
179 * Calculate the division of Speed and Frequency, which results in a Length scalar.
180 * @param v scalar
181 * @return scalar as a division of Speed and Frequency
182 */
183 public final Length divide(final Frequency v)
184 {
185 return new Length(this.si() / v.si(), Length.Unit.SI);
186 }
187
188 /**
189 * Calculate the multiplication of Speed and LinearObjectDensity, which results in a Frequency scalar.
190 * @param v scalar
191 * @return scalar as a multiplication of Speed and LinearObjectDensity
192 */
193 public final Frequency multiply(final LinearObjectDensity v)
194 {
195 return new Frequency(this.si() * v.si(), Frequency.Unit.SI);
196 }
197
198 /**
199 * Calculate the multiplication of Speed and Duration, which results in a Length scalar.
200 * @param v scalar
201 * @return scalar as a multiplication of Speed and Duration
202 */
203 public final Length multiply(final Duration v)
204 {
205 return new Length(this.si() * v.si(), Length.Unit.SI);
206 }
207
208 /**
209 * Calculate the division of Speed and Duration, which results in a Acceleration scalar.
210 * @param v scalar
211 * @return scalar as a division of Speed and Duration
212 */
213 public final Acceleration divide(final Duration v)
214 {
215 return new Acceleration(this.si() / v.si(), Acceleration.Unit.SI);
216 }
217
218 /**
219 * Calculate the division of Speed and Acceleration, which results in a Duration scalar.
220 * @param v scalar
221 * @return scalar as a division of Speed and Acceleration
222 */
223 public final Duration divide(final Acceleration v)
224 {
225 return new Duration(this.si() / v.si(), Duration.Unit.SI);
226 }
227
228 /**
229 * Calculate the multiplication of Speed and FlowMass, which results in a Force scalar.
230 * @param v scalar
231 * @return scalar as a multiplication of Speed and FlowMass
232 */
233 public final Force multiply(final FlowMass v)
234 {
235 return new Force(this.si() * v.si(), Force.Unit.SI);
236 }
237
238 /**
239 * Calculate the multiplication of Speed and Mass, which results in a Momentum scalar.
240 * @param v scalar
241 * @return scalar as a multiplication of Speed and Mass
242 */
243 public final Momentum multiply(final Mass v)
244 {
245 return new Momentum(this.si() * v.si(), Momentum.Unit.SI);
246 }
247
248 /**
249 * Calculate the multiplication of Speed and Momentum, which results in a Energy scalar.
250 * @param v scalar
251 * @return scalar as a multiplication of Speed and Momentum
252 */
253 public final Energy multiply(final Momentum v)
254 {
255 return new Energy(this.si() * v.si(), Energy.Unit.SI);
256 }
257
258 /******************************************************************************************************/
259 /********************************************** UNIT CLASS ********************************************/
260 /******************************************************************************************************/
261
262 /**
263 * Speed.Unit encodes the units of the rate of change of a position over time.
264 * <p>
265 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
266 * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
267 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
268 * @author Alexander Verbraeck
269 */
270 @SuppressWarnings("checkstyle:constantname")
271 public static class Unit extends AbstractUnit<Speed.Unit, Speed>
272 {
273 /** The dimensions of Speed: m/s. */
274 public static final SIUnit SI_UNIT = SIUnit.of("m/s");
275
276 /** Meter per second. */
277 public static final Speed.Unit m_s =
278 new Speed.Unit("m/s", "m/s", "meter per second", IdentityScale.SCALE, UnitSystem.SI_DERIVED);
279
280 /** The SI or BASE unit. */
281 public static final Speed.Unit SI = m_s;
282
283 /** m/h. */
284 public static final Speed.Unit m_h =
285 new Speed.Unit("m/h", "m/h", "meter per hour", new LinearScale(1.0, 3600.0), UnitSystem.SI_ACCEPTED);
286
287 /** km/s. */
288 public static final Speed.Unit km_s =
289 new Speed.Unit("km/s", "km/s", "kilometer per second", new LinearScale(1000.0), UnitSystem.SI_ACCEPTED);
290
291 /** km/h. */
292 public static final Speed.Unit km_h =
293 new Speed.Unit("km/h", "km/h", "kilometer per hour", new LinearScale(1000.0, 3600.0), UnitSystem.SI_ACCEPTED);
294
295 /** in/s. */
296 public static final Speed.Unit in_s =
297 new Speed.Unit("in/s", "in/s", "inch per second", new LinearScale(Length.Unit.CONST_IN), UnitSystem.IMPERIAL);
298
299 /** in/min. */
300 public static final Speed.Unit in_min = new Speed.Unit("in/min", "in/min", "inch per minute",
301 new LinearScale(Length.Unit.CONST_IN, 60.0), UnitSystem.IMPERIAL);
302
303 /** in/h. */
304 public static final Speed.Unit in_h = new Speed.Unit("in/h", "in/h", "inch per hour",
305 new LinearScale(Length.Unit.CONST_IN, 3600.0), UnitSystem.IMPERIAL);
306
307 /** ft/s. */
308 public static final Speed.Unit ft_s =
309 new Speed.Unit("ft/s", "ft/s", "foot per second", new LinearScale(Length.Unit.CONST_FT), UnitSystem.IMPERIAL);
310
311 /** ft/min. */
312 public static final Speed.Unit ft_min = new Speed.Unit("ft/min", "ft/min", "foot per minute",
313 new LinearScale(Length.Unit.CONST_FT, 60.0), UnitSystem.IMPERIAL);
314
315 /** ft/h. */
316 public static final Speed.Unit ft_h = new Speed.Unit("ft/h", "ft/h", "foot per hour",
317 new LinearScale(Length.Unit.CONST_FT, 3600.0), UnitSystem.IMPERIAL);
318
319 /** mi/s. */
320 public static final Speed.Unit mi_s =
321 new Speed.Unit("mi/s", "mi/s", "mile per second", new LinearScale(Length.Unit.CONST_MI), UnitSystem.IMPERIAL);
322
323 /** mi/min. */
324 public static final Speed.Unit mi_min = new Speed.Unit("mi/min", "mi/min", "mile per minute",
325 new LinearScale(Length.Unit.CONST_MI, 60.0), UnitSystem.IMPERIAL);
326
327 /** mi/h. */
328 public static final Speed.Unit mi_h = new Speed.Unit("mi/h", "mi/h", "mile per hour",
329 new LinearScale(Length.Unit.CONST_MI, 3600.0), UnitSystem.IMPERIAL);
330
331 /** knot = Nautical Mile per hour. */
332 public static final Speed.Unit kt = new Speed.Unit("kt", "knot", Length.Unit.CONST_NM / 3600.0, UnitSystem.OTHER);
333
334 /**
335 * Create a new Speed unit.
336 * @param id the id or main abbreviation of the unit
337 * @param name the full name of the unit
338 * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
339 * @param unitSystem the unit system such as SI or IMPERIAL
340 */
341 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
342 {
343 super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
344 }
345
346 /**
347 * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
348 * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
349 * @param displayAbbreviation the display abbreviation of the unit
350 * @param name the full name of the unit
351 * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
352 * @param unitSystem unit system, e.g. SI or Imperial
353 */
354 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
355 final UnitSystem unitSystem)
356 {
357 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
358 }
359
360 @Override
361 public SIUnit siUnit()
362 {
363 return SI_UNIT;
364 }
365
366 @Override
367 public Unit getBaseUnit()
368 {
369 return SI;
370 }
371
372 @Override
373 public Speed ofSi(final double si)
374 {
375 return Speed.ofSi(si);
376 }
377
378 @Override
379 public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
380 final double scaleFactor, final UnitSystem unitSystem)
381 {
382 if (getScale() instanceof LinearScale ls)
383 {
384 return new Speed.Unit(textualAbbreviation, displayAbbreviation, name,
385 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
386 }
387 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
388 }
389
390 }
391 }