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