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