1
19
20 package com.liferay.portlet.unitconverter.util;
21
22 import com.liferay.portlet.unitconverter.model.Conversion;
23
24
30 public class ConverterUtil {
31
32 public static int TEMPERATURE_CELSIUS = 1;
33
34 public static int TEMPERATURE_FAHRENHEIHT = 2;
35
36 public static Conversion getConversion(int type, int fromId,
37 int toId, double fromValue) {
38 double toValue = 0;
39
40 if (type == 0) {
41 toValue = convertLength(fromId, toId, fromValue);
42 }
43 else if (type == 1) {
44 toValue = convertArea(fromId, toId, fromValue);
45 }
46 else if (type == 2) {
47 toValue = convertVolume(fromId, toId, fromValue);
48 }
49 else if (type == 3) {
50 toValue = convertMass(fromId, toId, fromValue);
51 }
52 else if (type == 4) {
53 toValue = convertTemperature(fromId, toId, fromValue);
54 }
55
56 return new Conversion(type, fromId, toId, fromValue, toValue);
57 }
58
59 public static double convertArea(int fromId, int toId, double fromValue) {
60 return (fromValue / _AREA[fromId]) * _AREA[toId];
61 }
62
63 public static double convertLength(int fromId, int toId, double fromValue) {
64 return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
65 }
66
67 public static double convertMass(int fromId, int toId, double fromValue) {
68 return (fromValue / _MASS[fromId]) * _MASS[toId];
69 }
70
71 public static double convertTemperature(int fromId, int toId,
72 double fromValue) {
73 return _fromTemperature(toId, _toTemperature(fromId, fromValue));
74 }
75
76 public static double convertVolume(int fromId, int toId, double fromValue) {
77 return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
78 }
79
80 private final static double _fromTemperature(int toId, double fromValue) {
81 if (toId == 0) {
82 return fromValue; }
84 else if (toId == 1) {
85 return fromValue - 273.15; }
87 else if (toId == 2) {
88 return (1.8 * fromValue) - 459.67; }
90 else if (toId == 3) {
91 return 1.8 * fromValue; }
93 else if (toId == 4) {
94 return .8 * (fromValue - 273.15); }
96 else {
97 return 0;
98 }
99 }
100
101 private final static double _toTemperature(int fromId, double fromValue) {
102 if (fromId == 0) { return fromValue;
104 }
105 else if (fromId == 1) { return fromValue + 273.15;
107 }
108 else if (fromId == 2) { return .5555555555 * (fromValue + 459.67);
110 }
111 else if (fromId == 3) { return .5555555555 * fromValue;
113 }
114 else if (fromId == 4) {
115 return (1.25 * fromValue) + 273.15; }
117 else {
118 return 0;
119 }
120 }
121
122 private final static double _AREA[] = new double[] {
123 1.0, 1000000.0, 10000000000.0, 1000000000000.0, 10763910, 1550003000, 1195990, 0.3861022, 100, 247.1054, };
134
135 private final static double _LENGTH[] = new double[] {
136 1.0, 1000.0, 100.0, 0.001, 3.28084, 39.37008, 1.093613, 0.000621, 2.187227, 4.374453, 13.12336 };
148
149 private final static double _MASS[] = new double[] {
150 1.0, 2.204623, 0.00110, 0.02939497, 1.763698, 88.18491, 132.2774, 176.2698, 1763.698, };
160
161 private final static double _VOLUME[] = new double[] {
162 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, };
176
177 }