View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.DimensionlessUnit;
6   import org.djunits.unit.DurationUnit;
7   import org.djunits.unit.ElectricalCapacitanceUnit;
8   import org.djunits.unit.ElectricalChargeUnit;
9   import org.djunits.unit.ElectricalCurrentUnit;
10  import org.djunits.unit.ElectricalPotentialUnit;
11  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
12  import org.djutils.base.NumberParser;
13  import org.djutils.exceptions.Throw;
14  
15  import jakarta.annotation.Generated;
16  
17  /**
18   * Easy access methods for the ElectricalCharge DoubleScalar, which is relative by definition.
19   * <p>
20   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
22   * </p>
23   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
25   */
26  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
27  public class ElectricalCharge extends DoubleScalarRel<ElectricalChargeUnit, ElectricalCharge>
28  {
29      /** */
30      private static final long serialVersionUID = 20150905L;
31  
32      /** Constant with value zero. */
33      public static final ElectricalCharge ZERO = new ElectricalCharge(0.0, ElectricalChargeUnit.SI);
34  
35      /** Constant with value one. */
36      public static final ElectricalCharge ONE = new ElectricalCharge(1.0, ElectricalChargeUnit.SI);
37  
38      /** Constant with value NaN. */
39      @SuppressWarnings("checkstyle:constantname")
40      public static final ElectricalCharge NaN = new ElectricalCharge(Double.NaN, ElectricalChargeUnit.SI);
41  
42      /** Constant with value POSITIVE_INFINITY. */
43      public static final ElectricalCharge POSITIVE_INFINITY =
44              new ElectricalCharge(Double.POSITIVE_INFINITY, ElectricalChargeUnit.SI);
45  
46      /** Constant with value NEGATIVE_INFINITY. */
47      public static final ElectricalCharge NEGATIVE_INFINITY =
48              new ElectricalCharge(Double.NEGATIVE_INFINITY, ElectricalChargeUnit.SI);
49  
50      /** Constant with value MAX_VALUE. */
51      public static final ElectricalCharge POS_MAXVALUE = new ElectricalCharge(Double.MAX_VALUE, ElectricalChargeUnit.SI);
52  
53      /** Constant with value -MAX_VALUE. */
54      public static final ElectricalCharge NEG_MAXVALUE = new ElectricalCharge(-Double.MAX_VALUE, ElectricalChargeUnit.SI);
55  
56      /**
57       * Construct ElectricalCharge scalar with a unit.
58       * @param value the double value, expressed in the given unit
59       * @param unit unit for the double value
60       */
61      public ElectricalCharge(final double value, final ElectricalChargeUnit unit)
62      {
63          super(value, unit);
64      }
65  
66      /**
67       * Construct ElectricalCharge scalar.
68       * @param value Scalar from which to construct this instance
69       */
70      public ElectricalCharge(final ElectricalCharge value)
71      {
72          super(value);
73      }
74  
75      @Override
76      public final ElectricalCharge instantiateRel(final double value, final ElectricalChargeUnit unit)
77      {
78          return new ElectricalCharge(value, unit);
79      }
80  
81      /**
82       * Construct ElectricalCharge scalar based on an SI value.
83       * @param value the double value in SI units
84       * @return the new scalar with the SI value
85       */
86      public static final ElectricalCharge ofSI(final double value)
87      {
88          return new ElectricalCharge(value, ElectricalChargeUnit.SI);
89      }
90  
91      /**
92       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
93       * @param zero the value at a ratio of zero
94       * @param one the value at a ratio of one
95       * @param ratio the ratio between 0 and 1, inclusive
96       * @return a ElectricalCharge at the given ratio between 0 and 1
97       */
98      public static ElectricalCharge interpolate(final ElectricalCharge zero, final ElectricalCharge one, final double ratio)
99      {
100         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
101                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
102         return new ElectricalCharge(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
103                 zero.getDisplayUnit());
104     }
105 
106     /**
107      * Return the maximum value of two relative scalars.
108      * @param r1 the first scalar
109      * @param r2 the second scalar
110      * @return the maximum value of two relative scalars
111      */
112     public static ElectricalCharge max(final ElectricalCharge r1, final ElectricalCharge r2)
113     {
114         return r1.gt(r2) ? r1 : r2;
115     }
116 
117     /**
118      * Return the maximum value of more than two relative scalars.
119      * @param r1 the first scalar
120      * @param r2 the second scalar
121      * @param rn the other scalars
122      * @return the maximum value of more than two relative scalars
123      */
124     public static ElectricalCharge max(final ElectricalCharge r1, final ElectricalCharge r2, final ElectricalCharge... rn)
125     {
126         ElectricalCharge maxr = r1.gt(r2) ? r1 : r2;
127         for (ElectricalCharge r : rn)
128         {
129             if (r.gt(maxr))
130             {
131                 maxr = r;
132             }
133         }
134         return maxr;
135     }
136 
137     /**
138      * Return the minimum value of two relative scalars.
139      * @param r1 the first scalar
140      * @param r2 the second scalar
141      * @return the minimum value of two relative scalars
142      */
143     public static ElectricalCharge min(final ElectricalCharge r1, final ElectricalCharge r2)
144     {
145         return r1.lt(r2) ? r1 : r2;
146     }
147 
148     /**
149      * Return the minimum value of more than two relative scalars.
150      * @param r1 the first scalar
151      * @param r2 the second scalar
152      * @param rn the other scalars
153      * @return the minimum value of more than two relative scalars
154      */
155     public static ElectricalCharge min(final ElectricalCharge r1, final ElectricalCharge r2, final ElectricalCharge... rn)
156     {
157         ElectricalCharge minr = r1.lt(r2) ? r1 : r2;
158         for (ElectricalCharge r : rn)
159         {
160             if (r.lt(minr))
161             {
162                 minr = r;
163             }
164         }
165         return minr;
166     }
167 
168     /**
169      * Returns a ElectricalCharge representation of a textual representation of a value with a unit. The String representation
170      * that can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces
171      * are allowed, but not required, between the value and the unit.
172      * @param text the textual representation to parse into a ElectricalCharge
173      * @return the Scalar representation of the value in its unit
174      * @throws IllegalArgumentException when the text cannot be parsed
175      * @throws NullPointerException when the text argument is null
176      */
177     public static ElectricalCharge valueOf(final String text)
178     {
179         Throw.whenNull(text, "Error parsing ElectricalCharge: text to parse is null");
180         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing ElectricalCharge: empty text to parse");
181         try
182         {
183             NumberParser numberParser = new NumberParser().lenient().trailing();
184             double d = numberParser.parseDouble(text);
185             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
186             ElectricalChargeUnit unit = ElectricalChargeUnit.BASE.getUnitByAbbreviation(unitString);
187             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity ElectricalCharge",
188                     unitString);
189             return new ElectricalCharge(d, unit);
190         }
191         catch (Exception exception)
192         {
193             throw new IllegalArgumentException("Error parsing ElectricalCharge from " + text + " using Locale "
194                     + Locale.getDefault(Locale.Category.FORMAT), exception);
195         }
196     }
197 
198     /**
199      * Returns a ElectricalCharge based on a value and the textual representation of the unit, which can be localized.
200      * @param value the value to use
201      * @param unitString the textual representation of the unit
202      * @return the Scalar representation of the value in its unit
203      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
204      * @throws NullPointerException when the unitString argument is null
205      */
206     public static ElectricalCharge of(final double value, final String unitString)
207     {
208         Throw.whenNull(unitString, "Error parsing ElectricalCharge: unitString is null");
209         Throw.when(unitString.length() == 0, IllegalArgumentException.class,
210                 "Error parsing ElectricalCharge: empty unitString");
211         ElectricalChargeUnit unit = ElectricalChargeUnit.BASE.getUnitByAbbreviation(unitString);
212         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing ElectricalCharge with unit %s", unitString);
213         return new ElectricalCharge(value, unit);
214     }
215 
216     /**
217      * Calculate the division of ElectricalCharge and ElectricalCharge, which results in a Dimensionless scalar.
218      * @param v scalar
219      * @return scalar as a division of ElectricalCharge and ElectricalCharge
220      */
221     public final Dimensionless divide(final ElectricalCharge v)
222     {
223         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
224     }
225 
226     /**
227      * Calculate the division of ElectricalCharge and Duration, which results in a ElectricalCurrent scalar.
228      * @param v scalar
229      * @return scalar as a division of ElectricalCharge and Duration
230      */
231     public final ElectricalCurrent divide(final Duration v)
232     {
233         return new ElectricalCurrent(this.si / v.si, ElectricalCurrentUnit.SI);
234     }
235 
236     /**
237      * Calculate the division of ElectricalCharge and ElectricalCurrent, which results in a Duration scalar.
238      * @param v scalar
239      * @return scalar as a division of ElectricalCharge and ElectricalCurrent
240      */
241     public final Duration divide(final ElectricalCurrent v)
242     {
243         return new Duration(this.si / v.si, DurationUnit.SI);
244     }
245 
246     /**
247      * Calculate the division of ElectricalCharge and ElectricalPotential, which results in a ElectricalCapacitance scalar.
248      * @param v scalar
249      * @return scalar as a division of ElectricalCharge and ElectricalPotential
250      */
251     public final ElectricalCapacitance divide(final ElectricalPotential v)
252     {
253         return new ElectricalCapacitance(this.si / v.si, ElectricalCapacitanceUnit.SI);
254     }
255 
256     /**
257      * Calculate the division of ElectricalCharge and ElectricalCapacitance, which results in a ElectricalPotential scalar.
258      * @param v scalar
259      * @return scalar as a division of ElectricalCharge and ElectricalCapacitance
260      */
261     public final ElectricalPotential divide(final ElectricalCapacitance v)
262     {
263         return new ElectricalPotential(this.si / v.si, ElectricalPotentialUnit.SI);
264     }
265 
266     @Override
267     public SIScalar reciprocal()
268     {
269         return SIScalar.divide(Dimensionless.ONE, this);
270     }
271 
272     /**
273      * Multiply two scalars that result in a scalar of type ElectricalCharge.
274      * @param scalar1 the first scalar
275      * @param scalar2 the second scalar
276      * @return the multiplication of both scalars as an instance of ElectricalCharge
277      */
278     public static ElectricalCharge multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
279     {
280         Throw.whenNull(scalar1, "scalar1 cannot be null");
281         Throw.whenNull(scalar2, "scalar2 cannot be null");
282         Throw.when(
283                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
284                         .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions())
285                         .equals(ElectricalChargeUnit.BASE.getSiDimensions()),
286                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type ElectricalCharge",
287                 scalar1.toDisplayString(), scalar2.toDisplayString());
288         return new ElectricalCharge(scalar1.si * scalar2.si, ElectricalChargeUnit.SI);
289     }
290 
291     /**
292      * Divide two scalars that result in a scalar of type ElectricalCharge.
293      * @param scalar1 the first scalar
294      * @param scalar2 the second scalar
295      * @return the division of scalar1 by scalar2 as an instance of ElectricalCharge
296      */
297     public static ElectricalCharge divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
298     {
299         Throw.whenNull(scalar1, "scalar1 cannot be null");
300         Throw.whenNull(scalar2, "scalar2 cannot be null");
301         Throw.when(
302                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
303                         .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions())
304                         .equals(ElectricalChargeUnit.BASE.getSiDimensions()),
305                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type ElectricalCharge",
306                 scalar1.toDisplayString(), scalar2.toDisplayString());
307         return new ElectricalCharge(scalar1.si / scalar2.si, ElectricalChargeUnit.SI);
308     }
309 
310 }