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.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 DoubleScalar, which is relative by definition. Instead of:
13   * 
14   * <pre>
15   * DoubleScalar.Rel&lt;ElectricalChargeUnit&gt; value = new DoubleScalar.Rel&lt;ElectricalChargeUnit&gt;(100.0, ElectricalChargeUnit.SI);
16   * </pre>
17   * 
18   * we can now write:
19   * 
20   * <pre>
21   * ElectricalCharge value = new ElectricalCharge(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 ElectricalCharge extends AbstractDoubleScalarRel<ElectricalChargeUnit, ElectricalCharge>
36  {
37      /** */
38      private static final long serialVersionUID = 20150905L;
39  
40      /** constant with value zero. */
41      public static final ElectricalCharge ZERO = new ElectricalCharge(0.0, ElectricalChargeUnit.SI);
42  
43      /** constant with value NaN. */
44      @SuppressWarnings("checkstyle:constantname")
45      public static final ElectricalCharge NaN = new ElectricalCharge(Double.NaN, ElectricalChargeUnit.SI);
46  
47      /** constant with value POSITIVE_INFINITY. */
48      public static final ElectricalCharge POSITIVE_INFINITY =
49              new ElectricalCharge(Double.POSITIVE_INFINITY, ElectricalChargeUnit.SI);
50  
51      /** constant with value NEGATIVE_INFINITY. */
52      public static final ElectricalCharge NEGATIVE_INFINITY =
53              new ElectricalCharge(Double.NEGATIVE_INFINITY, ElectricalChargeUnit.SI);
54  
55      /** constant with value MAX_VALUE. */
56      public static final ElectricalCharge POS_MAXVALUE = new ElectricalCharge(Double.MAX_VALUE, ElectricalChargeUnit.SI);
57  
58      /** constant with value -MAX_VALUE. */
59      public static final ElectricalCharge NEG_MAXVALUE = new ElectricalCharge(-Double.MAX_VALUE, ElectricalChargeUnit.SI);
60  
61      /**
62       * Construct ElectricalCharge scalar.
63       * @param value double value
64       * @param unit unit for the double value
65       */
66      public ElectricalCharge(final double value, final ElectricalChargeUnit unit)
67      {
68          super(value, unit);
69      }
70  
71      /**
72       * Construct ElectricalCharge scalar.
73       * @param value Scalar from which to construct this instance
74       */
75      public ElectricalCharge(final ElectricalCharge value)
76      {
77          super(value);
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final ElectricalCharge instantiateRel(final double value, final ElectricalChargeUnit unit)
83      {
84          return new ElectricalCharge(value, unit);
85      }
86  
87      /**
88       * Construct ElectricalCharge scalar.
89       * @param value double value in SI units
90       * @return the new scalar with the SI value
91       */
92      public static final ElectricalCharge createSI(final double value)
93      {
94          return new ElectricalCharge(value, ElectricalChargeUnit.SI);
95      }
96  
97      /**
98       * Interpolate between two values.
99       * @param zero the low value
100      * @param one the high value
101      * @param ratio the ratio between 0 and 1, inclusive
102      * @return a Scalar at the ratio between
103      */
104     public static ElectricalCharge interpolate(final ElectricalCharge zero, final ElectricalCharge one, final double ratio)
105     {
106         return new ElectricalCharge(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
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 ElectricalCharge max(final ElectricalCharge r1, final ElectricalCharge 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 ElectricalCharge max(final ElectricalCharge r1, final ElectricalCharge r2, final ElectricalCharge... rn)
128     {
129         ElectricalCharge maxr = (r1.gt(r2)) ? r1 : r2;
130         for (ElectricalCharge r : rn)
131         {
132             if (r.gt(maxr))
133             {
134                 maxr = r;
135             }
136         }
137         return maxr;
138     }
139 
140     /**
141      * Return the minimum value of two relative scalars.
142      * @param r1 the first scalar
143      * @param r2 the second scalar
144      * @return the minimum value of two relative scalars
145      */
146     public static ElectricalCharge min(final ElectricalCharge r1, final ElectricalCharge r2)
147     {
148         return (r1.lt(r2)) ? r1 : r2;
149     }
150 
151     /**
152      * Return the minimum value of more than two relative scalars.
153      * @param r1 the first scalar
154      * @param r2 the second scalar
155      * @param rn the other scalars
156      * @return the minimum value of more than two relative scalars
157      */
158     public static ElectricalCharge min(final ElectricalCharge r1, final ElectricalCharge r2, final ElectricalCharge... rn)
159     {
160         ElectricalCharge minr = (r1.lt(r2)) ? r1 : r2;
161         for (ElectricalCharge r : rn)
162         {
163             if (r.lt(minr))
164             {
165                 minr = r;
166             }
167         }
168         return minr;
169     }
170 
171     /**
172      * Returns a ElectricalCharge representation of a textual representation of a value with a unit. The String representation
173      * that can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are
174      * allowed, but not necessary, between the value and the unit.
175      * @param text String; the textual representation to parse into a ElectricalCharge
176      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
177      * @throws IllegalArgumentException when the text cannot be parsed
178      */
179     public static ElectricalCharge valueOf(final String text) throws IllegalArgumentException
180     {
181         if (text == null || text.length() == 0)
182         {
183             throw new IllegalArgumentException("Error parsing ElectricalCharge -- null or empty argument");
184         }
185         Matcher matcher = NUMBER_PATTERN.matcher(text);
186         if (matcher.find())
187         {
188             int index = matcher.end();
189             try
190             {
191                 String unitString = text.substring(index).trim();
192                 String valueString = text.substring(0, index).trim();
193                 for (ElectricalChargeUnit unit : Unit.getUnits(ElectricalChargeUnit.class))
194                 {
195                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
196                     {
197                         double d = Double.parseDouble(valueString);
198                         return new ElectricalCharge(d, unit);
199                     }
200                 }
201             }
202             catch (Exception exception)
203             {
204                 throw new IllegalArgumentException("Error parsing ElectricalCharge from " + text, exception);
205             }
206         }
207         throw new IllegalArgumentException("Error parsing ElectricalCharge from " + text);
208     }
209 
210     /**
211      * Calculate the division of ElectricalCharge and ElectricalCharge, which results in a Dimensionless scalar.
212      * @param v ElectricalCharge scalar
213      * @return Dimensionless scalar as a division of ElectricalCharge and ElectricalCharge
214      */
215     public final Dimensionless divideBy(final ElectricalCharge v)
216     {
217         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
218     }
219 
220     /**
221      * Calculate the division of ElectricalCharge and Duration, which results in a ElectricalCurrent scalar.
222      * @param v ElectricalCharge scalar
223      * @return ElectricalCurrent scalar as a division of ElectricalCharge and Duration
224      */
225     public final ElectricalCurrent divideBy(final Duration v)
226     {
227         return new ElectricalCurrent(this.si / v.si, ElectricalCurrentUnit.SI);
228     }
229 
230     /**
231      * Calculate the division of ElectricalCharge and ElectricalCurrent, which results in a Duration scalar.
232      * @param v ElectricalCharge scalar
233      * @return Duration scalar as a division of ElectricalCharge and ElectricalCurrent
234      */
235     public final Duration divideBy(final ElectricalCurrent v)
236     {
237         return new Duration(this.si / v.si, DurationUnit.SI);
238     }
239 
240 }