View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import javax.annotation.Generated;
6   
7   import org.djunits.Throw;
8   import org.djunits.unit.DensityUnit;
9   import org.djunits.unit.DimensionlessUnit;
10  import org.djunits.unit.DurationUnit;
11  import org.djunits.unit.FlowMassUnit;
12  import org.djunits.unit.ForceUnit;
13  import org.djunits.unit.MassUnit;
14  import org.djunits.unit.VolumeUnit;
15  import org.djunits.value.util.ValueUtil;
16  import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarRel;
17  
18  /**
19   * Easy access methods for the Mass DoubleScalar, which is relative by definition.
20   * <p>
21   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
23   * </p>
24   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
26   */
27  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2019-10-18T12:12:25.568Z")
28  public class Mass extends AbstractDoubleScalarRel<MassUnit, Mass>
29  {
30      /** */
31      private static final long serialVersionUID = 20150905L;
32  
33      /** Constant with value zero. */
34      public static final Massuble/scalar/Mass.html#Mass">Mass ZERO = new Mass(0.0, MassUnit.SI);
35  
36      /** Constant with value one. */
37      public static final Massouble/scalar/Mass.html#Mass">Mass ONE = new Mass(1.0, MassUnit.SI);
38  
39      /** Constant with value NaN. */
40      @SuppressWarnings("checkstyle:constantname")
41      public static final Massouble/scalar/Mass.html#Mass">Mass NaN = new Mass(Double.NaN, MassUnit.SI);
42  
43      /** Constant with value POSITIVE_INFINITY. */
44      public static final Massass.html#Mass">Mass POSITIVE_INFINITY = new Mass(Double.POSITIVE_INFINITY, MassUnit.SI);
45  
46      /** Constant with value NEGATIVE_INFINITY. */
47      public static final Massass.html#Mass">Mass NEGATIVE_INFINITY = new Mass(Double.NEGATIVE_INFINITY, MassUnit.SI);
48  
49      /** Constant with value MAX_VALUE. */
50      public static final Masslar/Mass.html#Mass">Mass POS_MAXVALUE = new Mass(Double.MAX_VALUE, MassUnit.SI);
51  
52      /** Constant with value -MAX_VALUE. */
53      public static final Masslar/Mass.html#Mass">Mass NEG_MAXVALUE = new Mass(-Double.MAX_VALUE, MassUnit.SI);
54  
55      /**
56       * Construct Mass scalar.
57       * @param value double; the double value
58       * @param unit MassUnit; unit for the double value
59       */
60      public Mass(final double value, final MassUnit unit)
61      {
62          super(value, unit);
63      }
64  
65      /**
66       * Construct Mass scalar.
67       * @param value Mass; Scalar from which to construct this instance
68       */
69      public Masse/vdouble/scalar/Mass.html#Mass">Mass(final Mass value)
70      {
71          super(value);
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public final Mass instantiateRel(final double value, final MassUnit unit)
77      {
78          return new Mass(value, unit);
79      }
80  
81      /**
82       * Construct Mass scalar.
83       * @param value double; the double value in SI units
84       * @return Mass; the new scalar with the SI value
85       */
86      public static final Mass instantiateSI(final double value)
87      {
88          return new Mass(value, MassUnit.SI);
89      }
90  
91      /**
92       * Interpolate between two values.
93       * @param zero Mass; the low value
94       * @param one Mass; the high value
95       * @param ratio double; the ratio between 0 and 1, inclusive
96       * @return Mass; a Scalar at the ratio between
97       */
98      public static Massble/scalar/Mass.html#Mass">Massalar/Mass.html#Mass">Mass interpolate(final Massble/scalar/Mass.html#Mass">Mass zero, final Mass one, final double ratio)
99      {
100         return new Mass(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
101     }
102 
103     /**
104      * Return the maximum value of two relative scalars.
105      * @param r1 Mass; the first scalar
106      * @param r2 Mass; the second scalar
107      * @return Mass; the maximum value of two relative scalars
108      */
109     public static Massouble/scalar/Mass.html#Mass">Massouble/scalar/Mass.html#Mass">Mass max(final Massouble/scalar/Mass.html#Mass">Mass r1, final Mass 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 Mass; the first scalar
117      * @param r2 Mass; the second scalar
118      * @param rn Mass...; the other scalars
119      * @return Mass; the maximum value of more than two relative scalars
120      */
121     public static Massouble/scalar/Mass.html#Mass">Massouble/scalar/Mass.html#Mass">Mass max(final Massouble/scalar/Mass.html#Mass">Mass r1, final Mass r2, final Mass... rn)
122     {
123         Mass maxr = (r1.gt(r2)) ? r1 : r2;
124         for (Mass 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 Mass; the first scalar
137      * @param r2 Mass; the second scalar
138      * @return Mass; the minimum value of two relative scalars
139      */
140     public static Massouble/scalar/Mass.html#Mass">Massouble/scalar/Mass.html#Mass">Mass min(final Massouble/scalar/Mass.html#Mass">Mass r1, final Mass 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 Mass; the first scalar
148      * @param r2 Mass; the second scalar
149      * @param rn Mass...; the other scalars
150      * @return Mass; the minimum value of more than two relative scalars
151      */
152     public static Massouble/scalar/Mass.html#Mass">Massouble/scalar/Mass.html#Mass">Mass min(final Massouble/scalar/Mass.html#Mass">Mass r1, final Mass r2, final Mass... rn)
153     {
154         Mass minr = (r1.lt(r2)) ? r1 : r2;
155         for (Mass r : rn)
156         {
157             if (r.lt(minr))
158             {
159                 minr = r;
160             }
161         }
162         return minr;
163     }
164 
165     /**
166      * Returns a Mass representation of a textual representation of a value with a unit. The String representation that can be
167      * parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
168      * required, between the value and the unit.
169      * @param text String; the textual representation to parse into a Mass
170      * @return Mass; the Scalar representation of the value in its unit
171      * @throws IllegalArgumentException when the text cannot be parsed
172      * @throws NullPointerException when the text argument is null
173      */
174     public static Mass valueOf(final String text)
175     {
176         Throw.whenNull(text, "Error parsing Mass: text to parse is null");
177         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Mass: empty text to parse");
178         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
179         if (matcher.find())
180         {
181             int index = matcher.end();
182             String unitString = text.substring(index).trim();
183             String valueString = text.substring(0, index).trim();
184             MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
185             if (unit != null)
186             {
187                 double d = Double.parseDouble(valueString);
188                 return new Mass(d, unit);
189             }
190         }
191         throw new IllegalArgumentException("Error parsing Mass from " + text);
192     }
193 
194     /**
195      * Returns a Mass based on a value and the textual representation of the unit.
196      * @param value double; the value to use
197      * @param unitString String; the textual representation of the unit
198      * @return Mass; the Scalar representation of the value in its unit
199      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
200      * @throws NullPointerException when the unitString argument is null
201      */
202     public static Mass of(final double value, final String unitString)
203     {
204         Throw.whenNull(unitString, "Error parsing Mass: unitString is null");
205         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Mass: empty unitString");
206         MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
207         if (unit != null)
208         {
209             return new Mass(value, unit);
210         }
211         throw new IllegalArgumentException("Error parsing Mass with unit " + unitString);
212     }
213 
214     /**
215      * Calculate the division of Mass and Mass, which results in a Dimensionless scalar.
216      * @param v Mass scalar
217      * @return Dimensionless scalar as a division of Mass and Mass
218      */
219     public final Dimensionless divide(final Mass v)
220     {
221         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
222     }
223 
224     /**
225      * Calculate the division of Mass and FlowMass, which results in a Duration scalar.
226      * @param v Mass scalar
227      * @return Duration scalar as a division of Mass and FlowMass
228      */
229     public final Duration divide(final FlowMass v)
230     {
231         return new Duration(this.si / v.si, DurationUnit.SI);
232     }
233 
234     /**
235      * Calculate the division of Mass and Duration, which results in a FlowMass scalar.
236      * @param v Mass scalar
237      * @return FlowMass scalar as a division of Mass and Duration
238      */
239     public final FlowMass divide(final Duration v)
240     {
241         return new FlowMass(this.si / v.si, FlowMassUnit.SI);
242     }
243 
244     /**
245      * Calculate the multiplication of Mass and Acceleration, which results in a Force scalar.
246      * @param v Mass scalar
247      * @return Force scalar as a multiplication of Mass and Acceleration
248      */
249     public final Force times(final Acceleration v)
250     {
251         return new Force(this.si * v.si, ForceUnit.SI);
252     }
253 
254     /**
255      * Calculate the multiplication of Mass and Frequency, which results in a FlowMass scalar.
256      * @param v Mass scalar
257      * @return FlowMass scalar as a multiplication of Mass and Frequency
258      */
259     public final FlowMass times(final Frequency v)
260     {
261         return new FlowMass(this.si * v.si, FlowMassUnit.SI);
262     }
263 
264     /**
265      * Calculate the division of Mass and Density, which results in a Volume scalar.
266      * @param v Mass scalar
267      * @return Volume scalar as a division of Mass and Density
268      */
269     public final Volume divide(final Density v)
270     {
271         return new Volume(this.si / v.si, VolumeUnit.SI);
272     }
273 
274     /**
275      * Calculate the division of Mass and Volume, which results in a Density scalar.
276      * @param v Mass scalar
277      * @return Density scalar as a division of Mass and Volume
278      */
279     public final Density divide(final Volume v)
280     {
281         return new Density(this.si / v.si, DensityUnit.SI);
282     }
283 
284 }