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