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