1 package org.djunits.value.vfloat.scalar;
2
3 import java.util.regex.Matcher;
4
5 import org.djunits.unit.AreaUnit;
6 import org.djunits.unit.DimensionlessUnit;
7 import org.djunits.unit.DurationUnit;
8 import org.djunits.unit.EnergyUnit;
9 import org.djunits.unit.LengthUnit;
10 import org.djunits.unit.LinearDensityUnit;
11 import org.djunits.unit.MoneyUnit;
12 import org.djunits.unit.PositionUnit;
13 import org.djunits.unit.SpeedUnit;
14 import org.djunits.unit.Unit;
15 import org.djunits.unit.VolumeUnit;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class FloatLength extends AbstractFloatScalarRel<LengthUnit, FloatLength>
43 {
44
45 private static final long serialVersionUID = 20150901L;
46
47
48 public static final FloatLength ZERO = new FloatLength(0.0f, LengthUnit.SI);
49
50
51 @SuppressWarnings("checkstyle:constantname")
52 public static final FloatLength NaN = new FloatLength(Float.NaN, LengthUnit.SI);
53
54
55 public static final FloatLength POSITIVE_INFINITY = new FloatLength(Float.POSITIVE_INFINITY, LengthUnit.SI);
56
57
58 public static final FloatLength NEGATIVE_INFINITY = new FloatLength(Float.NEGATIVE_INFINITY, LengthUnit.SI);
59
60
61 public static final FloatLength POS_MAXVALUE = new FloatLength(Float.MAX_VALUE, LengthUnit.SI);
62
63
64 public static final FloatLength NEG_MAXVALUE = new FloatLength(-Float.MAX_VALUE, LengthUnit.SI);
65
66
67
68
69
70
71 public FloatLength(final float value, final LengthUnit unit)
72 {
73 super(value, unit);
74 }
75
76
77
78
79
80 public FloatLength(final FloatLength value)
81 {
82 super(value);
83 }
84
85
86
87
88
89
90 public FloatLength(final double value, final LengthUnit unit)
91 {
92 super((float) value, unit);
93 }
94
95
96 @Override
97 public final FloatLength instantiateRel(final float value, final LengthUnit unit)
98 {
99 return new FloatLength(value, unit);
100 }
101
102
103
104
105
106
107 public static final FloatLength createSI(final float value)
108 {
109 return new FloatLength(value, LengthUnit.SI);
110 }
111
112
113
114
115
116
117
118 public final FloatPosition instantiateAbs(final float value, final PositionUnit unit)
119 {
120 return new FloatPosition(value, unit);
121 }
122
123
124
125
126
127
128
129
130 public static FloatLength interpolate(final FloatLength zero, final FloatLength one, final float ratio)
131 {
132 return new FloatLength(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
133 }
134
135
136
137
138
139
140 public final FloatPosition plus(final FloatPosition v)
141 {
142 PositionUnit targetUnit = v.getUnit();
143 return instantiateAbs(v.getInUnit() + getInUnit(targetUnit.getRelativeUnit()), targetUnit);
144 }
145
146
147
148
149
150
151
152 public static FloatLength max(final FloatLength r1, final FloatLength r2)
153 {
154 return (r1.gt(r2)) ? r1 : r2;
155 }
156
157
158
159
160
161
162
163
164 public static FloatLength max(final FloatLength r1, final FloatLength r2, final FloatLength... rn)
165 {
166 FloatLength maxr = (r1.gt(r2)) ? r1 : r2;
167 for (FloatLength r : rn)
168 {
169 if (r.gt(maxr))
170 {
171 maxr = r;
172 }
173 }
174 return maxr;
175 }
176
177
178
179
180
181
182
183 public static FloatLength min(final FloatLength r1, final FloatLength r2)
184 {
185 return (r1.lt(r2)) ? r1 : r2;
186 }
187
188
189
190
191
192
193
194
195 public static FloatLength min(final FloatLength r1, final FloatLength r2, final FloatLength... rn)
196 {
197 FloatLength minr = (r1.lt(r2)) ? r1 : r2;
198 for (FloatLength r : rn)
199 {
200 if (r.lt(minr))
201 {
202 minr = r;
203 }
204 }
205 return minr;
206 }
207
208
209
210
211
212
213
214
215
216 public static FloatLength valueOf(final String text) throws IllegalArgumentException
217 {
218 if (text == null || text.length() == 0)
219 {
220 throw new IllegalArgumentException("Error parsing FloatLength -- null or empty argument");
221 }
222 Matcher matcher = NUMBER_PATTERN.matcher(text);
223 if (matcher.find())
224 {
225 int index = matcher.end();
226 try
227 {
228 String unitString = text.substring(index).trim();
229 String valueString = text.substring(0, index).trim();
230 for (LengthUnit unit : Unit.getUnits(LengthUnit.class))
231 {
232 if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
233 {
234 float f = Float.parseFloat(valueString);
235 return new FloatLength(f, unit);
236 }
237 }
238 }
239 catch (Exception exception)
240 {
241 throw new IllegalArgumentException("Error parsing FloatLength from " + text, exception);
242 }
243 }
244 throw new IllegalArgumentException("Error parsing FloatLength from " + text);
245 }
246
247
248
249
250
251
252 public final FloatDimensionless divideBy(final FloatLength v)
253 {
254 return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
255 }
256
257
258
259
260
261
262 public final FloatArea multiplyBy(final FloatLength v)
263 {
264 return new FloatArea(this.si * v.si, AreaUnit.SI);
265 }
266
267
268
269
270
271
272 public final FloatArea divideBy(final FloatLinearDensity v)
273 {
274 return new FloatArea(this.si / v.si, AreaUnit.SI);
275 }
276
277
278
279
280
281
282 public final FloatLinearDensity divideBy(final FloatArea v)
283 {
284 return new FloatLinearDensity(this.si / v.si, LinearDensityUnit.SI);
285 }
286
287
288
289
290
291
292 public final FloatVolume multiplyBy(final FloatArea v)
293 {
294 return new FloatVolume(this.si * v.si, VolumeUnit.SI);
295 }
296
297
298
299
300
301
302 public final FloatEnergy multiplyBy(final FloatForce v)
303 {
304 return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
305 }
306
307
308
309
310
311
312 public final FloatSpeed multiplyBy(final FloatFrequency v)
313 {
314 return new FloatSpeed(this.si * v.si, SpeedUnit.SI);
315 }
316
317
318
319
320
321
322 public final FloatSpeed divideBy(final FloatDuration v)
323 {
324 return new FloatSpeed(this.si / v.si, SpeedUnit.SI);
325 }
326
327
328
329
330
331
332 public final FloatDuration divideBy(final FloatSpeed v)
333 {
334 return new FloatDuration(this.si / v.si, DurationUnit.SI);
335 }
336
337
338
339
340
341
342 public final FloatMoney multiplyBy(final FloatMoneyPerLength v)
343 {
344 return new FloatMoney(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
345 }
346
347 }