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