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