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