1 package org.djunits.value.vdouble.scalar;
2
3 import java.util.regex.Matcher;
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.SpeedUnit;
10 import org.djunits.unit.Unit;
11
12 /**
13 * Easy access methods for the Acceleration DoubleScalar, which is relative by definition. Instead of:
14 *
15 * <pre>
16 * DoubleScalar.Rel<AccelerationUnit> value = new DoubleScalar.Rel<AccelerationUnit>(100.0, AccelerationUnit.SI);
17 * </pre>
18 *
19 * we can now write:
20 *
21 * <pre>
22 * Acceleration value = new Acceleration(100.0, AccelerationUnit.SI);
23 * </pre>
24 *
25 * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
26 * used are compatible.
27 * <p>
28 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29 * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
30 * <p>
31 * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
32 * initial version Sep 5, 2015 <br>
33 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
35 */
36 public class Acceleration extends AbstractDoubleScalarRel<AccelerationUnit, Acceleration>
37 {
38 /** */
39 private static final long serialVersionUID = 20150905L;
40
41 /** constant with value zero. */
42 public static final Acceleration ZERO = new Acceleration(0.0, AccelerationUnit.SI);
43
44 /** constant with value NaN. */
45 @SuppressWarnings("checkstyle:constantname")
46 public static final Acceleration NaN = new Acceleration(Double.NaN, AccelerationUnit.SI);
47
48 /** constant with value POSITIVE_INFINITY. */
49 public static final Acceleration POSITIVE_INFINITY = new Acceleration(Double.POSITIVE_INFINITY, AccelerationUnit.SI);
50
51 /** constant with value NEGATIVE_INFINITY. */
52 public static final Acceleration NEGATIVE_INFINITY = new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
53
54 /** constant with value MAX_VALUE. */
55 public static final Acceleration POS_MAXVALUE = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
56
57 /** constant with value -MAX_VALUE. */
58 public static final Acceleration NEG_MAXVALUE = new Acceleration(-Double.MAX_VALUE, AccelerationUnit.SI);
59
60 /**
61 * Construct Acceleration scalar.
62 * @param value double value
63 * @param unit unit for the double value
64 */
65 public Acceleration(final double value, final AccelerationUnit unit)
66 {
67 super(value, unit);
68 }
69
70 /**
71 * Construct Acceleration scalar.
72 * @param value Scalar from which to construct this instance
73 */
74 public Acceleration(final Acceleration value)
75 {
76 super(value);
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 public final Acceleration instantiateRel(final double value, final AccelerationUnit unit)
82 {
83 return new Acceleration(value, unit);
84 }
85
86 /**
87 * Construct Acceleration scalar.
88 * @param value double value in SI units
89 * @return the new scalar with the SI value
90 */
91 public static final Acceleration createSI(final double value)
92 {
93 return new Acceleration(value, AccelerationUnit.SI);
94 }
95
96 /**
97 * Interpolate between two values.
98 * @param zero the low value
99 * @param one the high value
100 * @param ratio the ratio between 0 and 1, inclusive
101 * @return a Scalar at the ratio between
102 */
103 public static Acceleration interpolate(final Acceleration zero, final Acceleration one, final double ratio)
104 {
105 return new Acceleration(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
106 }
107
108 /**
109 * Return the maximum value of two relative scalars.
110 * @param r1 the first scalar
111 * @param r2 the second scalar
112 * @return the maximum value of two relative scalars
113 */
114 public static Acceleration max(final Acceleration r1, final Acceleration r2)
115 {
116 return (r1.gt(r2)) ? r1 : r2;
117 }
118
119 /**
120 * Return the maximum value of more than two relative scalars.
121 * @param r1 the first scalar
122 * @param r2 the second scalar
123 * @param rn the other scalars
124 * @return the maximum value of more than two relative scalars
125 */
126 public static Acceleration max(final Acceleration r1, final Acceleration r2, final Acceleration... rn)
127 {
128 Acceleration maxr = (r1.gt(r2)) ? r1 : r2;
129 for (Acceleration r : rn)
130 {
131 if (r.gt(maxr))
132 {
133 maxr = r;
134 }
135 }
136 return maxr;
137 }
138
139 /**
140 * Return the minimum value of two relative scalars.
141 * @param r1 the first scalar
142 * @param r2 the second scalar
143 * @return the minimum value of two relative scalars
144 */
145 public static Acceleration min(final Acceleration r1, final Acceleration r2)
146 {
147 return (r1.lt(r2)) ? r1 : r2;
148 }
149
150 /**
151 * Return the minimum value of more than two relative scalars.
152 * @param r1 the first scalar
153 * @param r2 the second scalar
154 * @param rn the other scalars
155 * @return the minimum value of more than two relative scalars
156 */
157 public static Acceleration min(final Acceleration r1, final Acceleration r2, final Acceleration... rn)
158 {
159 Acceleration minr = (r1.lt(r2)) ? r1 : r2;
160 for (Acceleration r : rn)
161 {
162 if (r.lt(minr))
163 {
164 minr = r;
165 }
166 }
167 return minr;
168 }
169
170 /**
171 * Returns a Acceleration representation of a textual representation of a value with a unit. The String representation that
172 * can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but
173 * not necessary, between the value and the unit.
174 * @param text String; the textual representation to parse into a Acceleration
175 * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
176 * @throws IllegalArgumentException when the text cannot be parsed
177 */
178 public static Acceleration valueOf(final String text) throws IllegalArgumentException
179 {
180 if (text == null || text.length() == 0)
181 {
182 throw new IllegalArgumentException("Error parsing Acceleration -- null or empty argument");
183 }
184 Matcher matcher = NUMBER_PATTERN.matcher(text);
185 if (matcher.find())
186 {
187 int index = matcher.end();
188 try
189 {
190 String unitString = text.substring(index).trim();
191 String valueString = text.substring(0, index).trim();
192 for (AccelerationUnit unit : Unit.getUnits(AccelerationUnit.class))
193 {
194 if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
195 {
196 double d = Double.parseDouble(valueString);
197 return new Acceleration(d, unit);
198 }
199 }
200 }
201 catch (Exception exception)
202 {
203 throw new IllegalArgumentException("Error parsing Acceleration from " + text, exception);
204 }
205 }
206 throw new IllegalArgumentException("Error parsing Acceleration from " + text);
207 }
208
209 /**
210 * Calculate the division of Acceleration and Acceleration, which results in a Dimensionless scalar.
211 * @param v Acceleration scalar
212 * @return Dimensionless scalar as a division of Acceleration and Acceleration
213 */
214 public final Dimensionless divideBy(final Acceleration v)
215 {
216 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
217 }
218
219 /**
220 * Calculate the multiplication of Acceleration and Mass, which results in a Force scalar.
221 * @param v Acceleration scalar
222 * @return Force scalar as a multiplication of Acceleration and Mass
223 */
224 public final Force multiplyBy(final Mass v)
225 {
226 return new Force(this.si * v.si, ForceUnit.SI);
227 }
228
229 /**
230 * Calculate the multiplication of Acceleration and Duration, which results in a Speed scalar.
231 * @param v Acceleration scalar
232 * @return Speed scalar as a multiplication of Acceleration and Duration
233 */
234 public final Speed multiplyBy(final Duration v)
235 {
236 return new Speed(this.si * v.si, SpeedUnit.SI);
237 }
238
239 /**
240 * Calculate the division of Acceleration and Frequency, which results in a Speed scalar.
241 * @param v Acceleration scalar
242 * @return Speed scalar as a division of Acceleration and Frequency
243 */
244 public final Speed divideBy(final Frequency v)
245 {
246 return new Speed(this.si / v.si, SpeedUnit.SI);
247 }
248
249 /**
250 * Calculate the division of Acceleration and Speed, which results in a Frequency scalar.
251 * @param v Acceleration scalar
252 * @return Frequency scalar as a division of Acceleration and Speed
253 */
254 public final Frequency divideBy(final Speed v)
255 {
256 return new Frequency(this.si / v.si, FrequencyUnit.SI);
257 }
258
259 }