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