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.MomentumUnit;
15  import org.djunits.unit.VolumeUnit;
16  import org.djunits.unit.si.SIPrefixes;
17  import org.djunits.value.util.ValueUtil;
18  import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarRel;
19  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
20  
21  /**
22   * Easy access methods for the Mass DoubleScalar, which is relative by definition.
23   * <p>
24   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
26   * </p>
27   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
29   */
30  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2022-03-14T11:14:15.180987200Z")
31  public class Mass extends AbstractDoubleScalarRel<MassUnit, Mass>
32  {
33      /** */
34      private static final long serialVersionUID = 20150905L;
35  
36      /** Constant with value zero. */
37      public static final Massuble/scalar/Mass.html#Mass">Mass ZERO = new Mass(0.0, MassUnit.SI);
38  
39      /** Constant with value one. */
40      public static final Massouble/scalar/Mass.html#Mass">Mass ONE = new Mass(1.0, MassUnit.SI);
41  
42      /** Constant with value NaN. */
43      @SuppressWarnings("checkstyle:constantname")
44      public static final Massouble/scalar/Mass.html#Mass">Mass NaN = new Mass(Double.NaN, MassUnit.SI);
45  
46      /** Constant with value POSITIVE_INFINITY. */
47      public static final Massass.html#Mass">Mass POSITIVE_INFINITY = new Mass(Double.POSITIVE_INFINITY, MassUnit.SI);
48  
49      /** Constant with value NEGATIVE_INFINITY. */
50      public static final Massass.html#Mass">Mass NEGATIVE_INFINITY = new Mass(Double.NEGATIVE_INFINITY, MassUnit.SI);
51  
52      /** Constant with value MAX_VALUE. */
53      public static final Masslar/Mass.html#Mass">Mass POS_MAXVALUE = new Mass(Double.MAX_VALUE, MassUnit.SI);
54  
55      /** Constant with value -MAX_VALUE. */
56      public static final Masslar/Mass.html#Mass">Mass NEG_MAXVALUE = new Mass(-Double.MAX_VALUE, MassUnit.SI);
57  
58      /**
59       * Construct Mass scalar.
60       * @param value double; the double value
61       * @param unit MassUnit; unit for the double value
62       */
63      public Mass(final double value, final MassUnit unit)
64      {
65          super(value, unit);
66      }
67  
68      /**
69       * Construct Mass scalar.
70       * @param value Mass; Scalar from which to construct this instance
71       */
72      public Masse/vdouble/scalar/Mass.html#Mass">Mass(final Mass value)
73      {
74          super(value);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public final Mass instantiateRel(final double value, final MassUnit unit)
80      {
81          return new Mass(value, unit);
82      }
83  
84      /**
85       * Construct Mass scalar.
86       * @param value double; the double value in SI units
87       * @return Mass; the new scalar with the SI value
88       */
89      public static final Mass instantiateSI(final double value)
90      {
91          return new Mass(value, MassUnit.SI);
92      }
93  
94      /**
95       * Interpolate between two values.
96       * @param zero Mass; the low value
97       * @param one Mass; the high value
98       * @param ratio double; the ratio between 0 and 1, inclusive
99       * @return Mass; a Scalar at the ratio between
100      */
101     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)
102     {
103         return new Mass(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
104     }
105 
106     /**
107      * Return the maximum value of two relative scalars.
108      * @param r1 Mass; the first scalar
109      * @param r2 Mass; the second scalar
110      * @return Mass; the maximum value of two relative scalars
111      */
112     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)
113     {
114         return r1.gt(r2) ? r1 : r2;
115     }
116 
117     /**
118      * Return the maximum value of more than two relative scalars.
119      * @param r1 Mass; the first scalar
120      * @param r2 Mass; the second scalar
121      * @param rn Mass...; the other scalars
122      * @return Mass; the maximum value of more than two relative scalars
123      */
124     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)
125     {
126         Mass maxr = r1.gt(r2) ? r1 : r2;
127         for (Mass r : rn)
128         {
129             if (r.gt(maxr))
130             {
131                 maxr = r;
132             }
133         }
134         return maxr;
135     }
136 
137     /**
138      * Return the minimum value of two relative scalars.
139      * @param r1 Mass; the first scalar
140      * @param r2 Mass; the second scalar
141      * @return Mass; the minimum value of two relative scalars
142      */
143     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)
144     {
145         return r1.lt(r2) ? r1 : r2;
146     }
147 
148     /**
149      * Return the minimum value of more than two relative scalars.
150      * @param r1 Mass; the first scalar
151      * @param r2 Mass; the second scalar
152      * @param rn Mass...; the other scalars
153      * @return Mass; the minimum value of more than two relative scalars
154      */
155     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)
156     {
157         Mass minr = r1.lt(r2) ? r1 : r2;
158         for (Mass r : rn)
159         {
160             if (r.lt(minr))
161             {
162                 minr = r;
163             }
164         }
165         return minr;
166     }
167 
168     /**
169      * Returns a Mass representation of a textual representation of a value with a unit. The String representation that can be
170      * parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
171      * required, between the value and the unit.
172      * @param text String; the textual representation to parse into a Mass
173      * @return Mass; the Scalar representation of the value in its unit
174      * @throws IllegalArgumentException when the text cannot be parsed
175      * @throws NullPointerException when the text argument is null
176      */
177     public static Mass valueOf(final String text)
178     {
179         Throw.whenNull(text, "Error parsing Mass: text to parse is null");
180         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Mass: empty text to parse");
181         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
182         if (matcher.find())
183         {
184             int index = matcher.end();
185             String unitString = text.substring(index).trim();
186             String valueString = text.substring(0, index).trim();
187             MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
188             if (unit != null)
189             {
190                 double d = Double.parseDouble(valueString);
191                 return new Mass(d, unit);
192             }
193         }
194         throw new IllegalArgumentException("Error parsing Mass from " + text);
195     }
196 
197     /**
198      * Returns a Mass based on a value and the textual representation of the unit.
199      * @param value double; the value to use
200      * @param unitString String; the textual representation of the unit
201      * @return Mass; the Scalar representation of the value in its unit
202      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
203      * @throws NullPointerException when the unitString argument is null
204      */
205     public static Mass of(final double value, final String unitString)
206     {
207         Throw.whenNull(unitString, "Error parsing Mass: unitString is null");
208         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Mass: empty unitString");
209         MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
210         if (unit != null)
211         {
212             return new Mass(value, unit);
213         }
214         throw new IllegalArgumentException("Error parsing Mass with unit " + unitString);
215     }
216 
217     /** {@inheritDoc} */
218     @Override
219     public String toStringSIPrefixed(final int smallestPower, final int biggestPower)
220     {
221         if (!Double.isFinite(this.si))
222         {
223             return toString(getDisplayUnit().getStandardUnit());
224         }
225         // PK: I can't think of an easier way to figure out what the exponent will be; rounding of the mantissa to the available
226         // width makes this hard; This feels like an expensive way.
227         String check = String.format(this.si >= 0 ? "%10.8E" : "%10.7E", this.si);
228         int exponent = Integer.parseInt(check.substring(check.indexOf("E") + 1));
229         if (exponent < -27 || exponent < smallestPower || exponent > 21 + 2 || exponent > biggestPower)
230         {
231             // Out of SI prefix range; do not scale.
232             return String.format(this.si >= 0 ? "%10.4E" : "%10.3E", this.si) + " "
233                     + getDisplayUnit().getStandardUnit().getId();
234         }
235         Integer roundedExponent = (int) Math.ceil((exponent - 2.0) / 3) * 3 + 3;
236         // System.out.print(String.format("exponent=%d; roundedExponent=%d ", exponent, roundedExponent));
237         String key = SIPrefixes.FACTORS.get(roundedExponent).getDefaultTextualPrefix() + "g";
238         MassUnit displayUnit = getDisplayUnit().getQuantity().getUnitByAbbreviation(key);
239         return toString(displayUnit);
240     }
241 
242     /**
243      * Calculate the division of Mass and Mass, which results in a Dimensionless scalar.
244      * @param v Mass; scalar
245      * @return Dimensionless; scalar as a division of Mass and Mass
246      */
247     public final Dimensionless divide(final Mass v)
248     {
249         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
250     }
251 
252     /**
253      * Calculate the division of Mass and FlowMass, which results in a Duration scalar.
254      * @param v Mass; scalar
255      * @return Duration; scalar as a division of Mass and FlowMass
256      */
257     public final Duration divide(final FlowMass v)
258     {
259         return new Duration(this.si / v.si, DurationUnit.SI);
260     }
261 
262     /**
263      * Calculate the division of Mass and Duration, which results in a FlowMass scalar.
264      * @param v Mass; scalar
265      * @return FlowMass; scalar as a division of Mass and Duration
266      */
267     public final FlowMass divide(final Duration v)
268     {
269         return new FlowMass(this.si / v.si, FlowMassUnit.SI);
270     }
271 
272     /**
273      * Calculate the multiplication of Mass and Acceleration, which results in a Force scalar.
274      * @param v Mass; scalar
275      * @return Force; scalar as a multiplication of Mass and Acceleration
276      */
277     public final Force times(final Acceleration v)
278     {
279         return new Force(this.si * v.si, ForceUnit.SI);
280     }
281 
282     /**
283      * Calculate the multiplication of Mass and Frequency, which results in a FlowMass scalar.
284      * @param v Mass; scalar
285      * @return FlowMass; scalar as a multiplication of Mass and Frequency
286      */
287     public final FlowMass times(final Frequency v)
288     {
289         return new FlowMass(this.si * v.si, FlowMassUnit.SI);
290     }
291 
292     /**
293      * Calculate the division of Mass and Density, which results in a Volume scalar.
294      * @param v Mass; scalar
295      * @return Volume; scalar as a division of Mass and Density
296      */
297     public final Volume divide(final Density v)
298     {
299         return new Volume(this.si / v.si, VolumeUnit.SI);
300     }
301 
302     /**
303      * Calculate the division of Mass and Volume, which results in a Density scalar.
304      * @param v Mass; scalar
305      * @return Density; scalar as a division of Mass and Volume
306      */
307     public final Density divide(final Volume v)
308     {
309         return new Density(this.si / v.si, DensityUnit.SI);
310     }
311 
312     /**
313      * Calculate the multiplication of Mass and Speed, which results in a Momentum scalar.
314      * @param v Mass; scalar
315      * @return Momentum; scalar as a multiplication of Mass and Speed
316      */
317     public final Momentum times(final Speed v)
318     {
319         return new Momentum(this.si * v.si, MomentumUnit.SI);
320     }
321 
322     /** {@inheritDoc} */
323     @Override
324     public SIScalar reciprocal()
325     {
326         return DoubleScalar.divide(Dimensionless.ONE, this);
327     }
328 
329 }