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