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