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   * Electric charge denotes the electrostatic attraction or repulsion in the presence of other matter with charge, and is
19   * expressed in coulomb.
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 ElectricCharge extends Quantity<ElectricCharge>
27  {
28      /** Constant with value zero. */
29      public static final ElectricCharge ZERO = ofSi(0.0);
30  
31      /** Constant with value one. */
32      public static final ElectricCharge ONE = ofSi(1.0);
33  
34      /** Constant with value NaN. */
35      @SuppressWarnings("checkstyle:constantname")
36      public static final ElectricCharge NaN = ofSi(Double.NaN);
37  
38      /** Constant with value POSITIVE_INFINITY. */
39      public static final ElectricCharge POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
40  
41      /** Constant with value NEGATIVE_INFINITY. */
42      public static final ElectricCharge NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
43  
44      /** Constant with value MAX_VALUE. */
45      public static final ElectricCharge POS_MAXVALUE = ofSi(Double.MAX_VALUE);
46  
47      /** Constant with value -MAX_VALUE. */
48      public static final ElectricCharge NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
49  
50      /** */
51      private static final long serialVersionUID = 600L;
52  
53      /**
54       * Instantiate a ElectricCharge 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 ElectricCharge(final double value, final ElectricCharge.Unit displayUnit, final boolean useSi)
60      {
61          super(value, displayUnit, useSi);
62      }
63  
64      /**
65       * Instantiate a ElectricCharge 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 ElectricCharge(final double valueInUnit, final ElectricCharge.Unit unit)
70      {
71          this(valueInUnit, unit, false);
72      }
73  
74      /**
75       * Return a ElectricCharge instance based on an SI value.
76       * @param si the si value
77       * @return the ElectricCharge instance based on an SI value
78       */
79      public static ElectricCharge ofSi(final double si)
80      {
81          return new ElectricCharge(si, ElectricCharge.Unit.SI, true);
82      }
83  
84      /**
85       * Instantiate a ElectricCharge 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 ElectricCharge instance based on an SI value with the given display unit
89       */
90      public static ElectricCharge ofSi(final double siValue, final ElectricCharge.Unit displayUnit)
91      {
92          return new ElectricCharge(siValue, displayUnit, true);
93      }
94  
95      @Override
96      public ElectricCharge instantiateSi(final double siValue, final UnitInterface<ElectricCharge> displayUnit)
97      {
98          return new ElectricCharge(siValue, (ElectricCharge.Unit) displayUnit, true);
99      }
100 
101     /**
102      * Returns a ElectricCharge 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 ElectricCharge
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 ElectricCharge valueOf(final String text)
111     {
112         return Quantity.valueOf(text, ZERO);
113     }
114 
115     /**
116      * Returns a ElectricCharge 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 ElectricCharge representation of the value in its unit
120      */
121     public static ElectricCharge of(final double valueInUnit, final ElectricCharge.Unit unit)
122     {
123         return new ElectricCharge(valueInUnit, unit);
124     }
125 
126     /**
127      * Returns a ElectricCharge 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 ElectricCharge of(final double valueInUnit, final String unitString)
135     {
136         return Quantity.of(valueInUnit, unitString, ZERO);
137     }
138 
139     @Override
140     public ElectricCharge.Unit getDisplayUnit()
141     {
142         return (ElectricCharge.Unit) super.getDisplayUnit();
143     }
144 
145     /**
146      * Calculate the division of ElectricCharge and ElectricCharge, which results in a Dimensionless quantity.
147      * @param v quantity
148      * @return quantity as a division of ElectricCharge and ElectricCharge
149      */
150     public Dimensionless divide(final ElectricCharge v)
151     {
152         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
153     }
154 
155     /**
156      * Calculate the division of ElectricCharge and Duration, which results in a ElectricCurrent scalar.
157      * @param v scalar
158      * @return scalar as a division of ElectricCharge and Duration
159      */
160     public ElectricCurrent divide(final Duration v)
161     {
162         return new ElectricCurrent(this.si() / v.si(), ElectricCurrent.Unit.SI);
163     }
164 
165     /**
166      * Calculate the division of ElectricCharge and ElectricCurrent, which results in a Duration scalar.
167      * @param v scalar
168      * @return scalar as a division of ElectricCharge and ElectricCurrent
169      */
170     public Duration divide(final ElectricCurrent v)
171     {
172         return new Duration(this.si() / v.si(), Duration.Unit.SI);
173     }
174 
175     /**
176      * Calculate the division of ElectricCharge and ElectricPotential, which results in a ElectricalCapacitance scalar.
177      * @param v scalar
178      * @return scalar as a division of ElectricCharge and ElectricPotential
179      */
180     public ElectricalCapacitance divide(final ElectricPotential v)
181     {
182         return new ElectricalCapacitance(this.si() / v.si(), ElectricalCapacitance.Unit.SI);
183     }
184 
185     /**
186      * Calculate the division of ElectricCharge and ElectricalCapacitance, which results in a ElectricPotential scalar.
187      * @param v scalar
188      * @return scalar as a division of ElectricCharge and ElectricalCapacitance
189      */
190     public ElectricPotential divide(final ElectricalCapacitance v)
191     {
192         return new ElectricPotential(this.si() / v.si(), ElectricPotential.Unit.SI);
193     }
194 
195     /******************************************************************************************************/
196     /********************************************** UNIT CLASS ********************************************/
197     /******************************************************************************************************/
198 
199     /**
200      * ElectricCharge.Unit is a unit of electric charge and is expressed in Coulomb.
201      * <p>
202      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
203      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
204      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
205      * @author Alexander Verbraeck
206      */
207     @SuppressWarnings("checkstyle:constantname")
208     public static class Unit extends AbstractUnit<ElectricCharge>
209     {
210         /** The dimensions of electric charge, the Coulumb, is A.s. */
211         public static final SIUnit SI_UNIT = SIUnit.of("As");
212 
213         /** Gray. */
214         public static final ElectricCharge.Unit C = new ElectricCharge.Unit("C", "C", "coulomb", IdentityScale.SCALE,
215                 UnitSystem.SI_DERIVED, SIPrefixes.getSiPrefix(""));
216 
217         /** The SI or BASE unit. */
218         public static final ElectricCharge.Unit SI = (Unit) C.generateSiPrefixes(false, false);
219 
220         /** milliCoulomb = mA.s. */
221         public static final ElectricCharge.Unit mC = Units.resolve(ElectricCharge.Unit.class, "mC");
222 
223         /** microCoulomb = muA.s. */
224         public static final ElectricCharge.Unit muC = Units.resolve(ElectricCharge.Unit.class, "muC");
225 
226         /** ampere hour. */
227         public static final ElectricCharge.Unit Ah = C.deriveUnit("Ah", "ampere hour", 3600.0, UnitSystem.SI_DERIVED);
228 
229         /** milliampere hour. */
230         public static final ElectricCharge.Unit mAh = Ah.deriveUnit("mAh", "milliampere hour", 1E-3, UnitSystem.SI_DERIVED);
231 
232         /** milliampere second. */
233         public static final ElectricCharge.Unit mAs =
234                 mAh.deriveUnit("mAs", "milliampere second", 1.0 / 3600.0, UnitSystem.SI_DERIVED);
235 
236         /** kiloampere hour. */
237         public static final ElectricCharge.Unit kAh = Ah.deriveUnit("kAh", "kiloampere hour", 1E3, UnitSystem.SI_DERIVED);
238 
239         /** megaampere hour. */
240         public static final ElectricCharge.Unit MAh = Ah.deriveUnit("MAh", "megaampere hour", 1E6, UnitSystem.SI_DERIVED);
241 
242         /** Faraday. */
243         public static final ElectricCharge.Unit F = C.deriveUnit("F", "faraday", 96485.3383, UnitSystem.OTHER);
244 
245         /** atomic unit of charge. This value is exact since the 2019 redefinition of the SI base units. */
246         public static final ElectricCharge.Unit e =
247                 C.deriveUnit("e", "elementary unit of charge", 1.602176634E-19, UnitSystem.SI_ACCEPTED);
248 
249         /** statcoulomb (CGS ESU). */
250         public static final ElectricCharge.Unit statC = C.deriveUnit("statC", "statcoulomb", 3.335641E-10, UnitSystem.CGS_ESU);
251 
252         /** franklin (CGS ESU). */
253         public static final ElectricCharge.Unit Fr = statC.deriveUnit("Fr", "franklin", 1.0, UnitSystem.CGS_ESU);
254 
255         /** esu (CGS ESU). */
256         public static final ElectricCharge.Unit esu = statC.deriveUnit("esu", "electrostatic unit", 1.0, UnitSystem.CGS_ESU);
257 
258         /** abcoulomb (CGS EMU). */
259         public static final ElectricCharge.Unit abC = C.deriveUnit("abC", "abcoulomb", 10.0, UnitSystem.CGS_EMU);
260 
261         /** emu (CGS EMU). */
262         public static final ElectricCharge.Unit emu = abC.deriveUnit("emu", "electromagnetic unit", 1.0, UnitSystem.CGS_EMU);
263 
264         /**
265          * Create a new ElectricCharge unit.
266          * @param id the id or main abbreviation of the unit
267          * @param name the full name of the unit
268          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
269          * @param unitSystem the unit system such as SI or IMPERIAL
270          */
271         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
272         {
273             super(id, name, scaleFactorToBaseUnit, unitSystem);
274         }
275 
276         /**
277          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
278          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
279          * @param displayAbbreviation the display abbreviation of the unit
280          * @param name the full name of the unit
281          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
282          * @param unitSystem unit system, e.g. SI or Imperial
283          * @param siPrefix the SI Prefix of this unit
284          */
285         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
286                 final UnitSystem unitSystem, final SIPrefix siPrefix)
287         {
288             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
289         }
290 
291         @Override
292         public SIUnit siUnit()
293         {
294             return SI_UNIT;
295         }
296 
297         @Override
298         public Unit getBaseUnit()
299         {
300             return SI;
301         }
302 
303         @Override
304         public ElectricCharge ofSi(final double si, final UnitInterface<ElectricCharge> displayUnit)
305         {
306             return new ElectricCharge(si, (Unit) displayUnit, true);
307         }
308 
309         @Override
310         public ElectricCharge.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation,
311                 final String name, final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
312         {
313             if (getScale() instanceof LinearScale ls)
314             {
315                 return new ElectricCharge.Unit(textualAbbreviation, displayAbbreviation, name,
316                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
317             }
318             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
319         }
320 
321         @Override
322         public ElectricCharge.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
323                 final UnitSystem unitSystem)
324         {
325             return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
326         }
327 
328     }
329 
330 }