View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.AccelerationUnit;
6   import org.djunits.unit.DimensionlessUnit;
7   import org.djunits.unit.ForceUnit;
8   import org.djunits.unit.FrequencyUnit;
9   import org.djunits.unit.PowerUnit;
10  import org.djunits.unit.SpeedUnit;
11  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
12  import org.djutils.base.NumberParser;
13  import org.djutils.exceptions.Throw;
14  
15  import jakarta.annotation.Generated;
16  
17  /**
18   * Easy access methods for the Acceleration DoubleScalar, which is relative by definition.
19   * <p>
20   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
22   * </p>
23   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
25   */
26  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
27  public class Acceleration extends DoubleScalarRel<AccelerationUnit, Acceleration>
28  {
29      /** */
30      private static final long serialVersionUID = 20150905L;
31  
32      /** Constant with value zero. */
33      public static final Acceleration ZERO = new Acceleration(0.0, AccelerationUnit.SI);
34  
35      /** Constant with value one. */
36      public static final Acceleration ONE = new Acceleration(1.0, AccelerationUnit.SI);
37  
38      /** Constant with value NaN. */
39      @SuppressWarnings("checkstyle:constantname")
40      public static final Acceleration NaN = new Acceleration(Double.NaN, AccelerationUnit.SI);
41  
42      /** Constant with value POSITIVE_INFINITY. */
43      public static final Acceleration POSITIVE_INFINITY = new Acceleration(Double.POSITIVE_INFINITY, AccelerationUnit.SI);
44  
45      /** Constant with value NEGATIVE_INFINITY. */
46      public static final Acceleration NEGATIVE_INFINITY = new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
47  
48      /** Constant with value MAX_VALUE. */
49      public static final Acceleration POS_MAXVALUE = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
50  
51      /** Constant with value -MAX_VALUE. */
52      public static final Acceleration NEG_MAXVALUE = new Acceleration(-Double.MAX_VALUE, AccelerationUnit.SI);
53  
54      /**
55       * Construct Acceleration scalar with a unit.
56       * @param value the double value, expressed in the given unit
57       * @param unit unit for the double value
58       */
59      public Acceleration(final double value, final AccelerationUnit unit)
60      {
61          super(value, unit);
62      }
63  
64      /**
65       * Construct Acceleration scalar.
66       * @param value Scalar from which to construct this instance
67       */
68      public Acceleration(final Acceleration value)
69      {
70          super(value);
71      }
72  
73      @Override
74      public final Acceleration instantiateRel(final double value, final AccelerationUnit unit)
75      {
76          return new Acceleration(value, unit);
77      }
78  
79      /**
80       * Construct Acceleration scalar based on an SI value.
81       * @param value the double value in SI units
82       * @return the new scalar with the SI value
83       */
84      public static final Acceleration ofSI(final double value)
85      {
86          return new Acceleration(value, AccelerationUnit.SI);
87      }
88  
89      /**
90       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
91       * @param zero the value at a ratio of zero
92       * @param one the value at a ratio of one
93       * @param ratio the ratio between 0 and 1, inclusive
94       * @return a Acceleration at the given ratio between 0 and 1
95       */
96      public static Acceleration interpolate(final Acceleration zero, final Acceleration one, final double ratio)
97      {
98          Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
99                  "ratio for interpolation should be between 0 and 1, but is %f", ratio);
100         return new Acceleration(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
101                 zero.getDisplayUnit());
102     }
103 
104     /**
105      * Return the maximum value of two relative scalars.
106      * @param r1 the first scalar
107      * @param r2 the second scalar
108      * @return the maximum value of two relative scalars
109      */
110     public static Acceleration max(final Acceleration r1, final Acceleration r2)
111     {
112         return r1.gt(r2) ? r1 : r2;
113     }
114 
115     /**
116      * Return the maximum value of more than two relative scalars.
117      * @param r1 the first scalar
118      * @param r2 the second scalar
119      * @param rn the other scalars
120      * @return the maximum value of more than two relative scalars
121      */
122     public static Acceleration max(final Acceleration r1, final Acceleration r2, final Acceleration... rn)
123     {
124         Acceleration maxr = r1.gt(r2) ? r1 : r2;
125         for (Acceleration r : rn)
126         {
127             if (r.gt(maxr))
128             {
129                 maxr = r;
130             }
131         }
132         return maxr;
133     }
134 
135     /**
136      * Return the minimum value of two relative scalars.
137      * @param r1 the first scalar
138      * @param r2 the second scalar
139      * @return the minimum value of two relative scalars
140      */
141     public static Acceleration min(final Acceleration r1, final Acceleration r2)
142     {
143         return r1.lt(r2) ? r1 : r2;
144     }
145 
146     /**
147      * Return the minimum value of more than two relative scalars.
148      * @param r1 the first scalar
149      * @param r2 the second scalar
150      * @param rn the other scalars
151      * @return the minimum value of more than two relative scalars
152      */
153     public static Acceleration min(final Acceleration r1, final Acceleration r2, final Acceleration... rn)
154     {
155         Acceleration minr = r1.lt(r2) ? r1 : r2;
156         for (Acceleration r : rn)
157         {
158             if (r.lt(minr))
159             {
160                 minr = r;
161             }
162         }
163         return minr;
164     }
165 
166     /**
167      * Returns a Acceleration representation of a textual representation of a value with a unit. The String representation that
168      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
169      * allowed, but not required, between the value and the unit.
170      * @param text the textual representation to parse into a Acceleration
171      * @return the Scalar representation of the value in its unit
172      * @throws IllegalArgumentException when the text cannot be parsed
173      * @throws NullPointerException when the text argument is null
174      */
175     public static Acceleration valueOf(final String text)
176     {
177         Throw.whenNull(text, "Error parsing Acceleration: text to parse is null");
178         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Acceleration: empty text to parse");
179         try
180         {
181             NumberParser numberParser = new NumberParser().lenient().trailing();
182             double d = numberParser.parseDouble(text);
183             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
184             AccelerationUnit unit = AccelerationUnit.BASE.getUnitByAbbreviation(unitString);
185             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Acceleration", unitString);
186             return new Acceleration(d, unit);
187         }
188         catch (Exception exception)
189         {
190             throw new IllegalArgumentException(
191                     "Error parsing Acceleration from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
192                     exception);
193         }
194     }
195 
196     /**
197      * Returns a Acceleration based on a value and the textual representation of the unit, which can be localized.
198      * @param value the value to use
199      * @param unitString the textual representation of the unit
200      * @return the Scalar representation of the value in its unit
201      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
202      * @throws NullPointerException when the unitString argument is null
203      */
204     public static Acceleration of(final double value, final String unitString)
205     {
206         Throw.whenNull(unitString, "Error parsing Acceleration: unitString is null");
207         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Acceleration: empty unitString");
208         AccelerationUnit unit = AccelerationUnit.BASE.getUnitByAbbreviation(unitString);
209         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Acceleration with unit %s", unitString);
210         return new Acceleration(value, unit);
211     }
212 
213     /**
214      * Calculate the division of Acceleration and Acceleration, which results in a Dimensionless scalar.
215      * @param v scalar
216      * @return scalar as a division of Acceleration and Acceleration
217      */
218     public final Dimensionless divide(final Acceleration v)
219     {
220         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
221     }
222 
223     /**
224      * Calculate the multiplication of Acceleration and Mass, which results in a Force scalar.
225      * @param v scalar
226      * @return scalar as a multiplication of Acceleration and Mass
227      */
228     public final Force times(final Mass v)
229     {
230         return new Force(this.si * v.si, ForceUnit.SI);
231     }
232 
233     /**
234      * Calculate the multiplication of Acceleration and Duration, which results in a Speed scalar.
235      * @param v scalar
236      * @return scalar as a multiplication of Acceleration and Duration
237      */
238     public final Speed times(final Duration v)
239     {
240         return new Speed(this.si * v.si, SpeedUnit.SI);
241     }
242 
243     /**
244      * Calculate the division of Acceleration and Frequency, which results in a Speed scalar.
245      * @param v scalar
246      * @return scalar as a division of Acceleration and Frequency
247      */
248     public final Speed divide(final Frequency v)
249     {
250         return new Speed(this.si / v.si, SpeedUnit.SI);
251     }
252 
253     /**
254      * Calculate the division of Acceleration and Speed, which results in a Frequency scalar.
255      * @param v scalar
256      * @return scalar as a division of Acceleration and Speed
257      */
258     public final Frequency divide(final Speed v)
259     {
260         return new Frequency(this.si / v.si, FrequencyUnit.SI);
261     }
262 
263     /**
264      * Calculate the multiplication of Acceleration and Momentum, which results in a Power scalar.
265      * @param v scalar
266      * @return scalar as a multiplication of Acceleration and Momentum
267      */
268     public final Power times(final Momentum v)
269     {
270         return new Power(this.si * v.si, PowerUnit.SI);
271     }
272 
273     @Override
274     public SIScalar reciprocal()
275     {
276         return SIScalar.divide(Dimensionless.ONE, this);
277     }
278 
279     /**
280      * Multiply two scalars that result in a scalar of type Acceleration.
281      * @param scalar1 the first scalar
282      * @param scalar2 the second scalar
283      * @return the multiplication of both scalars as an instance of Acceleration
284      */
285     public static Acceleration multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
286     {
287         Throw.whenNull(scalar1, "scalar1 cannot be null");
288         Throw.whenNull(scalar2, "scalar2 cannot be null");
289         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
290                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(AccelerationUnit.BASE.getSiDimensions()),
291                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Acceleration",
292                 scalar1.toDisplayString(), scalar2.toDisplayString());
293         return new Acceleration(scalar1.si * scalar2.si, AccelerationUnit.SI);
294     }
295 
296     /**
297      * Divide two scalars that result in a scalar of type Acceleration.
298      * @param scalar1 the first scalar
299      * @param scalar2 the second scalar
300      * @return the division of scalar1 by scalar2 as an instance of Acceleration
301      */
302     public static Acceleration divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
303     {
304         Throw.whenNull(scalar1, "scalar1 cannot be null");
305         Throw.whenNull(scalar2, "scalar2 cannot be null");
306         Throw.when(
307                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
308                         .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions())
309                         .equals(AccelerationUnit.BASE.getSiDimensions()),
310                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Acceleration",
311                 scalar1.toDisplayString(), scalar2.toDisplayString());
312         return new Acceleration(scalar1.si / scalar2.si, AccelerationUnit.SI);
313     }
314 
315 }