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