-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUnitUtils.java
569 lines (475 loc) · 13.3 KB
/
UnitUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package cadcore;
import boardcad.i18n.LanguageResource;
public class UnitUtils {
static public final int CENTIMETERS = 0;
static public final int INCHES = 1;
static public final int MILLIMETERS = 2;
static public final int INCHES_DECIMAL = 3;
static public final int METERS = 4;
static public final double INCH = 2.54;
static public final double INCHES_PR_FOOT = 12;
static public final double FOOT = (INCH*INCHES_PR_FOOT);
public static final double CENTIMETER_PR_METER = 100;
public static final double MILLIMETER_PR_CENTIMETER = 10;
public static final double METER = CENTIMETER_PR_METER;
public static final double CENTIMETER = 1;
public static final double MILLIMETER = 0.1;
public static final double SQUARECENTIMETER_PR_METER = 10000;
public static final double SQUAREMETER = SQUARECENTIMETER_PR_METER;
public static final double CUBICCENTIMETER_PR_LITRE = 1000;
public static final double CUBICCENTIMETER_PR_US_PINT = 473;
static public final double FEET_PR_METER = CENTIMETER_PR_METER/(INCH*INCHES_PR_FOOT);
public static final double POUNDS_PR_KG = 2.20462262;
private static int mSelectedUnit = INCHES;
private static int mFractionAccuracy = 16;
private static int mNrOfDecimals = 2;
public static void setCurrentUnit(int unit)
{
mSelectedUnit = unit;
}
public static int getCurrentUnit()
{
return mSelectedUnit;
}
public static void setFractionAccuracy(int accuracy)
{
mFractionAccuracy = accuracy;
}
public static void setNrOfDecimals(int nrOfDecimals)
{
mNrOfDecimals = nrOfDecimals;
}
public static double convertInputStringToInternalLengthUnit(String string)
{
double value = 0;
try{
string = string.replace(",", ".");
if(string.contains("\'") || string.contains("\"") )
{
if(string.contains("\'"))
{
String[] sa = string.split("\'");
if(sa.length > 1)
{
string = sa[1];
}
else
{
string = "";
}
value += convertCombinedStringToValue(sa[0])*12*INCH;
}
if(string.contains("\""))
{
string = string.substring(0, string.indexOf("\""));
}
value += convertCombinedStringToValue(string)*INCH;
}
else if(string.contains(LanguageResource.getString("UNITMETER_STR") ) || string.contains("m"))
{
if(string.endsWith(LanguageResource.getString("UNITMILIMETER_STR") ) || string.endsWith("mm"))
{
string = string.substring(0, string.indexOf("m"));
value += convertCombinedStringToValue(string)*0.1;
}
else if(string.endsWith(LanguageResource.getString("UNITCENTIMETER_STR") ) || string.endsWith("cm"))
{
string = string.substring(0, string.indexOf("c"));
value += convertCombinedStringToValue(string)*1;
}
else if(string.endsWith(LanguageResource.getString("UNITMETER_STR") ) || string.endsWith("m"))
{
string = string.substring(0, string.indexOf("m"));
value += convertCombinedStringToValue(string)*100;
}
else
{
value += convertCombinedStringToValue(string)*1; //Default to centimeter
}
}
else
{
double mul = 1;
switch(mSelectedUnit)
{
case MILLIMETERS:
mul = 0.1;
break;
case INCHES:
mul = INCH;
break;
default:
case CENTIMETERS:
mul = 1.0;
break;
case METERS:
mul = 100.0;
break;
}
value += convertCombinedStringToValue(string)*mul;
}
}
catch(Exception e)
{
System.out.println("Exception in convertInputStringToInternalUnit(): " + e.toString());
}
return value;
}
public static double convertInputStringToInternalWeightUnit(String string)
{
double value = 0;
double mul = 0;
try{
string = string.replace(",", ".");
if(string.endsWith("kg") || string.endsWith("kilo") || string.endsWith("kilogram"))
{
mul = 1; //Default to kg
}
else if(string.endsWith("g") || string.endsWith("gram"))
{
mul = 0.001; //Gram to kg
}
else if(string.endsWith("lb") || string.endsWith("lbs") || string.endsWith("pounds"))
{
mul = 0.45359237; //Pound to kg
}
else if(string.endsWith("oz") || string.endsWith("ounces"))
{
mul = 0.0283495231; //Ounce to kg
}
else
{
//Unknown, use current logic
switch(mSelectedUnit)
{
case INCHES:
mul = 0.45359237;
break;
default:
mul = 1;
break;
}
}
value += convertCombinedStringToValue(string)*mul;
}
catch(Exception e)
{
System.out.println("Exception in convertInputStringToInternalWeightUnit(): " + e.toString());
}
return value;
}
public static double convertInputStringToInternalDensityUnit(String string)
{
double weight = 0.0;
double volume = 0.0;
try{
string = string.replace(",", ".");
if(string.contains("/"))
{
String[] sa = string.split("/");
weight = convertInputStringToInternalWeightUnit(sa[0]);
volume = convertInputStringToInternalVolumeUnit("1"+sa[1]);
}
else
{
weight = convertInputStringToInternalWeightUnit(string);
//Guess the most natural unit to use
if(string.endsWith("kg") || string.endsWith("kilo") || string.endsWith("kilogram"))
{
volume = 1000.0; //Default to one cubic meter
}
else if(string.endsWith("g") || string.endsWith("gram"))
{
volume = 1.0; //Gram pr. litre
}
else if(string.endsWith("lb") || string.endsWith("lbs") || string.endsWith("pounds"))
{
volume = 28.3168466; //Pound pr. cubic foot
}
else if(string.endsWith("oz") || string.endsWith("ounces"))
{
volume = 0.016387064; //Ounce pr. cubic inch
}
else
{
//Unknown, use current logic
switch(mSelectedUnit)
{
case INCHES:
volume = 28.3168466;
break;
default:
volume = 1;
break;
}
}
}
}
catch(Exception e)
{
System.out.println("Exception in convertInputStringToInternalWeightUnit(): " + e.toString());
}
double value = weight/volume;
return value;
}
public static double convertInputStringToInternalVolumeUnit(String string)
{
//Note: internal volume unit is liter
double value = 0;
double mul = 1;
try{
string = string.replace(",", ".");
if(string.endsWith("l") || string.endsWith("liter") || string.endsWith("litre") || string.endsWith("dm^3") || string.endsWith("dm³") || string.endsWith("dm3"))
{
mul = 1; //Default to cubic dm (liter)
}
else if(string.endsWith("m") || string.endsWith("m3") || string.endsWith("m^3") || string.endsWith("m³") || string.endsWith("cubicmeter") || string.endsWith("cubic") || string.endsWith("cubic meter") || string.endsWith("cubicmetre") || string.endsWith("cubic metre"))
{
mul = 1000.0; // to cubic meter
}
else if(string.endsWith("cubic feet") || string.endsWith("cubic foot") || string.endsWith("cubic ft") || string.endsWith("cubic ft") || string.endsWith("cu feet") || string.endsWith("cu foot") || string.endsWith("cu ft") || string.endsWith("ft³") || string.endsWith("ft") || string.endsWith("ft3") || string.endsWith("feet³") || string.endsWith("foot³") || string.endsWith("feet^3") || string.endsWith("foot^3") || string.endsWith("ft^3"))
{
mul = 28.3168466; //Pound pr. cubic foot
}
if(string.charAt(string.length()-1) == '3')
string = string.substring(0, string.length()-1);
value = convertCombinedStringToValue(string)*mul;
}
catch(Exception e)
{
System.out.println("Exception in convertInputStringToInternalVolumeUnit(): " + e.toString());
}
return value;
}
public static double convertCombinedStringToValue(String string)
{
double value = 0;
//to remove the first blanks
string=string.replaceAll("^\\s+", "");
//Remove all nonnumerical characters at end of string
int n = string.length();
while(n > 0 && !Character.isDigit(string.charAt(n-1)) )
{
n--;
}
string = string.substring(0, n);
if(string.contains(" ") && string.contains("/"))
{
String[] sa = string.split("\\s+");
string = sa[0];
value += convertFractionStringToValue(sa[1].trim()); //Fraction part
}
if(string.contains("/"))
{
value += convertFractionStringToValue(string.trim()); //Fraction part
}
else if(string.trim().length() > 0)
{
value += Double.parseDouble(string.trim()); //INCH part
}
return value;
}
public static double convertFractionStringToValue(String string)
{
String[] sa = string.split("/");
try{
return Double.parseDouble(sa[0].trim())/Double.parseDouble(sa[1].trim());
}
catch(Exception e)
{
System.out.println("Exception in UnitUtils.convertFractionStringToValue(): " + e.toString());
return 0;
}
}
public static String convertLengthToCurrentUnit(double value, boolean useLargeUnits)
{
return convertLengthToUnit(value, useLargeUnits, mSelectedUnit);
}
public static String convertLengthToUnit(double value, boolean useLargeUnits, int unit)
{
switch(unit)
{
case INCHES:
{
String format = "";
if(value < 0)
{
format = format.concat("-");
}
value = Math.abs(value);
int feet = 0, inches = 0, fraction = 0, divider = mFractionAccuracy;
inches = (int)(value/INCH);
if(useLargeUnits && inches > 3*INCHES_PR_FOOT)
{
feet = (int)(inches/INCHES_PR_FOOT);
inches %= INCHES_PR_FOOT;
format = format.concat("%1$d'");
}
if(inches >= 1)
{
if(format.length() > 0)
format = format.concat(" ");
format = format.concat("%2$d");
}
fraction = (int)(((value/INCH) - (double)(inches + (feet*12)))*divider);
if(fraction > 0)
{
while(fraction%2 == 0)
{
fraction /=2;
divider /=2;
}
if(format.length() > 0)
format = format.concat(" ");
format = format.concat("%3$d/%4$d");
}
if(format.length() == 0 || format.equalsIgnoreCase("-") || format.endsWith("'"))
{
format = format.concat("0");
}
format = format.concat("\"");
return String.format(format, feet, inches, fraction, divider);
}
default:
case CENTIMETERS:
{
if(useLargeUnits && value > CENTIMETER_PR_METER)
{
return String.format("%1$.3f m", value/CENTIMETER_PR_METER);
}
// Default to cm
return String.format("%1$.2f cm", value);
}
case METERS:
{
return String.format("%1$.3f m", value/CENTIMETER_PR_METER);
}
case MILLIMETERS:
{
// mm
return String.format("%1$.1f mm", value*MILLIMETER_PR_CENTIMETER);
}
case INCHES_DECIMAL:
{
String format = "";
if(value < 0)
{
format = format.concat("-");
}
value = Math.abs(value);
int feet = 0;
float inches = 0;
inches = (float)(value/INCH);
if(useLargeUnits && inches > (3*INCHES_PR_FOOT))
{
feet = (int)(inches/INCHES_PR_FOOT);
inches -= feet*INCHES_PR_FOOT;
format = format.concat("%1$d'");
}
if(inches >= 0.01)
{
format = format.concat("%2$." + mNrOfDecimals + "f");
}
if(format.length() == 0 || format.equalsIgnoreCase("-"))
{
format = format.concat("0");
}
format = format.concat("\"");
return String.format(format, feet, inches);
}
}
}
public static String convertAreaToCurrentUnit(double value)
{
switch(mSelectedUnit)
{
case INCHES:
case INCHES_DECIMAL:
{
return String.format("%.3f %s", value*0.00107639104, LanguageResource.getString("UNITSQUAREFEET_STR") );
}
default:
case CENTIMETERS:
case MILLIMETERS:
case METERS:
{
return String.format("%.3f %s", value/(CENTIMETER_PR_METER*CENTIMETER_PR_METER), LanguageResource.getString("UNITSQUAREMETERS_STR") );
}
}
}
public static String convertVolumeToCurrentUnit(double value)
{
switch(mSelectedUnit)
{
default:
case INCHES:
case INCHES_DECIMAL:
case CENTIMETERS:
case MILLIMETERS:
case METERS:
{
return String.format("%.3f %s", value/(CUBICCENTIMETER_PR_LITRE), LanguageResource.getString("UNITLITERS_STR"));
}
}
}
public static String convertWeightToCurrentUnit(double value, boolean useSmallUnits)
{
switch(mSelectedUnit)
{
case INCHES:
case INCHES_DECIMAL:
{
if(useSmallUnits && value < 1.0)
return String.format("%.3f %s", value/0.0283495231, LanguageResource.getString("UNITOUNCE_STR") );
else
return String.format("%.3f %s", value/0.45359237, LanguageResource.getString("UNITPOUNDS_STR") );
}
default:
case CENTIMETERS:
case MILLIMETERS:
case METERS:
{
if(useSmallUnits && value < 1.0)
return String.format("%.0f %s", value*1000, LanguageResource.getString("UNITGRAMS_STR") );
else
return String.format("%.3f %s", value, LanguageResource.getString("UNITKILOGRAMS_STR") );
}
}
}
public static String convertDensityToCurrentUnit(double value)
{
switch(mSelectedUnit)
{
case INCHES:
case INCHES_DECIMAL:
{
return String.format("%.3f %s", value*62.4279606, LanguageResource.getString("UNITPOUNDSPRFOOT_STR") );
}
default:
case CENTIMETERS:
case MILLIMETERS:
case METERS:
{
return String.format("%.3f %s", value, LanguageResource.getString("UNITKILOGRAMSPRLITER_STR") );
}
}
}
public static String convertMomentOfInertiaToCurrentUnit(double value)
{
switch(mSelectedUnit)
{
case INCHES:
case INCHES_DECIMAL:
{
return String.format("%.3f %s", value*UnitUtils.POUNDS_PR_KG*UnitUtils.FEET_PR_METER*UnitUtils.FEET_PR_METER, LanguageResource.getString("UNITPOUNDSPRFOOTSQUARED_STR") );
}
default:
case CENTIMETERS:
case MILLIMETERS:
case METERS:
{
return String.format("%.3f %s", value, LanguageResource.getString("UNITKILOGRAMSPRMETERSQUARED_STR") );
}
}
}
}