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