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.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   * Length is the measure of distance between two points, expressed in meters (m).
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 Length extends Quantity<Length>
22  {
23      /** Constant with value zero. */
24      public static final Length ZERO = Length.ofSi(0.0);
25  
26      /** Constant with value one. */
27      public static final Length ONE = Length.ofSi(1.0);
28  
29      /** Constant with value NaN. */
30      @SuppressWarnings("checkstyle:constantname")
31      public static final Length NaN = Length.ofSi(Double.NaN);
32  
33      /** Constant with value POSITIVE_INFINITY. */
34      public static final Length POSITIVE_INFINITY = Length.ofSi(Double.POSITIVE_INFINITY);
35  
36      /** Constant with value NEGATIVE_INFINITY. */
37      public static final Length NEGATIVE_INFINITY = Length.ofSi(Double.NEGATIVE_INFINITY);
38  
39      /** Constant with value MAX_VALUE. */
40      public static final Length POS_MAXVALUE = Length.ofSi(Double.MAX_VALUE);
41  
42      /** Constant with value -MAX_VALUE. */
43      public static final Length NEG_MAXVALUE = Length.ofSi(-Double.MAX_VALUE);
44  
45      /** */
46      private static final long serialVersionUID = 600L;
47  
48      /**
49       * Instantiate a Length 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 Length(final double valueInUnit, final Length.Unit unit)
54      {
55          super(valueInUnit, unit);
56      }
57  
58      /**
59       * Instantiate a Length quantity with a unit, expressed as a String.
60       * @param valueInUnit the value, expressed in the unit
61       * @param abbreviation the String abbreviation of the unit in which the value is expressed
62       */
63      public Length(final double valueInUnit, final String abbreviation)
64      {
65          this(valueInUnit, Units.resolve(Length.Unit.class, abbreviation));
66      }
67  
68      /**
69       * Construct Length quantity.
70       * @param value Scalar from which to construct this instance
71       */
72      public Length(final Length value)
73      {
74          super(value.si(), Length.Unit.SI);
75          setDisplayUnit(value.getDisplayUnit());
76      }
77  
78      /**
79       * Return a Length instance based on an SI value.
80       * @param si the si value
81       * @return the Length instance based on an SI value
82       */
83      public static Length ofSi(final double si)
84      {
85          return new Length(si, Length.Unit.SI);
86      }
87  
88      @Override
89      public Length instantiateSi(final double si)
90      {
91          return ofSi(si);
92      }
93  
94      @Override
95      public SIUnit siUnit()
96      {
97          return Length.Unit.SI_UNIT;
98      }
99  
100     /**
101      * Returns a Length representation of a textual representation of a value with a unit. The String representation that can be
102      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
103      * but not required, between the value and the unit.
104      * @param text the textual representation to parse into a Length
105      * @return the Scalar representation of the value in its unit
106      * @throws IllegalArgumentException when the text cannot be parsed
107      * @throws NullPointerException when the text argument is null
108      */
109     public static Length valueOf(final String text)
110     {
111         return Quantity.valueOf(text, ZERO);
112     }
113 
114     /**
115      * Returns a Length based on a value and the textual representation of the unit, which can be localized.
116      * @param valueInUnit the value, expressed in the unit as given by unitString
117      * @param unitString the textual representation of the unit
118      * @return the Scalar representation of the value in its unit
119      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
120      * @throws NullPointerException when the unitString argument is null
121      */
122     public static Length of(final double valueInUnit, final String unitString)
123     {
124         return Quantity.of(valueInUnit, unitString, ZERO);
125     }
126 
127     @Override
128     public Length.Unit getDisplayUnit()
129     {
130         return (Length.Unit) super.getDisplayUnit();
131     }
132 
133     /**
134      * Add an (absolute) position to this length, and return a position. The unit of the return value will be the unit of this
135      * length, and the reference of the return value will be the reference belonging to the given position.
136      * <code>R.add(A)</code> = unit of R and reference value of A.
137      * @param position the absolute position to add
138      * @return the absolute position plus this length
139      */
140     public final Position add(final Position position)
141     {
142         return position.add(this).setDisplayUnit(getDisplayUnit());
143     }
144 
145     /**
146      * Calculate the division of Length and Length, which results in a Dimensionless quantity.
147      * @param v quantity
148      * @return quantity as a division of Length and Length
149      */
150     public final Dimensionless divide(final Length v)
151     {
152         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
153     }
154 
155     /**
156      * Calculate the multiplication of Length and LinearObjectDensity, which results in a Dimensionless quantity.
157      * @param v quantity
158      * @return quantity as a multiplication of Length and LinearObjectDensity
159      */
160     public final Dimensionless multiply(final LinearObjectDensity v)
161     {
162         return new Dimensionless(this.si() * v.si(), Unitless.BASE);
163     }
164 
165     /**
166      * Calculate the multiplication of Length and Length, which results in a Area quantity.
167      * @param v quantity
168      * @return quantity as a multiplication of Length and Length
169      */
170     public final Area multiply(final Length v)
171     {
172         return new Area(this.si() * v.si(), Area.Unit.SI);
173     }
174 
175     /**
176      * Calculate the division of Length and LinearObjectDensity, which results in a Area quantity.
177      * @param v quantity
178      * @return quantity as a division of Length and LinearObjectDensity
179      */
180     public final Area divide(final LinearObjectDensity v)
181     {
182         return new Area(this.si() / v.si(), Area.Unit.SI);
183     }
184 
185     /**
186      * Calculate the division of Length and Area, which results in a LinearObjectDensity quantity.
187      * @param v quantity
188      * @return quantity as a division of Length and Area
189      */
190     public final LinearObjectDensity divide(final Area v)
191     {
192         return new LinearObjectDensity(this.si() / v.si(), LinearObjectDensity.Unit.SI);
193     }
194 
195     /**
196      * Calculate the multiplication of Length and Area, which results in a Volume quantity.
197      * @param v quantity
198      * @return quantity as a multiplication of Length and Area
199      */
200     public final Volume multiply(final Area v)
201     {
202         return new Volume(this.si() * v.si(), Volume.Unit.SI);
203     }
204 
205     /**
206      * Calculate the multiplication of Length and Force, which results in a Energy quantity.
207      * @param v quantity
208      * @return quantity as a multiplication of Length and Force
209      */
210     public final Energy multiply(final Force v)
211     {
212         return new Energy(this.si() * v.si(), Energy.Unit.SI);
213     }
214 
215     /**
216      * Calculate the multiplication of Length and Frequency, which results in a Speed quantity.
217      * @param v quantity
218      * @return quantity as a multiplication of Length and Frequency
219      */
220     public final Speed multiply(final Frequency v)
221     {
222         return new Speed(this.si() * v.si(), Speed.Unit.SI);
223     }
224 
225     /**
226      * Calculate the division of Length and Duration, which results in a Speed quantity.
227      * @param v quantity
228      * @return quantity as a division of Length and Duration
229      */
230     public final Speed divide(final Duration v)
231     {
232         return new Speed(this.si() / v.si(), Speed.Unit.SI);
233     }
234 
235     /**
236      * Calculate the division of Length and Speed, which results in a Duration quantity.
237      * @param v quantity
238      * @return quantity as a division of Length and Speed
239      */
240     public final Duration divide(final Speed v)
241     {
242         return new Duration(this.si() / v.si(), Duration.Unit.SI);
243     }
244 
245     /**
246      * Calculate the multiplication of Length and FlowMass, which results in a Momentum quantity.
247      * @param v quantity
248      * @return quantity as a multiplication of Length and FlowMass
249      */
250     public final Momentum multiply(final FlowMass v)
251     {
252         return new Momentum(this.si() * v.si(), Momentum.Unit.SI);
253     }
254 
255     @Override
256     public LinearObjectDensity reciprocal()
257     {
258         return LinearObjectDensity.ofSi(1.0 / this.si());
259     }
260 
261     /******************************************************************************************************/
262     /********************************************** UNIT CLASS ********************************************/
263     /******************************************************************************************************/
264 
265     /**
266      * Length.Unit encodes the length unit.
267      * <p>
268      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
269      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
270      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
271      * @author Alexander Verbraeck
272      */
273     @SuppressWarnings("checkstyle:constantname")
274     public static class Unit extends AbstractUnit<Length.Unit, Length>
275     {
276         /** Constant for the foot. */
277         public static final double CONST_FT = 0.3048;
278 
279         /** Constant for the yard. */
280         public static final double CONST_YD = 3.0 * CONST_FT;
281 
282         /** Constant for the inch. */
283         public static final double CONST_IN = CONST_FT / 12.0;
284 
285         /** Constant for the mile. */
286         public static final double CONST_MI = 5280.0 * CONST_FT;
287 
288         /** Constant for the nautical mile. */
289         public static final double CONST_NM = 1852.0;
290 
291         /** Constant for the Astronomical Unit = 149,597,870,700 m. */
292         public static final double CONST_AU = 149_597_870_700.0;
293 
294         /** Constant for the lightyear = 9,460,730,472,580,800 m. */
295         public static final double CONST_LY = 9_460_730_472_580_800.0;
296 
297         /** Constant for the parsec = AU / tan(1 arcsecond) = AU * 648,000 / PI m. */
298         public static final double CONST_PC = 149_597_870_700.0 * 648_000.0 / Math.PI;
299 
300         /** The dimensions of the length: m. */
301         public static final SIUnit SI_UNIT = SIUnit.of("m");
302 
303         /** meter. */
304         public static final Length.Unit m = new Length.Unit("m", "meter", 1.0, UnitSystem.SI_BASE);
305 
306         /** The SI or BASE unit. */
307         public static final Length.Unit SI = m.generateSiPrefixes(false, false);
308 
309         /** decameter. */
310         public static final Length.Unit dam = Units.resolve(Length.Unit.class, "dam");
311 
312         /** hectometer. */
313         public static final Length.Unit hm = Units.resolve(Length.Unit.class, "hm");
314 
315         /** kilometer. */
316         public static final Length.Unit km = Units.resolve(Length.Unit.class, "km");
317 
318         /** decimeter. */
319         public static final Length.Unit dm = Units.resolve(Length.Unit.class, "dm");
320 
321         /** centimeter. */
322         public static final Length.Unit cm = Units.resolve(Length.Unit.class, "cm");
323 
324         /** millimeter. */
325         public static final Length.Unit mm = Units.resolve(Length.Unit.class, "mm");
326 
327         /** micrometer. */
328         public static final Length.Unit mum = Units.resolve(Length.Unit.class, "mum");
329 
330         /** nanometer. */
331         public static final Length.Unit nm = Units.resolve(Length.Unit.class, "nm");
332 
333         /** picometer. */
334         public static final Length.Unit pm = Units.resolve(Length.Unit.class, "pm");
335 
336         /** attometer. */
337         public static final Length.Unit am = Units.resolve(Length.Unit.class, "am");
338 
339         /** femtometer. */
340         public static final Length.Unit fm = Units.resolve(Length.Unit.class, "fm");
341 
342         /** foot (international) = 0.3048 m = 1/3 yd = 12 inches. */
343         public static final Length.Unit ft =
344                 new Length.Unit("ft", "ft", "foot", new LinearScale(CONST_FT), UnitSystem.IMPERIAL);
345 
346         /** inch (international) = 2.54 cm = 1/36 yd = 1/12 ft. */
347         public static final Length.Unit in =
348                 new Length.Unit("in", "in", "inch", new LinearScale(CONST_IN), UnitSystem.IMPERIAL);
349 
350         /** yard (international) = 0.9144 m = 3 ft = 36 in. */
351         public static final Length.Unit yd =
352                 new Length.Unit("yd", "yd", "yard", new LinearScale(CONST_YD), UnitSystem.IMPERIAL);
353 
354         /** mile (international) = 5280 ft = 1760 yd. */
355         public static final Length.Unit mi =
356                 new Length.Unit("mi", "mi", "mile", new LinearScale(CONST_MI), UnitSystem.IMPERIAL);
357 
358         /** nautical mile (international) = 1852 m. */
359         public static final Length.Unit NM = new Length.Unit("NM", "Nautical Mile", CONST_NM, UnitSystem.OTHER);
360 
361         /** Astronomical Unit = 149,597,870,700 m. */
362         public static final Length.Unit AU = new Length.Unit("AU", "Astronomical Unit", CONST_AU, UnitSystem.OTHER);
363 
364         /** Lightyear = 9,460,730,472,580,800 m. */
365         public static final Length.Unit ly = new Length.Unit("ly", "lightyear", CONST_LY, UnitSystem.OTHER);
366 
367         /** Parsec = AU / tan(1 arcsecond) = AU * 648,000 / PI m. */
368         public static final Length.Unit pc = new Length.Unit("pc", "Parsec", CONST_PC, UnitSystem.OTHER);
369 
370         /** Angstrom = 10^-10 m. */
371         public static final Length.Unit A =
372                 new Length.Unit("A", "\u00C5", "angstrom", new LinearScale(1.0E-10), UnitSystem.OTHER);
373 
374         /**
375          * Create a new length unit.
376          * @param id the id or main abbreviation of the unit
377          * @param name the full name of the unit
378          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
379          * @param unitSystem the unit system such as SI or IMPERIAL
380          */
381         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
382         {
383             super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
384         }
385 
386         /**
387          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
388          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
389          * @param displayAbbreviation the display abbreviation of the unit
390          * @param name the full name of the unit
391          * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
392          * @param unitSystem unit system, e.g. SI or Imperial
393          */
394         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
395                 final UnitSystem unitSystem)
396         {
397             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
398         }
399 
400         @Override
401         public SIUnit siUnit()
402         {
403             return SI_UNIT;
404         }
405 
406         @Override
407         public Unit getBaseUnit()
408         {
409             return SI;
410         }
411 
412         @Override
413         public Length ofSi(final double si)
414         {
415             return Length.ofSi(si);
416         }
417 
418         @Override
419         public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
420                 final double scaleFactor, final UnitSystem unitSystem)
421         {
422             if (getScale() instanceof LinearScale ls)
423             {
424                 return new Length.Unit(textualAbbreviation, displayAbbreviation, name,
425                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
426             }
427             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
428         }
429 
430     }
431 }