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