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