1 package org.djunits.value.vdouble.scalar;
2
3 import org.djunits.unit.DensityUnit;
4 import org.djunits.unit.DimensionlessUnit;
5 import org.djunits.unit.MassUnit;
6
7 /**
8 * Easy access methods for the Density DoubleScalar, which is relative by definition. Instead of:
9 *
10 * <pre>
11 * DoubleScalar.Rel<DensityUnit> value = new DoubleScalar.Rel<DensityUnit>(100.0, DensityUnit.SI);
12 * </pre>
13 *
14 * we can now write:
15 *
16 * <pre>
17 * Density value = new Density(100.0, DensityUnit.SI);
18 * </pre>
19 *
20 * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
21 * used are compatible.
22 * <p>
23 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24 * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
25 * <p>
26 * $LastChangedDate: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $,
27 * initial version Sep 5, 2015 <br>
28 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30 */
31 public class Density extends AbstractDoubleScalarRel<DensityUnit, Density>
32 {
33 /** */
34 private static final long serialVersionUID = 20150905L;
35
36 /** constant with value zero. */
37 public static final Density ZERO = new Density(0.0, DensityUnit.SI);
38
39 /** constant with value NaN. */
40 @SuppressWarnings("checkstyle:constantname")
41 public static final Density NaN = new Density(Double.NaN, DensityUnit.SI);
42
43 /** constant with value POSITIVE_INFINITY. */
44 public static final Density POSITIVE_INFINITY = new Density(Double.POSITIVE_INFINITY, DensityUnit.SI);
45
46 /** constant with value NEGATIVE_INFINITY. */
47 public static final Density NEGATIVE_INFINITY = new Density(Double.NEGATIVE_INFINITY, DensityUnit.SI);
48
49 /** constant with value MAX_VALUE. */
50 public static final Density POS_MAXVALUE = new Density(Double.MAX_VALUE, DensityUnit.SI);
51
52 /** constant with value -MAX_VALUE. */
53 public static final Density NEG_MAXVALUE = new Density(-Double.MAX_VALUE, DensityUnit.SI);
54
55 /**
56 * Construct Density scalar.
57 * @param value double value
58 * @param unit unit for the double value
59 */
60 public Density(final double value, final DensityUnit unit)
61 {
62 super(value, unit);
63 }
64
65 /**
66 * Construct Density scalar.
67 * @param value Scalar from which to construct this instance
68 */
69 public Density(final Density value)
70 {
71 super(value);
72 }
73
74 /** {@inheritDoc} */
75 @Override
76 public final Density instantiateRel(final double value, final DensityUnit unit)
77 {
78 return new Density(value, unit);
79 }
80
81 /**
82 * Construct Density scalar.
83 * @param value double value in SI units
84 * @return the new scalar with the SI value
85 */
86 public static final Density createSI(final double value)
87 {
88 return new Density(value, DensityUnit.SI);
89 }
90
91 /**
92 * Interpolate between two values.
93 * @param zero the low value
94 * @param one the high value
95 * @param ratio the ratio between 0 and 1, inclusive
96 * @return a Scalar at the ratio between
97 */
98 public static Density interpolate(final Density zero, final Density one, final double ratio)
99 {
100 return new Density(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
101 }
102
103 /**
104 * Return the maximum value of two relative scalars.
105 * @param r1 the first scalar
106 * @param r2 the second scalar
107 * @return the maximum value of two relative scalars
108 */
109 public static Density max(final Density r1, final Density r2)
110 {
111 return (r1.gt(r2)) ? r1 : r2;
112 }
113
114 /**
115 * Return the maximum value of more than two relative scalars.
116 * @param r1 the first scalar
117 * @param r2 the second scalar
118 * @param rn the other scalars
119 * @return the maximum value of more than two relative scalars
120 */
121 public static Density max(final Density r1, final Density r2, final Density... rn)
122 {
123 Density maxr = (r1.gt(r2)) ? r1 : r2;
124 for (Density r : rn)
125 {
126 if (r.gt(maxr))
127 {
128 maxr = r;
129 }
130 }
131 return maxr;
132 }
133
134 /**
135 * Return the minimum value of two relative scalars.
136 * @param r1 the first scalar
137 * @param r2 the second scalar
138 * @return the minimum value of two relative scalars
139 */
140 public static Density min(final Density r1, final Density r2)
141 {
142 return (r1.lt(r2)) ? r1 : r2;
143 }
144
145 /**
146 * Return the minimum value of more than two relative scalars.
147 * @param r1 the first scalar
148 * @param r2 the second scalar
149 * @param rn the other scalars
150 * @return the minimum value of more than two relative scalars
151 */
152 public static Density min(final Density r1, final Density r2, final Density... rn)
153 {
154 Density minr = (r1.lt(r2)) ? r1 : r2;
155 for (Density r : rn)
156 {
157 if (r.lt(minr))
158 {
159 minr = r;
160 }
161 }
162 return minr;
163 }
164
165 /**
166 * Calculate the division of Density and Density, which results in a Dimensionless scalar.
167 * @param v Density scalar
168 * @return Dimensionless scalar as a division of Density and Density
169 */
170 public final Dimensionless divideBy(final Density v)
171 {
172 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
173 }
174
175 /**
176 * Calculate the multiplication of Density and Volume, which results in a Mass scalar.
177 * @param v Density scalar
178 * @return Mass scalar as a multiplication of Density and Volume
179 */
180 public final Mass multiplyBy(final Volume v)
181 {
182 return new Mass(this.si * v.si, MassUnit.SI);
183 }
184
185 }