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