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