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