1
22
23 package com.liferay.portlet.unitconverter.util;
24
25 import com.liferay.portlet.unitconverter.model.Conversion;
26
27
32 public class ConverterUtil {
33
34 public static int TEMPERATURE_CELSIUS = 1;
35
36 public static int TEMPERATURE_FAHRENHEIHT = 2;
37
38 public static Conversion getConversion(int type, int fromId,
39 int toId, double fromValue) {
40 double toValue = 0;
41
42 if (type == 0) {
43 toValue = convertLength(fromId, toId, fromValue);
44 }
45 else if (type == 1) {
46 toValue = convertArea(fromId, toId, fromValue);
47 }
48 else if (type == 2) {
49 toValue = convertVolume(fromId, toId, fromValue);
50 }
51 else if (type == 3) {
52 toValue = convertMass(fromId, toId, fromValue);
53 }
54 else if (type == 4) {
55 toValue = convertTemperature(fromId, toId, fromValue);
56 }
57
58 return new Conversion(type, fromId, toId, fromValue, toValue);
59 }
60
61 public static double convertArea(int fromId, int toId, double fromValue) {
62 return (fromValue / _AREA[fromId]) * _AREA[toId];
63 }
64
65 public static double convertLength(int fromId, int toId, double fromValue) {
66 return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
67 }
68
69 public static double convertMass(int fromId, int toId, double fromValue) {
70 return (fromValue / _MASS[fromId]) * _MASS[toId];
71 }
72
73 public static double convertTemperature(int fromId, int toId,
74 double fromValue) {
75 return _fromTemperature(toId, _toTemperature(fromId, fromValue));
76 }
77
78 public static double convertVolume(int fromId, int toId, double fromValue) {
79 return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
80 }
81
82 private final static double _fromTemperature(int toId, double fromValue) {
83 if (toId == 0) {
84 return fromValue; }
86 else if (toId == 1) {
87 return fromValue - 273.15; }
89 else if (toId == 2) {
90 return (1.8 * fromValue) - 459.67; }
92 else if (toId == 3) {
93 return 1.8 * fromValue; }
95 else if (toId == 4) {
96 return .8 * (fromValue - 273.15); }
98 else {
99 return 0;
100 }
101 }
102
103 private final static double _toTemperature(int fromId, double fromValue) {
104 if (fromId == 0) { return fromValue;
106 }
107 else if (fromId == 1) { return fromValue + 273.15;
109 }
110 else if (fromId == 2) { return .5555555555 * (fromValue + 459.67);
112 }
113 else if (fromId == 3) { return .5555555555 * fromValue;
115 }
116 else if (fromId == 4) {
117 return (1.25 * fromValue) + 273.15; }
119 else {
120 return 0;
121 }
122 }
123
124 private final static double _AREA[] = new double[] {
125 1.0, 1000000.0, 10000000000.0, 1000000000000.0, 10763910, 1550003000, 1195990, 0.3861022, 100, 247.1054, };
136
137 private final static double _LENGTH[] = new double[] {
138 1.0, 1000.0, 100.0, 0.001, 3.28084, 39.37008, 1.093613, 0.000621, 2.187227, 4.374453, 13.12336 };
150
151 private final static double _MASS[] = new double[] {
152 1.0, 2.204623, 0.00110, 0.02939497, 1.763698, 88.18491, 132.2774, 176.2698, 1763.698, };
162
163 private final static double _VOLUME[] = new double[] {
164 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, };
178
179 }