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