View Javadoc
1   package org.djunits.value.vfloat.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 FloatScalar, which is relative by definition. An example is Speed. Instead of:
9    * 
10   * <pre>
11   * FloatScalar.Rel&lt;DensityUnit&gt; value = new FloatScalar.Rel&lt;DensityUnit&gt;(100.0, DensityUnit.SI);
12   * </pre>
13   * 
14   * we can now write:
15   * 
16   * <pre>
17   * FloatDensity value = new FloatDensity(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 FloatDensity extends AbstractFloatScalarRel<DensityUnit, FloatDensity>
32  {
33      /** */
34      private static final long serialVersionUID = 20150901L;
35  
36      /** constant with value zero. */
37      public static final FloatDensity ZERO = new FloatDensity(0.0f, DensityUnit.SI);
38  
39      /** constant with value NaN. */
40      @SuppressWarnings("checkstyle:constantname")
41      public static final FloatDensity NaN = new FloatDensity(Float.NaN, DensityUnit.SI);
42  
43      /** constant with value POSITIVE_INFINITY. */
44      public static final FloatDensity POSITIVE_INFINITY = new FloatDensity(Float.POSITIVE_INFINITY, DensityUnit.SI);
45  
46      /** constant with value NEGATIVE_INFINITY. */
47      public static final FloatDensity NEGATIVE_INFINITY = new FloatDensity(Float.NEGATIVE_INFINITY, DensityUnit.SI);
48  
49      /** constant with value MAX_VALUE. */
50      public static final FloatDensity POS_MAXVALUE = new FloatDensity(Float.MAX_VALUE, DensityUnit.SI);
51  
52      /** constant with value -MAX_VALUE. */
53      public static final FloatDensity NEG_MAXVALUE = new FloatDensity(-Float.MAX_VALUE, DensityUnit.SI);
54  
55      /**
56       * Construct FloatDensity scalar.
57       * @param value float value
58       * @param unit unit for the float value
59       */
60      public FloatDensity(final float value, final DensityUnit unit)
61      {
62          super(value, unit);
63      }
64  
65      /**
66       * Construct FloatDensity scalar.
67       * @param value Scalar from which to construct this instance
68       */
69      public FloatDensity(final FloatDensity value)
70      {
71          super(value);
72      }
73  
74      /**
75       * Construct FloatDensity scalar using a double value.
76       * @param value double value
77       * @param unit unit for the resulting float value
78       */
79      public FloatDensity(final double value, final DensityUnit unit)
80      {
81          super((float) value, unit);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public final FloatDensity instantiateRel(final float value, final DensityUnit unit)
87      {
88          return new FloatDensity(value, unit);
89      }
90  
91      /**
92       * Construct FloatDensity scalar.
93       * @param value float value in SI units
94       * @return the new scalar with the SI value
95       */
96      public static final FloatDensity createSI(final float value)
97      {
98          return new FloatDensity(value, DensityUnit.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 FloatDensity interpolate(final FloatDensity zero, final FloatDensity one, final float ratio)
109     {
110         return new FloatDensity(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
111     }
112 
113     /**
114      * Return the maximum value of two relative scalars.
115      * @param r1 the first scalar
116      * @param r2 the second scalar
117      * @return the maximum value of two relative scalars
118      */
119     public static FloatDensity max(final FloatDensity r1, final FloatDensity r2)
120     {
121         return (r1.gt(r2)) ? r1 : r2;
122     }
123 
124     /**
125      * Return the maximum value of more than two relative scalars.
126      * @param r1 the first scalar
127      * @param r2 the second scalar
128      * @param rn the other scalars
129      * @return the maximum value of more than two relative scalars
130      */
131     public static FloatDensity max(final FloatDensity r1, final FloatDensity r2, final FloatDensity... rn)
132     {
133         FloatDensity maxr = (r1.gt(r2)) ? r1 : r2;
134         for (FloatDensity r : rn)
135         {
136             if (r.gt(maxr))
137             {
138                 maxr = r;
139             }
140         }
141         return maxr;
142     }
143 
144     /**
145      * Return the minimum value of two relative scalars.
146      * @param r1 the first scalar
147      * @param r2 the second scalar
148      * @return the minimum value of two relative scalars
149      */
150     public static FloatDensity min(final FloatDensity r1, final FloatDensity r2)
151     {
152         return (r1.lt(r2)) ? r1 : r2;
153     }
154 
155     /**
156      * Return the minimum value of more than two relative scalars.
157      * @param r1 the first scalar
158      * @param r2 the second scalar
159      * @param rn the other scalars
160      * @return the minimum value of more than two relative scalars
161      */
162     public static FloatDensity min(final FloatDensity r1, final FloatDensity r2, final FloatDensity... rn)
163     {
164         FloatDensity minr = (r1.lt(r2)) ? r1 : r2;
165         for (FloatDensity r : rn)
166         {
167             if (r.lt(minr))
168             {
169                 minr = r;
170             }
171         }
172         return minr;
173     }
174 
175     /**
176      * Calculate the division of FloatDensity and FloatDensity, which results in a FloatDimensionless scalar.
177      * @param v FloatDensity scalar
178      * @return FloatDimensionless scalar as a division of FloatDensity and FloatDensity
179      */
180     public final FloatDimensionless divideBy(final FloatDensity v)
181     {
182         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
183     }
184 
185     /**
186      * Calculate the multiplication of FloatDensity and FloatVolume, which results in a FloatMass scalar.
187      * @param v FloatDensity scalar
188      * @return FloatMass scalar as a multiplication of FloatDensity and FloatVolume
189      */
190     public final FloatMass multiplyBy(final FloatVolume v)
191     {
192         return new FloatMass(this.si * v.si, MassUnit.SI);
193     }
194 
195 }