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