View Javadoc
1   package org.djunits.demo.website;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.Throw;
6   import org.djunits.unit.AccelerationUnit;
7   import org.djunits.unit.DimensionlessUnit;
8   import org.djunits.unit.FrequencyUnit;
9   import org.djunits.value.util.ValueUtil;
10  import org.djunits.value.vdouble.scalar.Acceleration;
11  import org.djunits.value.vdouble.scalar.Dimensionless;
12  import org.djunits.value.vdouble.scalar.Duration;
13  import org.djunits.value.vdouble.scalar.Frequency;
14  import org.djunits.value.vdouble.scalar.SIScalar;
15  import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarRel;
16  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
17  
18  /**
19   * <p>
20   * Copyright (c) 2013-2022 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  public class Jerk extends AbstractDoubleScalarRel<JerkUnit, Jerk>
27  {
28      /** */
29      private static final long serialVersionUID = 1L;
30  
31      /**
32       * Construct Jerk scalar.
33       * @param value double; the double value
34       * @param unit JerkUnit; unit for the double value
35       */
36      public Jerk(final double value, final JerkUnit unit)
37      {
38          super(value, unit);
39      }
40  
41      /**
42       * Construct Jerk scalar.
43       * @param value Jerk; Scalar from which to construct this instance
44       */
45      public Jerkbsite/Jerk.html#Jerk">Jerk(final Jerk value)
46      {
47          super(value);
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public final Jerk instantiateRel(final double value, final JerkUnit unit)
53      {
54          return new Jerk(value, unit);
55      }
56  
57      /**
58       * Construct Jerk scalar.
59       * @param value double; the double value in SI units
60       * @return Jerk; the new scalar with the SI value
61       */
62      public static final Jerk instantiateSI(final double value)
63      {
64          return new Jerk(value, JerkUnit.SI);
65      }
66  
67      /**
68       * Interpolate between two values.
69       * @param zero Jerk; the low value
70       * @param one Jerk; the high value
71       * @param ratio double; the ratio between 0 and 1, inclusive
72       * @return Jerk; a Scalar at the ratio between
73       */
74      public static JerkJerk.html#Jerk">Jerktml#Jerk">Jerk interpolate(final JerkJerk.html#Jerk">Jerk zero, final Jerk one, final double ratio)
75      {
76          return new Jerk(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
77      }
78  
79      /**
80       * Return the maximum value of two relative scalars.
81       * @param r1 Jerk; the first scalar
82       * @param r2 Jerk; the second scalar
83       * @return Jerk; the maximum value of two relative scalars
84       */
85      public static Jerke/Jerk.html#Jerk">Jerke/Jerk.html#Jerk">Jerk max(final Jerke/Jerk.html#Jerk">Jerk r1, final Jerk r2)
86      {
87          return (r1.gt(r2)) ? r1 : r2;
88      }
89  
90      /**
91       * Return the maximum value of more than two relative scalars.
92       * @param r1 Jerk; the first scalar
93       * @param r2 Jerk; the second scalar
94       * @param rn Jerk...; the other scalars
95       * @return Jerk; the maximum value of more than two relative scalars
96       */
97      public static Jerke/Jerk.html#Jerk">Jerke/Jerk.html#Jerk">Jerk max(final Jerke/Jerk.html#Jerk">Jerk r1, final Jerk r2, final Jerk... rn)
98      {
99          Jerk maxr = (r1.gt(r2)) ? r1 : r2;
100         for (Jerk r : rn)
101         {
102             if (r.gt(maxr))
103             {
104                 maxr = r;
105             }
106         }
107         return maxr;
108     }
109 
110     /**
111      * Return the minimum value of two relative scalars.
112      * @param r1 Jerk; the first scalar
113      * @param r2 Jerk; the second scalar
114      * @return Jerk; the minimum value of two relative scalars
115      */
116     public static Jerke/Jerk.html#Jerk">Jerke/Jerk.html#Jerk">Jerk min(final Jerke/Jerk.html#Jerk">Jerk r1, final Jerk r2)
117     {
118         return r1.lt(r2) ? r1 : r2;
119     }
120 
121     /**
122      * Return the minimum value of more than two relative scalars.
123      * @param r1 Jerk; the first scalar
124      * @param r2 Jerk; the second scalar
125      * @param rn Jerk...; the other scalars
126      * @return Jerk; the minimum value of more than two relative scalars
127      */
128     public static Jerke/Jerk.html#Jerk">Jerke/Jerk.html#Jerk">Jerk min(final Jerke/Jerk.html#Jerk">Jerk r1, final Jerk r2, final Jerk... rn)
129     {
130         Jerk minr = r1.lt(r2) ? r1 : r2;
131         for (Jerk r : rn)
132         {
133             if (r.lt(minr))
134             {
135                 minr = r;
136             }
137         }
138         return minr;
139     }
140 
141     /**
142      * Returns a Jerk representation of a textual representation of a value with a unit. The String representation that can be
143      * parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
144      * required, between the value and the unit.
145      * @param text String; the textual representation to parse into a Jerk
146      * @return Jerk; the Scalar representation of the value in its unit
147      * @throws IllegalArgumentException when the text cannot be parsed
148      * @throws NullPointerException when the text argument is null
149      */
150     public static Jerk valueOf(final String text)
151     {
152         Throw.whenNull(text, "Error parsing Jerk: text to parse is null");
153         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Jerk: empty text to parse");
154         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
155         if (matcher.find())
156         {
157             int index = matcher.end();
158             String unitString = text.substring(index).trim();
159             String valueString = text.substring(0, index).trim();
160             JerkUnit unit = JerkUnit.BASE.getUnitByAbbreviation(unitString);
161             if (unit != null)
162             {
163                 double d = Double.parseDouble(valueString);
164                 return new Jerk(d, unit);
165             }
166         }
167         throw new IllegalArgumentException("Error parsing Jerk from " + text);
168     }
169 
170     /**
171      * Returns a Jerk based on a value and the textual representation of the unit.
172      * @param value double; the value to use
173      * @param unitString String; the textual representation of the unit
174      * @return Jerk; the Scalar representation of the value in its unit
175      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
176      * @throws NullPointerException when the unitString argument is null
177      */
178     public static Jerk of(final double value, final String unitString)
179     {
180         Throw.whenNull(unitString, "Error parsing Jerk: unitString is null");
181         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Jerk: empty unitString");
182         JerkUnit unit = JerkUnit.BASE.getUnitByAbbreviation(unitString);
183         if (unit != null)
184         {
185             return new Jerk(value, unit);
186         }
187         throw new IllegalArgumentException("Error parsing Jerk with unit " + unitString);
188     }
189 
190     /**
191      * Calculate the division of Jerk and Jerk, which results in a Dimensionless scalar.
192      * @param v Jerk; Jerk scalar
193      * @return Dimensionless scalar as a division of Jerk and Jerk
194      */
195     public final Dimensionless divide(final Jerk v)
196     {
197         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
198     }
199 
200     /**
201      * Calculate the multiplication of Jerk and Duration, which results in an Acceleration scalar.
202      * @param v Duration; Duration scalar
203      * @return Acceleration scalar as a multiplication of Jerk and Duration
204      */
205     public final Acceleration times(final Duration v)
206     {
207         return new Acceleration(this.si * v.si, AccelerationUnit.SI);
208     }
209 
210     /**
211      * Calculate the division of Jerk and Acceleration, which results in a Frequency scalar.
212      * @param v Acceleration; Acceleration scalar
213      * @return Frequency scalar as a division of Jerk and Accelration
214      */
215     public final Frequency times(final Acceleration v)
216     {
217         return new Frequency(this.si * v.si, FrequencyUnit.SI);
218     }
219 
220     /** {@inheritDoc} */
221     @Override
222     public SIScalar reciprocal()
223     {
224         return DoubleScalar.divide(Dimensionless.ONE, this);
225     }
226 
227 }