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