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.FloatScalar;
15 import org.djunits.value.vfloat.scalar.base.FloatScalarRel;
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-07-23T14:06:38.224104100Z")
31 public class FloatMass extends FloatScalarRel<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 @Override
88 public final FloatMass instantiateRel(final float value, final MassUnit unit)
89 {
90 return new FloatMass(value, unit);
91 }
92
93
94
95
96
97
98 public static final FloatMass instantiateSI(final float value)
99 {
100 return new FloatMass(value, MassUnit.SI);
101 }
102
103
104
105
106
107
108
109
110 public static FloatMass interpolate(final FloatMass zero, final FloatMass one, final float ratio)
111 {
112 return new FloatMass(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
113 zero.getDisplayUnit());
114 }
115
116
117
118
119
120
121
122 public static FloatMass max(final FloatMass r1, final FloatMass r2)
123 {
124 return r1.gt(r2) ? r1 : r2;
125 }
126
127
128
129
130
131
132
133
134 public static FloatMass max(final FloatMass r1, final FloatMass r2, final FloatMass... rn)
135 {
136 FloatMass maxr = r1.gt(r2) ? r1 : r2;
137 for (FloatMass r : rn)
138 {
139 if (r.gt(maxr))
140 {
141 maxr = r;
142 }
143 }
144 return maxr;
145 }
146
147
148
149
150
151
152
153 public static FloatMass min(final FloatMass r1, final FloatMass r2)
154 {
155 return r1.lt(r2) ? r1 : r2;
156 }
157
158
159
160
161
162
163
164
165 public static FloatMass min(final FloatMass r1, final FloatMass r2, final FloatMass... rn)
166 {
167 FloatMass minr = r1.lt(r2) ? r1 : r2;
168 for (FloatMass r : rn)
169 {
170 if (r.lt(minr))
171 {
172 minr = r;
173 }
174 }
175 return minr;
176 }
177
178
179
180
181
182
183
184
185
186
187 public static FloatMass valueOf(final String text)
188 {
189 Throw.whenNull(text, "Error parsing FloatMass: text to parse is null");
190 Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatMass: empty text to parse");
191 try
192 {
193 NumberParser numberParser = new NumberParser().lenient().trailing();
194 float f = numberParser.parseFloat(text);
195 String unitString = text.substring(numberParser.getTrailingPosition()).trim();
196 MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
197 if (unit == null)
198 throw new IllegalArgumentException("Unit " + unitString + " not found");
199 return new FloatMass(f, unit);
200 }
201 catch (Exception exception)
202 {
203 throw new IllegalArgumentException(
204 "Error parsing FloatMass from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
205 exception);
206 }
207 }
208
209
210
211
212
213
214
215
216
217 public static FloatMass of(final float value, final String unitString)
218 {
219 Throw.whenNull(unitString, "Error parsing FloatMass: unitString is null");
220 Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatMass: empty unitString");
221 MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
222 if (unit != null)
223 {
224 return new FloatMass(value, unit);
225 }
226 throw new IllegalArgumentException("Error parsing FloatMass with unit " + unitString);
227 }
228
229 @Override
230 public String toStringSIPrefixed(final int smallestPower, final int biggestPower)
231 {
232 if (!Float.isFinite(this.si))
233 {
234 return toString(getDisplayUnit().getStandardUnit());
235 }
236
237
238 String check = String.format(this.si >= 0 ? "%10.8E" : "%10.7E", this.si);
239 int exponent = Integer.parseInt(check.substring(check.indexOf("E") + 1));
240 if (exponent < -27 || exponent < smallestPower || exponent > 21 + 2 || exponent > biggestPower)
241 {
242
243 return String.format(this.si >= 0 ? "%10.4E" : "%10.3E", this.si) + " "
244 + getDisplayUnit().getStandardUnit().getId();
245 }
246 Integer roundedExponent = (int) Math.ceil((exponent - 2.0) / 3) * 3 + 3;
247
248 String key = SIPrefixes.FACTORS.get(roundedExponent).getDefaultTextualPrefix() + "g";
249 MassUnit displayUnit = getDisplayUnit().getQuantity().getUnitByAbbreviation(key);
250 return toString(displayUnit);
251 }
252
253
254
255
256
257
258 public final FloatDimensionless divide(final FloatMass v)
259 {
260 return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
261 }
262
263
264
265
266
267
268 public final FloatDuration divide(final FloatFlowMass v)
269 {
270 return new FloatDuration(this.si / v.si, DurationUnit.SI);
271 }
272
273
274
275
276
277
278 public final FloatFlowMass divide(final FloatDuration v)
279 {
280 return new FloatFlowMass(this.si / v.si, FlowMassUnit.SI);
281 }
282
283
284
285
286
287
288 public final FloatForce times(final FloatAcceleration v)
289 {
290 return new FloatForce(this.si * v.si, ForceUnit.SI);
291 }
292
293
294
295
296
297
298 public final FloatFlowMass times(final FloatFrequency v)
299 {
300 return new FloatFlowMass(this.si * v.si, FlowMassUnit.SI);
301 }
302
303
304
305
306
307
308 public final FloatVolume divide(final FloatDensity v)
309 {
310 return new FloatVolume(this.si / v.si, VolumeUnit.SI);
311 }
312
313
314
315
316
317
318 public final FloatDensity divide(final FloatVolume v)
319 {
320 return new FloatDensity(this.si / v.si, DensityUnit.SI);
321 }
322
323
324
325
326
327
328 public final FloatMomentum times(final FloatSpeed v)
329 {
330 return new FloatMomentum(this.si * v.si, MomentumUnit.SI);
331 }
332
333 @Override
334 public FloatSIScalar reciprocal()
335 {
336 return FloatScalar.divide(FloatDimensionless.ONE, this);
337 }
338
339 }