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