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