1
14
15 package com.liferay.portlet.unitconverter.util;
16
17 import com.liferay.portlet.unitconverter.model.Conversion;
18
19
24 public class ConverterUtil {
25
26 public static int TEMPERATURE_CELSIUS = 1;
27
28 public static int TEMPERATURE_FAHRENHEIHT = 2;
29
30 public static Conversion getConversion(int type, int fromId,
31 int toId, double fromValue) {
32 double toValue = 0;
33
34 if (type == 0) {
35 toValue = convertLength(fromId, toId, fromValue);
36 }
37 else if (type == 1) {
38 toValue = convertArea(fromId, toId, fromValue);
39 }
40 else if (type == 2) {
41 toValue = convertVolume(fromId, toId, fromValue);
42 }
43 else if (type == 3) {
44 toValue = convertMass(fromId, toId, fromValue);
45 }
46 else if (type == 4) {
47 toValue = convertTemperature(fromId, toId, fromValue);
48 }
49
50 return new Conversion(type, fromId, toId, fromValue, toValue);
51 }
52
53 public static double convertArea(int fromId, int toId, double fromValue) {
54 return (fromValue / _AREA[fromId]) * _AREA[toId];
55 }
56
57 public static double convertLength(int fromId, int toId, double fromValue) {
58 return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
59 }
60
61 public static double convertMass(int fromId, int toId, double fromValue) {
62 return (fromValue / _MASS[fromId]) * _MASS[toId];
63 }
64
65 public static double convertTemperature(int fromId, int toId,
66 double fromValue) {
67 return _fromTemperature(toId, _toTemperature(fromId, fromValue));
68 }
69
70 public static double convertVolume(int fromId, int toId, double fromValue) {
71 return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
72 }
73
74 private final static double _fromTemperature(int toId, double fromValue) {
75 if (toId == 0) {
76 return fromValue; }
78 else if (toId == 1) {
79 return fromValue - 273.15; }
81 else if (toId == 2) {
82 return (1.8 * fromValue) - 459.67; }
84 else if (toId == 3) {
85 return 1.8 * fromValue; }
87 else if (toId == 4) {
88 return .8 * (fromValue - 273.15); }
90 else {
91 return 0;
92 }
93 }
94
95 private final static double _toTemperature(int fromId, double fromValue) {
96 if (fromId == 0) { return fromValue;
98 }
99 else if (fromId == 1) { return fromValue + 273.15;
101 }
102 else if (fromId == 2) { return .5555555555 * (fromValue + 459.67);
104 }
105 else if (fromId == 3) { return .5555555555 * fromValue;
107 }
108 else if (fromId == 4) {
109 return (1.25 * fromValue) + 273.15; }
111 else {
112 return 0;
113 }
114 }
115
116 private final static double _AREA[] = new double[] {
117 1.0, 1000000.0, 10000000000.0, 1000000000000.0, 10763910, 1550003000, 1195990, 0.3861022, 100, 247.1054, };
128
129 private final static double _LENGTH[] = new double[] {
130 1.0, 1000.0, 100.0, 0.001, 3.28084, 39.37008, 1.093613, 0.000621, 2.187227, 4.374453, 13.12336 };
142
143 private final static double _MASS[] = new double[] {
144 1.0, 2.204623, 0.00110, 0.02939497, 1.763698, 88.18491, 132.2774, 176.2698, 1763.698, };
154
155 private final static double _VOLUME[] = new double[] {
156 1.0, 1000, 61.02374, 1.816166, 0.004729599, 0.009459198, 0.04729599, 0.141888, 0.4729599, 0.851328, 0.04402868, 0.2641721, 3.170065, };
170
171 }