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