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.UnitInterface;
6   import org.djunits.unit.UnitRuntimeException;
7   import org.djunits.unit.Unitless;
8   import org.djunits.unit.Units;
9   import org.djunits.unit.scale.IdentityScale;
10  import org.djunits.unit.scale.LinearScale;
11  import org.djunits.unit.scale.Scale;
12  import org.djunits.unit.si.SIPrefix;
13  import org.djunits.unit.si.SIPrefixes;
14  import org.djunits.unit.si.SIUnit;
15  import org.djunits.unit.system.UnitSystem;
16  
17  /**
18   * Linear object density counts the number of objects per unit of length, measured in number per meter (/m).
19   * <p>
20   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
21   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
22   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
23   * @author Alexander Verbraeck
24   */
25  public class LinearObjectDensity extends Quantity<LinearObjectDensity>
26  {
27      /** Constant with value zero. */
28      public static final LinearObjectDensity ZERO = ofSi(0.0);
29  
30      /** Constant with value one. */
31      public static final LinearObjectDensity ONE = ofSi(1.0);
32  
33      /** Constant with value NaN. */
34      @SuppressWarnings("checkstyle:constantname")
35      public static final LinearObjectDensity NaN = ofSi(Double.NaN);
36  
37      /** Constant with value POSITIVE_INFINITY. */
38      public static final LinearObjectDensity POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
39  
40      /** Constant with value NEGATIVE_INFINITY. */
41      public static final LinearObjectDensity NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
42  
43      /** Constant with value MAX_VALUE. */
44      public static final LinearObjectDensity POS_MAXVALUE = ofSi(Double.MAX_VALUE);
45  
46      /** Constant with value -MAX_VALUE. */
47      public static final LinearObjectDensity NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
48  
49      /** */
50      private static final long serialVersionUID = 600L;
51  
52      /**
53       * Instantiate a LinearObjectDensity quantity with an SI or base value and a display unit.
54       * @param value the quantity value expressed in the SI or base unit
55       * @param displayUnit the display unit to use
56       * @param useSi use SI value when true, use value in unit when false
57       */
58      public LinearObjectDensity(final double value, final LinearObjectDensity.Unit displayUnit, final boolean useSi)
59      {
60          super(value, displayUnit, useSi);
61      }
62  
63      /**
64       * Instantiate a LinearObjectDensity quantity expressed in the given unit.
65       * @param valueInUnit the quantity value expressed in the given unit
66       * @param unit the unit of the value, also acts as the display unit
67       */
68      public LinearObjectDensity(final double valueInUnit, final LinearObjectDensity.Unit unit)
69      {
70          this(valueInUnit, unit, false);
71      }
72  
73      /**
74       * Return a LinearObjectDensity instance based on an SI value.
75       * @param si the si value
76       * @return the LinearObjectDensity instance based on an SI value
77       */
78      public static LinearObjectDensity ofSi(final double si)
79      {
80          return new LinearObjectDensity(si, LinearObjectDensity.Unit.SI, true);
81      }
82  
83      /**
84       * Instantiate a LinearObjectDensity quantity with an SI or base value and a display unit.
85       * @param siValue the quantity value expressed in the SI or base unit
86       * @param displayUnit the display unit to use
87       * @return the LinearObjectDensity instance based on an SI value with the given display unit
88       */
89      public static LinearObjectDensity ofSi(final double siValue, final LinearObjectDensity.Unit displayUnit)
90      {
91          return new LinearObjectDensity(siValue, displayUnit, true);
92      }
93  
94      @Override
95      public LinearObjectDensity instantiateSi(final double siValue, final UnitInterface<LinearObjectDensity> displayUnit)
96      {
97          return new LinearObjectDensity(siValue, (LinearObjectDensity.Unit) displayUnit, true);
98      }
99  
100     /**
101      * Returns a LinearObjectDensity representation of a textual representation of a value with a unit. The String
102      * representation that can be parsed is the double value in the unit, followed by a localized or English abbreviation of the
103      * unit. Spaces are allowed, but not required, between the value and the unit.
104      * @param text the textual representation to parse into a LinearObjectDensity
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 LinearObjectDensity valueOf(final String text)
110     {
111         return Quantity.valueOf(text, ZERO);
112     }
113 
114     /**
115      * Returns a LinearObjectDensity based on a value expressed in the unit.
116      * @param valueInUnit the value, expressed in the given unit
117      * @param unit the unit of the value, also acts as the display unit
118      * @return ab LinearObjectDensity representation of the value in its unit
119      */
120     public static LinearObjectDensity of(final double valueInUnit, final LinearObjectDensity.Unit unit)
121     {
122         return new LinearObjectDensity(valueInUnit, unit);
123     }
124 
125     /**
126      * Returns a LinearObjectDensity based on a value and the textual representation of the unit, which can be localized.
127      * @param valueInUnit the value, expressed in the unit as given by unitString
128      * @param unitString the textual representation of the unit
129      * @return the Scalar representation of the value in its unit
130      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
131      * @throws NullPointerException when the unitString argument is null
132      */
133     public static LinearObjectDensity of(final double valueInUnit, final String unitString)
134     {
135         return Quantity.of(valueInUnit, unitString, ZERO);
136     }
137 
138     @Override
139     public LinearObjectDensity.Unit getDisplayUnit()
140     {
141         return (LinearObjectDensity.Unit) super.getDisplayUnit();
142     }
143 
144     /**
145      * Calculate the division of LinearObjectDensity and LinearObjectDensity, which results in a Dimensionless quantity.
146      * @param v quantity
147      * @return quantity as a division of LinearObjectDensity and LinearObjectDensity
148      */
149     public Dimensionless divide(final LinearObjectDensity v)
150     {
151         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
152     }
153 
154     /**
155      * Calculate the multiplication of LinearObjectDensity and Length, which results in a Dimensionless scalar.
156      * @param v scalar
157      * @return scalar as a multiplication of LinearObjectDensity and Length
158      */
159     public Dimensionless multiply(final Length v)
160     {
161         return new Dimensionless(this.si() * v.si(), Unitless.BASE);
162     }
163 
164     /**
165      * Calculate the multiplication of LinearObjectDensity and Area, which results in a Length scalar.
166      * @param v scalar
167      * @return scalar as a multiplication of LinearObjectDensity and Area
168      */
169     public Length multiply(final Area v)
170     {
171         return new Length(this.si() * v.si(), Length.Unit.SI);
172     }
173 
174     /**
175      * Calculate the multiplication of LinearObjectDensity and Energy, which results in a Force scalar.
176      * @param v scalar
177      * @return scalar as a multiplication of LinearObjectDensity and Energy
178      */
179     public Force multiply(final Energy v)
180     {
181         return new Force(this.si() * v.si(), Force.Unit.SI);
182     }
183 
184     /**
185      * Calculate the multiplication of LinearObjectDensity and Speed, which results in a Frequency scalar.
186      * @param v scalar
187      * @return scalar as a multiplication of LinearObjectDensity and Speed
188      */
189     public Frequency multiply(final Speed v)
190     {
191         return new Frequency(this.si() * v.si(), Frequency.Unit.SI);
192     }
193 
194     @Override
195     public Length reciprocal()
196     {
197         return Length.ofSi(1.0 / this.si());
198     }
199 
200     /******************************************************************************************************/
201     /********************************************** UNIT CLASS ********************************************/
202     /******************************************************************************************************/
203 
204     /**
205      * LinearObjectDensity.Unit encodes the unit for the number of objects per unit of length.
206      * <p>
207      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
208      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
209      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
210      * @author Alexander Verbraeck
211      */
212     @SuppressWarnings("checkstyle:constantname")
213     public static class Unit extends AbstractUnit<LinearObjectDensity>
214     {
215         /** The dimensions of the number of objects per unit of length: per meter (/m). */
216         public static final SIUnit SI_UNIT = SIUnit.of("/m");
217 
218         /** per meter. */
219         public static final LinearObjectDensity.Unit per_m = new LinearObjectDensity.Unit("/m", "/m", "per meter",
220                 IdentityScale.SCALE, UnitSystem.SI_DERIVED, SIPrefixes.getSiPrefixPer("/"));
221 
222         /** The SI or BASE unit. */
223         public static final LinearObjectDensity.Unit SI = (Unit) per_m.generateSiPrefixes(false, true);
224 
225         /** per millimeter. */
226         public static final LinearObjectDensity.Unit per_mm = Units.resolve(LinearObjectDensity.Unit.class, "/mm");
227 
228         /** per centimeter. */
229         public static final LinearObjectDensity.Unit per_cm = Units.resolve(LinearObjectDensity.Unit.class, "/cm");
230 
231         /** per decimeter. */
232         public static final LinearObjectDensity.Unit per_dm = Units.resolve(LinearObjectDensity.Unit.class, "/dm");
233 
234         /** per decameter. */
235         public static final LinearObjectDensity.Unit per_dam = Units.resolve(LinearObjectDensity.Unit.class, "/dam");
236 
237         /** per hectometer. */
238         public static final LinearObjectDensity.Unit per_hm = Units.resolve(LinearObjectDensity.Unit.class, "/hm");
239 
240         /** per kilometer. */
241         public static final LinearObjectDensity.Unit per_km = Units.resolve(LinearObjectDensity.Unit.class, "/km");
242 
243         /** per inch. */
244         public static final LinearObjectDensity.Unit per_in =
245                 new LinearObjectDensity.Unit("/in", "per inch", 1.0 / Length.Unit.CONST_IN, UnitSystem.IMPERIAL);
246 
247         /** per foot. */
248         public static final LinearObjectDensity.Unit per_ft =
249                 new LinearObjectDensity.Unit("/ft", "per foot", 1.0 / Length.Unit.CONST_FT, UnitSystem.IMPERIAL);
250 
251         /** per yard. */
252         public static final LinearObjectDensity.Unit per_yd =
253                 new LinearObjectDensity.Unit("/yd", "per yard", 1.0 / Length.Unit.CONST_YD, UnitSystem.IMPERIAL);
254 
255         /** per mile. */
256         public static final LinearObjectDensity.Unit per_mi =
257                 new LinearObjectDensity.Unit("/mi", "per mile", 1.0 / Length.Unit.CONST_MI, UnitSystem.IMPERIAL);
258 
259         /**
260          * Create a new LinearObjectDensity unit.
261          * @param id the id or main abbreviation of the unit
262          * @param name the full name of the unit
263          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
264          * @param unitSystem the unit system such as SI or IMPERIAL
265          */
266         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
267         {
268             super(id, name, scaleFactorToBaseUnit, unitSystem);
269         }
270 
271         /**
272          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
273          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
274          * @param displayAbbreviation the display abbreviation of the unit
275          * @param name the full name of the unit
276          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
277          * @param unitSystem unit system, e.g. SI or Imperial
278          * @param siPrefix the SI Prefix of this unit
279          */
280         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
281                 final UnitSystem unitSystem, final SIPrefix siPrefix)
282         {
283             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
284         }
285 
286         @Override
287         public SIUnit siUnit()
288         {
289             return SI_UNIT;
290         }
291 
292         @Override
293         public Unit getBaseUnit()
294         {
295             return SI;
296         }
297 
298         @Override
299         public LinearObjectDensity ofSi(final double si, final UnitInterface<LinearObjectDensity> displayUnit)
300         {
301             return new LinearObjectDensity(si, (Unit) displayUnit, true);
302         }
303 
304         @Override
305         public LinearObjectDensity.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation,
306                 final String name, final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
307         {
308             if (getScale() instanceof LinearScale ls)
309             {
310                 return new LinearObjectDensity.Unit(textualAbbreviation, displayAbbreviation, name,
311                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
312             }
313             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
314         }
315 
316     }
317 
318 }