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.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>
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 valueInUnit the value, expressed in the unit
52       * @param unit the unit in which the value is expressed
53       */
54      public Speed(final double valueInUnit, final Speed.Unit unit)
55      {
56          super(valueInUnit, unit);
57      }
58  
59      /**
60       * Instantiate a Speed quantity with a unit, expressed as a String.
61       * @param valueInUnit 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 valueInUnit, final String abbreviation)
65      {
66          this(valueInUnit, 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 instantiateSi(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 valueInUnit the value, expressed in the unit as given by unitString
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 valueInUnit, final String unitString)
124     {
125         return Quantity.of(valueInUnit, unitString, ZERO);
126     }
127 
128     @Override
129     public Speed.Unit getDisplayUnit()
130     {
131         return (Speed.Unit) super.getDisplayUnit();
132     }
133 
134     /**
135      * Calculate the division of Speed and Speed, which results in a Dimensionless scalar.
136      * @param v scalar
137      * @return scalar as a division of Speed and Speed
138      */
139     public final Dimensionless divide(final Speed v)
140     {
141         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
142     }
143 
144     /**
145      * Calculate the multiplication of Speed and Area, which results in a FlowVolume scalar.
146      * @param v scalar
147      * @return scalar as a multiplication of Speed and Area
148      */
149     public final FlowVolume multiply(final Area v)
150     {
151         return new FlowVolume(this.si() * v.si(), FlowVolume.Unit.SI);
152     }
153 
154     /**
155      * Calculate the multiplication of Speed and Force, which results in a Power scalar.
156      * @param v scalar
157      * @return scalar as a multiplication of Speed and Force
158      */
159     public final Power multiply(final Force v)
160     {
161         return new Power(this.si() * v.si(), Power.Unit.SI);
162     }
163 
164     /**
165      * Calculate the multiplication of Speed and Frequency, which results in a Acceleration scalar.
166      * @param v scalar
167      * @return scalar as a multiplication of Speed and Frequency
168      */
169     public final Acceleration multiply(final Frequency v)
170     {
171         return new Acceleration(this.si() * v.si(), Acceleration.Unit.SI);
172     }
173 
174     /**
175      * Calculate the division of Speed and Length, which results in a Frequency scalar.
176      * @param v scalar
177      * @return scalar as a division of Speed and Length
178      */
179     public final Frequency divide(final Length v)
180     {
181         return new Frequency(this.si() / v.si(), Frequency.Unit.SI);
182     }
183 
184     /**
185      * Calculate the division of Speed and Frequency, which results in a Length scalar.
186      * @param v scalar
187      * @return scalar as a division of Speed and Frequency
188      */
189     public final Length divide(final Frequency v)
190     {
191         return new Length(this.si() / v.si(), Length.Unit.SI);
192     }
193 
194     /**
195      * Calculate the multiplication of Speed and LinearObjectDensity, which results in a Frequency scalar.
196      * @param v scalar
197      * @return scalar as a multiplication of Speed and LinearObjectDensity
198      */
199     public final Frequency multiply(final LinearObjectDensity v)
200     {
201         return new Frequency(this.si() * v.si(), Frequency.Unit.SI);
202     }
203 
204     /**
205      * Calculate the multiplication of Speed and Duration, which results in a Length scalar.
206      * @param v scalar
207      * @return scalar as a multiplication of Speed and Duration
208      */
209     public final Length multiply(final Duration v)
210     {
211         return new Length(this.si() * v.si(), Length.Unit.SI);
212     }
213 
214     /**
215      * Calculate the division of Speed and Duration, which results in a Acceleration scalar.
216      * @param v scalar
217      * @return scalar as a division of Speed and Duration
218      */
219     public final Acceleration divide(final Duration v)
220     {
221         return new Acceleration(this.si() / v.si(), Acceleration.Unit.SI);
222     }
223 
224     /**
225      * Calculate the division of Speed and Acceleration, which results in a Duration scalar.
226      * @param v scalar
227      * @return scalar as a division of Speed and Acceleration
228      */
229     public final Duration divide(final Acceleration v)
230     {
231         return new Duration(this.si() / v.si(), Duration.Unit.SI);
232     }
233 
234     /**
235      * Calculate the multiplication of Speed and FlowMass, which results in a Force scalar.
236      * @param v scalar
237      * @return scalar as a multiplication of Speed and FlowMass
238      */
239     public final Force multiply(final FlowMass v)
240     {
241         return new Force(this.si() * v.si(), Force.Unit.SI);
242     }
243 
244     /**
245      * Calculate the multiplication of Speed and Mass, which results in a Momentum scalar.
246      * @param v scalar
247      * @return scalar as a multiplication of Speed and Mass
248      */
249     public final Momentum multiply(final Mass v)
250     {
251         return new Momentum(this.si() * v.si(), Momentum.Unit.SI);
252     }
253 
254     /**
255      * Calculate the multiplication of Speed and Momentum, which results in a Energy scalar.
256      * @param v scalar
257      * @return scalar as a multiplication of Speed and Momentum
258      */
259     public final Energy multiply(final Momentum v)
260     {
261         return new Energy(this.si() * v.si(), Energy.Unit.SI);
262     }
263 
264     /******************************************************************************************************/
265     /********************************************** UNIT CLASS ********************************************/
266     /******************************************************************************************************/
267 
268     /**
269      * Speed.Unit encodes the units of the rate of change of a position over time.
270      * <p>
271      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
272      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
273      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
274      * @author Alexander Verbraeck
275      */
276     @SuppressWarnings("checkstyle:constantname")
277     public static class Unit extends AbstractUnit<Speed.Unit, Speed>
278     {
279         /** The dimensions of Speed: m/s. */
280         public static final SIUnit SI_UNIT = SIUnit.of("m/s");
281 
282         /** Meter per second. */
283         public static final Speed.Unit m_s =
284                 new Speed.Unit("m/s", "m/s", "meter per second", IdentityScale.SCALE, UnitSystem.SI_DERIVED);
285 
286         /** The SI or BASE unit. */
287         public static final Speed.Unit SI = m_s;
288 
289         /** m/h. */
290         public static final Speed.Unit m_h =
291                 new Speed.Unit("m/h", "m/h", "meter per hour", new LinearScale(1.0, 3600.0), UnitSystem.SI_ACCEPTED);
292 
293         /** km/s. */
294         public static final Speed.Unit km_s =
295                 new Speed.Unit("km/s", "km/s", "kilometer per second", new LinearScale(1000.0), UnitSystem.SI_ACCEPTED);
296 
297         /** km/h. */
298         public static final Speed.Unit km_h =
299                 new Speed.Unit("km/h", "km/h", "kilometer per hour", new LinearScale(1000.0, 3600.0), UnitSystem.SI_ACCEPTED);
300 
301         /** in/s. */
302         public static final Speed.Unit in_s =
303                 new Speed.Unit("in/s", "in/s", "inch per second", new LinearScale(Length.Unit.CONST_IN), UnitSystem.IMPERIAL);
304 
305         /** in/min. */
306         public static final Speed.Unit in_min = new Speed.Unit("in/min", "in/min", "inch per minute",
307                 new LinearScale(Length.Unit.CONST_IN, 60.0), UnitSystem.IMPERIAL);
308 
309         /** in/h. */
310         public static final Speed.Unit in_h = new Speed.Unit("in/h", "in/h", "inch per hour",
311                 new LinearScale(Length.Unit.CONST_IN, 3600.0), UnitSystem.IMPERIAL);
312 
313         /** ft/s. */
314         public static final Speed.Unit ft_s =
315                 new Speed.Unit("ft/s", "ft/s", "foot per second", new LinearScale(Length.Unit.CONST_FT), UnitSystem.IMPERIAL);
316 
317         /** ft/min. */
318         public static final Speed.Unit ft_min = new Speed.Unit("ft/min", "ft/min", "foot per minute",
319                 new LinearScale(Length.Unit.CONST_FT, 60.0), UnitSystem.IMPERIAL);
320 
321         /** ft/h. */
322         public static final Speed.Unit ft_h = new Speed.Unit("ft/h", "ft/h", "foot per hour",
323                 new LinearScale(Length.Unit.CONST_FT, 3600.0), UnitSystem.IMPERIAL);
324 
325         /** mi/s. */
326         public static final Speed.Unit mi_s =
327                 new Speed.Unit("mi/s", "mi/s", "mile per second", new LinearScale(Length.Unit.CONST_MI), UnitSystem.IMPERIAL);
328 
329         /** mi/min. */
330         public static final Speed.Unit mi_min = new Speed.Unit("mi/min", "mi/min", "mile per minute",
331                 new LinearScale(Length.Unit.CONST_MI, 60.0), UnitSystem.IMPERIAL);
332 
333         /** mi/h. */
334         public static final Speed.Unit mi_h = new Speed.Unit("mi/h", "mi/h", "mile per hour",
335                 new LinearScale(Length.Unit.CONST_MI, 3600.0), UnitSystem.IMPERIAL);
336 
337         /** knot = Nautical Mile per hour. */
338         public static final Speed.Unit kt = new Speed.Unit("kt", "knot", Length.Unit.CONST_NM / 3600.0, UnitSystem.OTHER);
339 
340         /**
341          * Create a new Speed unit.
342          * @param id the id or main abbreviation of the unit
343          * @param name the full name of the unit
344          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
345          * @param unitSystem the unit system such as SI or IMPERIAL
346          */
347         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
348         {
349             super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
350         }
351 
352         /**
353          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
354          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
355          * @param displayAbbreviation the display abbreviation of the unit
356          * @param name the full name of the unit
357          * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
358          * @param unitSystem unit system, e.g. SI or Imperial
359          */
360         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
361                 final UnitSystem unitSystem)
362         {
363             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
364         }
365 
366         @Override
367         public SIUnit siUnit()
368         {
369             return SI_UNIT;
370         }
371 
372         @Override
373         public Unit getBaseUnit()
374         {
375             return SI;
376         }
377 
378         @Override
379         public Speed ofSi(final double si)
380         {
381             return Speed.ofSi(si);
382         }
383 
384         @Override
385         public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
386                 final double scaleFactor, final UnitSystem unitSystem)
387         {
388             if (getScale() instanceof LinearScale ls)
389             {
390                 return new Speed.Unit(textualAbbreviation, displayAbbreviation, name,
391                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
392             }
393             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
394         }
395 
396     }
397 }