1 package org.djunits.value.vfloat.scalar;
2
3 import java.util.Locale;
4
5 import org.djunits.unit.DensityUnit;
6 import org.djunits.unit.DimensionlessUnit;
7 import org.djunits.unit.DurationUnit;
8 import org.djunits.unit.FlowMassUnit;
9 import org.djunits.unit.ForceUnit;
10 import org.djunits.unit.MassUnit;
11 import org.djunits.unit.MomentumUnit;
12 import org.djunits.unit.VolumeUnit;
13 import org.djunits.unit.si.SIPrefixes;
14 import org.djunits.value.vfloat.scalar.base.AbstractFloatScalarRel;
15 import org.djunits.value.vfloat.scalar.base.FloatScalar;
16 import org.djutils.base.NumberParser;
17 import org.djutils.exceptions.Throw;
18
19 import jakarta.annotation.Generated;
20
21
22
23
24
25
26
27
28
29
30 @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2023-04-30T13:59:27.633664900Z")
31 public class FloatMass extends AbstractFloatScalarRel<MassUnit, FloatMass>
32 {
33
34 private static final long serialVersionUID = 20150901L;
35
36
37 public static final FloatMass ZERO = new FloatMass(0.0f, MassUnit.SI);
38
39
40 public static final FloatMass ONE = new FloatMass(1.0f, MassUnit.SI);
41
42
43 @SuppressWarnings("checkstyle:constantname")
44 public static final FloatMass NaN = new FloatMass(Float.NaN, MassUnit.SI);
45
46
47 public static final FloatMass POSITIVE_INFINITY = new FloatMass(Float.POSITIVE_INFINITY, MassUnit.SI);
48
49
50 public static final FloatMass NEGATIVE_INFINITY = new FloatMass(Float.NEGATIVE_INFINITY, MassUnit.SI);
51
52
53 public static final FloatMass POS_MAXVALUE = new FloatMass(Float.MAX_VALUE, MassUnit.SI);
54
55
56 public static final FloatMass NEG_MAXVALUE = new FloatMass(-Float.MAX_VALUE, MassUnit.SI);
57
58
59
60
61
62
63 public FloatMass(final float value, final MassUnit unit)
64 {
65 super(value, unit);
66 }
67
68
69
70
71
72 public FloatMass(final FloatMass value)
73 {
74 super(value);
75 }
76
77
78
79
80
81
82 public FloatMass(final double value, final MassUnit unit)
83 {
84 super((float) value, unit);
85 }
86
87
88 @Override
89 public final FloatMass instantiateRel(final float value, final MassUnit unit)
90 {
91 return new FloatMass(value, unit);
92 }
93
94
95
96
97
98
99 public static final FloatMass instantiateSI(final float value)
100 {
101 return new FloatMass(value, MassUnit.SI);
102 }
103
104
105
106
107
108
109
110
111 public static FloatMass interpolate(final FloatMass zero, final FloatMass one, final float ratio)
112 {
113 return new FloatMass(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
114 zero.getDisplayUnit());
115 }
116
117
118
119
120
121
122
123 public static FloatMass max(final FloatMass r1, final FloatMass r2)
124 {
125 return r1.gt(r2) ? r1 : r2;
126 }
127
128
129
130
131
132
133
134
135 public static FloatMass max(final FloatMass r1, final FloatMass r2, final FloatMass... rn)
136 {
137 FloatMass maxr = r1.gt(r2) ? r1 : r2;
138 for (FloatMass r : rn)
139 {
140 if (r.gt(maxr))
141 {
142 maxr = r;
143 }
144 }
145 return maxr;
146 }
147
148
149
150
151
152
153
154 public static FloatMass min(final FloatMass r1, final FloatMass r2)
155 {
156 return r1.lt(r2) ? r1 : r2;
157 }
158
159
160
161
162
163
164
165
166 public static FloatMass min(final FloatMass r1, final FloatMass r2, final FloatMass... rn)
167 {
168 FloatMass minr = r1.lt(r2) ? r1 : r2;
169 for (FloatMass r : rn)
170 {
171 if (r.lt(minr))
172 {
173 minr = r;
174 }
175 }
176 return minr;
177 }
178
179
180
181
182
183
184
185
186
187
188 public static FloatMass valueOf(final String text)
189 {
190 Throw.whenNull(text, "Error parsing FloatMass: text to parse is null");
191 Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatMass: empty text to parse");
192 try
193 {
194 NumberParser numberParser = new NumberParser().lenient().trailing();
195 float f = numberParser.parseFloat(text);
196 String unitString = text.substring(numberParser.getTrailingPosition()).trim();
197 MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
198 if (unit == null)
199 throw new IllegalArgumentException("Unit " + unitString + " not found");
200 return new FloatMass(f, unit);
201 }
202 catch (Exception exception)
203 {
204 throw new IllegalArgumentException(
205 "Error parsing FloatMass from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
206 exception);
207 }
208 }
209
210
211
212
213
214
215
216
217
218 public static FloatMass of(final float value, final String unitString)
219 {
220 Throw.whenNull(unitString, "Error parsing FloatMass: unitString is null");
221 Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatMass: empty unitString");
222 MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
223 if (unit != null)
224 {
225 return new FloatMass(value, unit);
226 }
227 throw new IllegalArgumentException("Error parsing FloatMass with unit " + unitString);
228 }
229
230
231 @Override
232 public String toStringSIPrefixed(final int smallestPower, final int biggestPower)
233 {
234 if (!Float.isFinite(this.si))
235 {
236 return toString(getDisplayUnit().getStandardUnit());
237 }
238
239
240 String check = String.format(this.si >= 0 ? "%10.8E" : "%10.7E", this.si);
241 int exponent = Integer.parseInt(check.substring(check.indexOf("E") + 1));
242 if (exponent < -27 || exponent < smallestPower || exponent > 21 + 2 || exponent > biggestPower)
243 {
244
245 return String.format(this.si >= 0 ? "%10.4E" : "%10.3E", this.si) + " "
246 + getDisplayUnit().getStandardUnit().getId();
247 }
248 Integer roundedExponent = (int) Math.ceil((exponent - 2.0) / 3) * 3 + 3;
249
250 String key = SIPrefixes.FACTORS.get(roundedExponent).getDefaultTextualPrefix() + "g";
251 MassUnit displayUnit = getDisplayUnit().getQuantity().getUnitByAbbreviation(key);
252 return toString(displayUnit);
253 }
254
255
256
257
258
259
260 public final FloatDimensionless divide(final FloatMass v)
261 {
262 return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
263 }
264
265
266
267
268
269
270 public final FloatDuration divide(final FloatFlowMass v)
271 {
272 return new FloatDuration(this.si / v.si, DurationUnit.SI);
273 }
274
275
276
277
278
279
280 public final FloatFlowMass divide(final FloatDuration v)
281 {
282 return new FloatFlowMass(this.si / v.si, FlowMassUnit.SI);
283 }
284
285
286
287
288
289
290 public final FloatForce times(final FloatAcceleration v)
291 {
292 return new FloatForce(this.si * v.si, ForceUnit.SI);
293 }
294
295
296
297
298
299
300 public final FloatFlowMass times(final FloatFrequency v)
301 {
302 return new FloatFlowMass(this.si * v.si, FlowMassUnit.SI);
303 }
304
305
306
307
308
309
310 public final FloatVolume divide(final FloatDensity v)
311 {
312 return new FloatVolume(this.si / v.si, VolumeUnit.SI);
313 }
314
315
316
317
318
319
320 public final FloatDensity divide(final FloatVolume v)
321 {
322 return new FloatDensity(this.si / v.si, DensityUnit.SI);
323 }
324
325
326
327
328
329
330 public final FloatMomentum times(final FloatSpeed v)
331 {
332 return new FloatMomentum(this.si * v.si, MomentumUnit.SI);
333 }
334
335
336 @Override
337 public FloatSIScalar reciprocal()
338 {
339 return FloatScalar.divide(FloatDimensionless.ONE, this);
340 }
341
342 }