View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.DimensionlessUnit;
6   import org.djunits.unit.ElectricalChargeUnit;
7   import org.djunits.unit.ElectricalCurrentUnit;
8   import org.djunits.unit.ElectricalPotentialUnit;
9   import org.djunits.unit.PowerUnit;
10  import org.djunits.unit.Unit;
11  
12  /**
13   * Easy access methods for the ElectricalCurrent DoubleScalar, which is relative by definition. Instead of:
14   * 
15   * <pre>
16   * DoubleScalar.Rel&lt;ElectricalCurrentUnit&gt; value = new DoubleScalar.Rel&lt;ElectricalCurrentUnit&gt;(100.0, ElectricalCurrentUnit.SI);
17   * </pre>
18   * 
19   * we can now write:
20   * 
21   * <pre>
22   * ElectricalCurrent value = new ElectricalCurrent(100.0, ElectricalCurrentUnit.SI);
23   * </pre>
24   * 
25   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
26   * used are compatible.
27   * <p>
28   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
30   * <p>
31   * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
32   * initial version Sep 5, 2015 <br>
33   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
35   */
36  public class ElectricalCurrent extends AbstractDoubleScalarRel<ElectricalCurrentUnit, ElectricalCurrent>
37  {
38      /** */
39      private static final long serialVersionUID = 20150905L;
40  
41      /** constant with value zero. */
42      public static final ElectricalCurrent ZERO = new ElectricalCurrent(0.0, ElectricalCurrentUnit.SI);
43  
44      /** constant with value NaN. */
45      @SuppressWarnings("checkstyle:constantname")
46      public static final ElectricalCurrent NaN = new ElectricalCurrent(Double.NaN, ElectricalCurrentUnit.SI);
47  
48      /** constant with value POSITIVE_INFINITY. */
49      public static final ElectricalCurrent POSITIVE_INFINITY =
50              new ElectricalCurrent(Double.POSITIVE_INFINITY, ElectricalCurrentUnit.SI);
51  
52      /** constant with value NEGATIVE_INFINITY. */
53      public static final ElectricalCurrent NEGATIVE_INFINITY =
54              new ElectricalCurrent(Double.NEGATIVE_INFINITY, ElectricalCurrentUnit.SI);
55  
56      /** constant with value MAX_VALUE. */
57      public static final ElectricalCurrent POS_MAXVALUE = new ElectricalCurrent(Double.MAX_VALUE, ElectricalCurrentUnit.SI);
58  
59      /** constant with value -MAX_VALUE. */
60      public static final ElectricalCurrent NEG_MAXVALUE = new ElectricalCurrent(-Double.MAX_VALUE, ElectricalCurrentUnit.SI);
61  
62      /**
63       * Construct ElectricalCurrent scalar.
64       * @param value double value
65       * @param unit unit for the double value
66       */
67      public ElectricalCurrent(final double value, final ElectricalCurrentUnit unit)
68      {
69          super(value, unit);
70      }
71  
72      /**
73       * Construct ElectricalCurrent scalar.
74       * @param value Scalar from which to construct this instance
75       */
76      public ElectricalCurrent(final ElectricalCurrent value)
77      {
78          super(value);
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public final ElectricalCurrent instantiateRel(final double value, final ElectricalCurrentUnit unit)
84      {
85          return new ElectricalCurrent(value, unit);
86      }
87  
88      /**
89       * Construct ElectricalCurrent scalar.
90       * @param value double value in SI units
91       * @return the new scalar with the SI value
92       */
93      public static final ElectricalCurrent createSI(final double value)
94      {
95          return new ElectricalCurrent(value, ElectricalCurrentUnit.SI);
96      }
97  
98      /**
99       * Interpolate between two values.
100      * @param zero the low value
101      * @param one the high value
102      * @param ratio the ratio between 0 and 1, inclusive
103      * @return a Scalar at the ratio between
104      */
105     public static ElectricalCurrent interpolate(final ElectricalCurrent zero, final ElectricalCurrent one, final double ratio)
106     {
107         return new ElectricalCurrent(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
108     }
109 
110     /**
111      * Return the maximum value of two relative scalars.
112      * @param r1 the first scalar
113      * @param r2 the second scalar
114      * @return the maximum value of two relative scalars
115      */
116     public static ElectricalCurrent max(final ElectricalCurrent r1, final ElectricalCurrent r2)
117     {
118         return (r1.gt(r2)) ? r1 : r2;
119     }
120 
121     /**
122      * Return the maximum value of more than two relative scalars.
123      * @param r1 the first scalar
124      * @param r2 the second scalar
125      * @param rn the other scalars
126      * @return the maximum value of more than two relative scalars
127      */
128     public static ElectricalCurrent max(final ElectricalCurrent r1, final ElectricalCurrent r2, final ElectricalCurrent... rn)
129     {
130         ElectricalCurrent maxr = (r1.gt(r2)) ? r1 : r2;
131         for (ElectricalCurrent 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 ElectricalCurrent min(final ElectricalCurrent r1, final ElectricalCurrent 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 ElectricalCurrent min(final ElectricalCurrent r1, final ElectricalCurrent r2, final ElectricalCurrent... rn)
160     {
161         ElectricalCurrent minr = (r1.lt(r2)) ? r1 : r2;
162         for (ElectricalCurrent r : rn)
163         {
164             if (r.lt(minr))
165             {
166                 minr = r;
167             }
168         }
169         return minr;
170     }
171 
172     /**
173      * Returns a ElectricalCurrent representation of a textual representation of a value with a unit. The String representation
174      * that can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are
175      * allowed, but not necessary, between the value and the unit.
176      * @param text String; the textual representation to parse into a ElectricalCurrent
177      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
178      * @throws IllegalArgumentException when the text cannot be parsed
179      */
180     public static ElectricalCurrent valueOf(final String text) throws IllegalArgumentException
181     {
182         if (text == null || text.length() == 0)
183         {
184             throw new IllegalArgumentException("Error parsing ElectricalCurrent -- null or empty argument");
185         }
186         Matcher matcher = NUMBER_PATTERN.matcher(text);
187         if (matcher.find())
188         {
189             int index = matcher.end();
190             try
191             {
192                 String unitString = text.substring(index).trim();
193                 String valueString = text.substring(0, index).trim();
194                 for (ElectricalCurrentUnit unit : Unit.getUnits(ElectricalCurrentUnit.class))
195                 {
196                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
197                     {
198                         double d = Double.parseDouble(valueString);
199                         return new ElectricalCurrent(d, unit);
200                     }
201                 }
202             }
203             catch (Exception exception)
204             {
205                 throw new IllegalArgumentException("Error parsing ElectricalCurrent from " + text, exception);
206             }
207         }
208         throw new IllegalArgumentException("Error parsing ElectricalCurrent from " + text);
209     }
210 
211     /**
212      * Calculate the division of ElectricalCurrent and ElectricalCurrent, which results in a Dimensionless scalar.
213      * @param v ElectricalCurrent scalar
214      * @return Dimensionless scalar as a division of ElectricalCurrent and ElectricalCurrent
215      */
216     public final Dimensionless divideBy(final ElectricalCurrent v)
217     {
218         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
219     }
220 
221     /**
222      * Calculate the multiplication of ElectricalCurrent and ElectricalPotential, which results in a Power scalar.
223      * @param v ElectricalCurrent scalar
224      * @return Power scalar as a multiplication of ElectricalCurrent and ElectricalPotential
225      */
226     public final Power multiplyBy(final ElectricalPotential v)
227     {
228         return new Power(this.si * v.si, PowerUnit.SI);
229     }
230 
231     /**
232      * Calculate the multiplication of ElectricalCurrent and Duration, which results in a ElectricalCharge scalar.
233      * @param v ElectricalCurrent scalar
234      * @return ElectricalCharge scalar as a multiplication of ElectricalCurrent and Duration
235      */
236     public final ElectricalCharge multiplyBy(final Duration v)
237     {
238         return new ElectricalCharge(this.si * v.si, ElectricalChargeUnit.SI);
239     }
240 
241     /**
242      * Calculate the multiplication of ElectricalCurrent and ElectricalResistance, which results in a ElectricalPotential
243      * scalar.
244      * @param v ElectricalCurrent scalar
245      * @return ElectricalPotential scalar as a multiplication of ElectricalCurrent and ElectricalResistance
246      */
247     public final ElectricalPotential multiplyBy(final ElectricalResistance v)
248     {
249         return new ElectricalPotential(this.si * v.si, ElectricalPotentialUnit.SI);
250     }
251 
252 }