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