View Javadoc
1   package org.djunits.value.vfloat.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.vfloat.scalar.base.FloatScalarRel;
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 FloatElectricalCharge FloatScalar, 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 FloatElectricalCharge extends FloatScalarRel<ElectricalChargeUnit, FloatElectricalCharge>
28  {
29      /** */
30      private static final long serialVersionUID = 20150901L;
31  
32      /** Constant with value zero. */
33      public static final FloatElectricalCharge ZERO = new FloatElectricalCharge(0.0f, ElectricalChargeUnit.SI);
34  
35      /** Constant with value one. */
36      public static final FloatElectricalCharge ONE = new FloatElectricalCharge(1.0f, ElectricalChargeUnit.SI);
37  
38      /** Constant with value NaN. */
39      @SuppressWarnings("checkstyle:constantname")
40      public static final FloatElectricalCharge NaN = new FloatElectricalCharge(Float.NaN, ElectricalChargeUnit.SI);
41  
42      /** Constant with value POSITIVE_INFINITY. */
43      public static final FloatElectricalCharge POSITIVE_INFINITY =
44              new FloatElectricalCharge(Float.POSITIVE_INFINITY, ElectricalChargeUnit.SI);
45  
46      /** Constant with value NEGATIVE_INFINITY. */
47      public static final FloatElectricalCharge NEGATIVE_INFINITY =
48              new FloatElectricalCharge(Float.NEGATIVE_INFINITY, ElectricalChargeUnit.SI);
49  
50      /** Constant with value MAX_VALUE. */
51      public static final FloatElectricalCharge POS_MAXVALUE =
52              new FloatElectricalCharge(Float.MAX_VALUE, ElectricalChargeUnit.SI);
53  
54      /** Constant with value -MAX_VALUE. */
55      public static final FloatElectricalCharge NEG_MAXVALUE =
56              new FloatElectricalCharge(-Float.MAX_VALUE, ElectricalChargeUnit.SI);
57  
58      /**
59       * Construct FloatElectricalCharge scalar with a unit.
60       * @param value the float value, expressed in the given unit
61       * @param unit unit for the float value
62       */
63      public FloatElectricalCharge(final float value, final ElectricalChargeUnit unit)
64      {
65          super(value, unit);
66      }
67  
68      /**
69       * Construct FloatElectricalCharge scalar.
70       * @param value Scalar from which to construct this instance
71       */
72      public FloatElectricalCharge(final FloatElectricalCharge value)
73      {
74          super(value);
75      }
76  
77      /**
78       * Construct FloatElectricalCharge scalar with a unit using a double value.
79       * @param value the double value, expressed in the given unit
80       * @param unit unit for the resulting float value
81       */
82      public FloatElectricalCharge(final double value, final ElectricalChargeUnit unit)
83      {
84          super((float) value, unit);
85      }
86  
87      @Override
88      public final FloatElectricalCharge instantiateRel(final float value, final ElectricalChargeUnit unit)
89      {
90          return new FloatElectricalCharge(value, unit);
91      }
92  
93      /**
94       * Construct FloatElectricalCharge scalar based on an SI value.
95       * @param value the float value in SI units
96       * @return the new scalar with the SI value
97       */
98      public static final FloatElectricalCharge ofSI(final float value)
99      {
100         return new FloatElectricalCharge(value, ElectricalChargeUnit.SI);
101     }
102 
103     /**
104      * Interpolate between two values. Note that the first value does not have to be smaller than the second.
105      * @param zero the value at a ratio of zero
106      * @param one the value at a ratio of one
107      * @param ratio the ratio between 0 and 1, inclusive
108      * @return a FloatElectricalCharge at the given ratio between 0 and 1
109      */
110     public static FloatElectricalCharge interpolate(final FloatElectricalCharge zero, final FloatElectricalCharge one,
111             final float ratio)
112     {
113         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
114                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
115         return new FloatElectricalCharge(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
116                 zero.getDisplayUnit());
117     }
118 
119     /**
120      * Return the maximum value of two relative scalars.
121      * @param r1 the first scalar
122      * @param r2 the second scalar
123      * @return the maximum value of two relative scalars
124      */
125     public static FloatElectricalCharge max(final FloatElectricalCharge r1, final FloatElectricalCharge r2)
126     {
127         return r1.gt(r2) ? r1 : r2;
128     }
129 
130     /**
131      * Return the maximum value of more than two relative scalars.
132      * @param r1 the first scalar
133      * @param r2 the second scalar
134      * @param rn the other scalars
135      * @return the maximum value of more than two relative scalars
136      */
137     public static FloatElectricalCharge max(final FloatElectricalCharge r1, final FloatElectricalCharge r2,
138             final FloatElectricalCharge... rn)
139     {
140         FloatElectricalCharge maxr = r1.gt(r2) ? r1 : r2;
141         for (FloatElectricalCharge r : rn)
142         {
143             if (r.gt(maxr))
144             {
145                 maxr = r;
146             }
147         }
148         return maxr;
149     }
150 
151     /**
152      * Return the minimum value of two relative scalars.
153      * @param r1 the first scalar
154      * @param r2 the second scalar
155      * @return the minimum value of two relative scalars
156      */
157     public static FloatElectricalCharge min(final FloatElectricalCharge r1, final FloatElectricalCharge r2)
158     {
159         return r1.lt(r2) ? r1 : r2;
160     }
161 
162     /**
163      * Return the minimum value of more than two relative scalars.
164      * @param r1 the first scalar
165      * @param r2 the second scalar
166      * @param rn the other scalars
167      * @return the minimum value of more than two relative scalars
168      */
169     public static FloatElectricalCharge min(final FloatElectricalCharge r1, final FloatElectricalCharge r2,
170             final FloatElectricalCharge... rn)
171     {
172         FloatElectricalCharge minr = r1.lt(r2) ? r1 : r2;
173         for (FloatElectricalCharge r : rn)
174         {
175             if (r.lt(minr))
176             {
177                 minr = r;
178             }
179         }
180         return minr;
181     }
182 
183     /**
184      * Returns a FloatElectricalCharge representation of a textual representation of a value with a unit. The String
185      * representation that can be parsed is the double value in the unit, followed by a localized or English abbreviation of the
186      * unit. Spaces are allowed, but not required, between the value and the unit.
187      * @param text the textual representation to parse into a FloatElectricalCharge
188      * @return the Scalar representation of the value in its unit
189      * @throws IllegalArgumentException when the text cannot be parsed
190      * @throws NullPointerException when the text argument is null
191      */
192     public static FloatElectricalCharge valueOf(final String text)
193     {
194         Throw.whenNull(text, "Error parsing FloatElectricalCharge: text to parse is null");
195         Throw.when(text.length() == 0, IllegalArgumentException.class,
196                 "Error parsing FloatElectricalCharge: empty text to parse");
197         try
198         {
199             NumberParser numberParser = new NumberParser().lenient().trailing();
200             float f = numberParser.parseFloat(text);
201             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
202             ElectricalChargeUnit unit = ElectricalChargeUnit.BASE.getUnitByAbbreviation(unitString);
203             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity ElectricalCharge",
204                     unitString);
205             return new FloatElectricalCharge(f, unit);
206         }
207         catch (Exception exception)
208         {
209             throw new IllegalArgumentException("Error parsing FloatElectricalCharge from " + text + " using Locale "
210                     + Locale.getDefault(Locale.Category.FORMAT), exception);
211         }
212     }
213 
214     /**
215      * Returns a FloatElectricalCharge based on a value and the textual representation of the unit, which can be localized.
216      * @param value the value to use
217      * @param unitString the textual representation of the unit
218      * @return the Scalar representation of the value in its unit
219      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
220      * @throws NullPointerException when the unitString argument is null
221      */
222     public static FloatElectricalCharge of(final float value, final String unitString)
223     {
224         Throw.whenNull(unitString, "Error parsing FloatElectricalCharge: unitString is null");
225         Throw.when(unitString.length() == 0, IllegalArgumentException.class,
226                 "Error parsing FloatElectricalCharge: empty unitString");
227         ElectricalChargeUnit unit = ElectricalChargeUnit.BASE.getUnitByAbbreviation(unitString);
228         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing FloatElectricalCharge with unit %s",
229                 unitString);
230         return new FloatElectricalCharge(value, unit);
231     }
232 
233     /**
234      * Calculate the division of FloatElectricalCharge and FloatElectricalCharge, which results in a FloatDimensionless scalar.
235      * @param v scalar
236      * @return scalar as a division of FloatElectricalCharge and FloatElectricalCharge
237      */
238     public final FloatDimensionless divide(final FloatElectricalCharge v)
239     {
240         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
241     }
242 
243     /**
244      * Calculate the division of FloatElectricalCharge and FloatDuration, which results in a FloatElectricalCurrent scalar.
245      * @param v scalar
246      * @return scalar as a division of FloatElectricalCharge and FloatDuration
247      */
248     public final FloatElectricalCurrent divide(final FloatDuration v)
249     {
250         return new FloatElectricalCurrent(this.si / v.si, ElectricalCurrentUnit.SI);
251     }
252 
253     /**
254      * Calculate the division of FloatElectricalCharge and FloatElectricalCurrent, which results in a FloatDuration scalar.
255      * @param v scalar
256      * @return scalar as a division of FloatElectricalCharge and FloatElectricalCurrent
257      */
258     public final FloatDuration divide(final FloatElectricalCurrent v)
259     {
260         return new FloatDuration(this.si / v.si, DurationUnit.SI);
261     }
262 
263     /**
264      * Calculate the division of FloatElectricalCharge and FloatElectricalPotential, which results in a
265      * FloatElectricalCapacitance scalar.
266      * @param v scalar
267      * @return scalar as a division of FloatElectricalCharge and FloatElectricalPotential
268      */
269     public final FloatElectricalCapacitance divide(final FloatElectricalPotential v)
270     {
271         return new FloatElectricalCapacitance(this.si / v.si, ElectricalCapacitanceUnit.SI);
272     }
273 
274     /**
275      * Calculate the division of FloatElectricalCharge and FloatElectricalCapacitance, which results in a
276      * FloatElectricalPotential scalar.
277      * @param v scalar
278      * @return scalar as a division of FloatElectricalCharge and FloatElectricalCapacitance
279      */
280     public final FloatElectricalPotential divide(final FloatElectricalCapacitance v)
281     {
282         return new FloatElectricalPotential(this.si / v.si, ElectricalPotentialUnit.SI);
283     }
284 
285     @Override
286     public FloatSIScalar reciprocal()
287     {
288         return FloatSIScalar.divide(FloatDimensionless.ONE, this);
289     }
290 
291     /**
292      * Multiply two scalars that result in a scalar of type FloatElectricalCharge.
293      * @param scalar1 the first scalar
294      * @param scalar2 the second scalar
295      * @return the multiplication of both scalars as an instance of FloatElectricalCharge
296      */
297     public static FloatElectricalCharge multiply(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> 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                         .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions())
304                         .equals(ElectricalChargeUnit.BASE.getSiDimensions()),
305                 IllegalArgumentException.class,
306                 "Multiplying %s by %s does not result in instance of type FloatElectricalCharge", scalar1.toDisplayString(),
307                 scalar2.toDisplayString());
308         return new FloatElectricalCharge(scalar1.si * scalar2.si, ElectricalChargeUnit.SI);
309     }
310 
311     /**
312      * Divide two scalars that result in a scalar of type FloatElectricalCharge.
313      * @param scalar1 the first scalar
314      * @param scalar2 the second scalar
315      * @return the division of scalar1 by scalar2 as an instance of FloatElectricalCharge
316      */
317     public static FloatElectricalCharge divide(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> scalar2)
318     {
319         Throw.whenNull(scalar1, "scalar1 cannot be null");
320         Throw.whenNull(scalar2, "scalar2 cannot be null");
321         Throw.when(
322                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
323                         .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions())
324                         .equals(ElectricalChargeUnit.BASE.getSiDimensions()),
325                 IllegalArgumentException.class,
326                 "Dividing %s by %s does not result in an instance of type FloatElectricalCharge", scalar1.toDisplayString(),
327                 scalar2.toDisplayString());
328         return new FloatElectricalCharge(scalar1.si / scalar2.si, ElectricalChargeUnit.SI);
329     }
330 
331 }