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