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