View Javadoc
1   package org.djunits.value.vfloat.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 FloatScalar, which is relative by definition. An example is Speed. Instead of:
14   * 
15   * <pre>
16   * FloatScalar.Rel&lt;ElectricalCurrentUnit&gt; value = new FloatScalar.Rel&lt;ElectricalCurrentUnit&gt;(100.0, ElectricalCurrentUnit.SI);
17   * </pre>
18   * 
19   * we can now write:
20   * 
21   * <pre>
22   * FloatElectricalCurrent value = new FloatElectricalCurrent(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 FloatElectricalCurrent extends AbstractFloatScalarRel<ElectricalCurrentUnit, FloatElectricalCurrent>
37  {
38      /** */
39      private static final long serialVersionUID = 20150901L;
40  
41      /** constant with value zero. */
42      public static final FloatElectricalCurrent ZERO = new FloatElectricalCurrent(0.0f, ElectricalCurrentUnit.SI);
43  
44      /** constant with value NaN. */
45      @SuppressWarnings("checkstyle:constantname")
46      public static final FloatElectricalCurrent NaN = new FloatElectricalCurrent(Float.NaN, ElectricalCurrentUnit.SI);
47  
48      /** constant with value POSITIVE_INFINITY. */
49      public static final FloatElectricalCurrent POSITIVE_INFINITY =
50              new FloatElectricalCurrent(Float.POSITIVE_INFINITY, ElectricalCurrentUnit.SI);
51  
52      /** constant with value NEGATIVE_INFINITY. */
53      public static final FloatElectricalCurrent NEGATIVE_INFINITY =
54              new FloatElectricalCurrent(Float.NEGATIVE_INFINITY, ElectricalCurrentUnit.SI);
55  
56      /** constant with value MAX_VALUE. */
57      public static final FloatElectricalCurrent POS_MAXVALUE =
58              new FloatElectricalCurrent(Float.MAX_VALUE, ElectricalCurrentUnit.SI);
59  
60      /** constant with value -MAX_VALUE. */
61      public static final FloatElectricalCurrent NEG_MAXVALUE =
62              new FloatElectricalCurrent(-Float.MAX_VALUE, ElectricalCurrentUnit.SI);
63  
64      /**
65       * Construct FloatElectricalCurrent scalar.
66       * @param value float value
67       * @param unit unit for the float value
68       */
69      public FloatElectricalCurrent(final float value, final ElectricalCurrentUnit unit)
70      {
71          super(value, unit);
72      }
73  
74      /**
75       * Construct FloatElectricalCurrent scalar.
76       * @param value Scalar from which to construct this instance
77       */
78      public FloatElectricalCurrent(final FloatElectricalCurrent value)
79      {
80          super(value);
81      }
82  
83      /**
84       * Construct FloatElectricalCurrent scalar using a double value.
85       * @param value double value
86       * @param unit unit for the resulting float value
87       */
88      public FloatElectricalCurrent(final double value, final ElectricalCurrentUnit unit)
89      {
90          super((float) value, unit);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public final FloatElectricalCurrent instantiateRel(final float value, final ElectricalCurrentUnit unit)
96      {
97          return new FloatElectricalCurrent(value, unit);
98      }
99  
100     /**
101      * Construct FloatElectricalCurrent scalar.
102      * @param value float value in SI units
103      * @return the new scalar with the SI value
104      */
105     public static final FloatElectricalCurrent createSI(final float value)
106     {
107         return new FloatElectricalCurrent(value, ElectricalCurrentUnit.SI);
108     }
109 
110     /**
111      * Interpolate between two values.
112      * @param zero the low value
113      * @param one the high value
114      * @param ratio the ratio between 0 and 1, inclusive
115      * @return a Scalar at the ratio between
116      */
117     public static FloatElectricalCurrent interpolate(final FloatElectricalCurrent zero, final FloatElectricalCurrent one,
118             final float ratio)
119     {
120         return new FloatElectricalCurrent(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio,
121                 zero.getUnit());
122     }
123 
124     /**
125      * Return the maximum value of two relative scalars.
126      * @param r1 the first scalar
127      * @param r2 the second scalar
128      * @return the maximum value of two relative scalars
129      */
130     public static FloatElectricalCurrent max(final FloatElectricalCurrent r1, final FloatElectricalCurrent r2)
131     {
132         return (r1.gt(r2)) ? r1 : r2;
133     }
134 
135     /**
136      * Return the maximum value of more than two relative scalars.
137      * @param r1 the first scalar
138      * @param r2 the second scalar
139      * @param rn the other scalars
140      * @return the maximum value of more than two relative scalars
141      */
142     public static FloatElectricalCurrent max(final FloatElectricalCurrent r1, final FloatElectricalCurrent r2,
143             final FloatElectricalCurrent... rn)
144     {
145         FloatElectricalCurrent maxr = (r1.gt(r2)) ? r1 : r2;
146         for (FloatElectricalCurrent r : rn)
147         {
148             if (r.gt(maxr))
149             {
150                 maxr = r;
151             }
152         }
153         return maxr;
154     }
155 
156     /**
157      * Return the minimum value of two relative scalars.
158      * @param r1 the first scalar
159      * @param r2 the second scalar
160      * @return the minimum value of two relative scalars
161      */
162     public static FloatElectricalCurrent min(final FloatElectricalCurrent r1, final FloatElectricalCurrent r2)
163     {
164         return (r1.lt(r2)) ? r1 : r2;
165     }
166 
167     /**
168      * Return the minimum value of more than two relative scalars.
169      * @param r1 the first scalar
170      * @param r2 the second scalar
171      * @param rn the other scalars
172      * @return the minimum value of more than two relative scalars
173      */
174     public static FloatElectricalCurrent min(final FloatElectricalCurrent r1, final FloatElectricalCurrent r2,
175             final FloatElectricalCurrent... rn)
176     {
177         FloatElectricalCurrent minr = (r1.lt(r2)) ? r1 : r2;
178         for (FloatElectricalCurrent r : rn)
179         {
180             if (r.lt(minr))
181             {
182                 minr = r;
183             }
184         }
185         return minr;
186     }
187 
188     /**
189      * Returns a FloatElectricalCurrent representation of a textual representation of a value with a unit. The String
190      * representation that can be parsed is the double value in the unit, followed by the official abbreviation of the unit.
191      * Spaces are allowed, but not necessary, between the value and the unit.
192      * @param text String; the textual representation to parse into a FloatElectricalCurrent
193      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
194      * @throws IllegalArgumentException when the text cannot be parsed
195      */
196     public static FloatElectricalCurrent valueOf(final String text) throws IllegalArgumentException
197     {
198         if (text == null || text.length() == 0)
199         {
200             throw new IllegalArgumentException("Error parsing FloatElectricalCurrent -- null or empty argument");
201         }
202         Matcher matcher = NUMBER_PATTERN.matcher(text);
203         if (matcher.find())
204         {
205             int index = matcher.end();
206             try
207             {
208                 String unitString = text.substring(index).trim();
209                 String valueString = text.substring(0, index).trim();
210                 for (ElectricalCurrentUnit unit : Unit.getUnits(ElectricalCurrentUnit.class))
211                 {
212                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
213                     {
214                         float f = Float.parseFloat(valueString);
215                         return new FloatElectricalCurrent(f, unit);
216                     }
217                 }
218             }
219             catch (Exception exception)
220             {
221                 throw new IllegalArgumentException("Error parsing FloatElectricalCurrent from " + text, exception);
222             }
223         }
224         throw new IllegalArgumentException("Error parsing FloatElectricalCurrent from " + text);
225     }
226 
227     /**
228      * Calculate the division of FloatElectricalCurrent and FloatElectricalCurrent, which results in a FloatDimensionless
229      * scalar.
230      * @param v FloatElectricalCurrent scalar
231      * @return FloatDimensionless scalar as a division of FloatElectricalCurrent and FloatElectricalCurrent
232      */
233     public final FloatDimensionless divideBy(final FloatElectricalCurrent v)
234     {
235         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
236     }
237 
238     /**
239      * Calculate the multiplication of FloatElectricalCurrent and FloatElectricalPotential, which results in a FloatPower
240      * scalar.
241      * @param v FloatElectricalCurrent scalar
242      * @return FloatPower scalar as a multiplication of FloatElectricalCurrent and FloatElectricalPotential
243      */
244     public final FloatPower multiplyBy(final FloatElectricalPotential v)
245     {
246         return new FloatPower(this.si * v.si, PowerUnit.SI);
247     }
248 
249     /**
250      * Calculate the multiplication of FloatElectricalCurrent and FloatDuration, which results in a FloatElectricalCharge
251      * scalar.
252      * @param v FloatElectricalCurrent scalar
253      * @return FloatElectricalCharge scalar as a multiplication of FloatElectricalCurrent and FloatDuration
254      */
255     public final FloatElectricalCharge multiplyBy(final FloatDuration v)
256     {
257         return new FloatElectricalCharge(this.si * v.si, ElectricalChargeUnit.SI);
258     }
259 
260     /**
261      * Calculate the multiplication of FloatElectricalCurrent and FloatElectricalResistance, which results in a
262      * FloatElectricalPotential scalar.
263      * @param v FloatElectricalCurrent scalar
264      * @return FloatElectricalPotential scalar as a multiplication of FloatElectricalCurrent and FloatElectricalResistance
265      */
266     public final FloatElectricalPotential multiplyBy(final FloatElectricalResistance v)
267     {
268         return new FloatElectricalPotential(this.si * v.si, ElectricalPotentialUnit.SI);
269     }
270 
271 }