001
014
015 package com.liferay.portlet.unitconverter.util;
016
017 import com.liferay.portlet.unitconverter.model.Conversion;
018
019
022 public class ConverterUtil {
023
024 public static int TEMPERATURE_CELSIUS = 1;
025
026 public static int TEMPERATURE_FAHRENHEIHT = 2;
027
028 public static Conversion getConversion(int type, int fromId,
029 int toId, double fromValue) {
030 double toValue = 0;
031
032 if (type == 0) {
033 toValue = convertLength(fromId, toId, fromValue);
034 }
035 else if (type == 1) {
036 toValue = convertArea(fromId, toId, fromValue);
037 }
038 else if (type == 2) {
039 toValue = convertVolume(fromId, toId, fromValue);
040 }
041 else if (type == 3) {
042 toValue = convertMass(fromId, toId, fromValue);
043 }
044 else if (type == 4) {
045 toValue = convertTemperature(fromId, toId, fromValue);
046 }
047
048 return new Conversion(type, fromId, toId, fromValue, toValue);
049 }
050
051 public static double convertArea(int fromId, int toId, double fromValue) {
052 return (fromValue / _AREA[fromId]) * _AREA[toId];
053 }
054
055 public static double convertLength(int fromId, int toId, double fromValue) {
056 return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
057 }
058
059 public static double convertMass(int fromId, int toId, double fromValue) {
060 return (fromValue / _MASS[fromId]) * _MASS[toId];
061 }
062
063 public static double convertTemperature(int fromId, int toId,
064 double fromValue) {
065 return _fromTemperature(toId, _toTemperature(fromId, fromValue));
066 }
067
068 public static double convertVolume(int fromId, int toId, double fromValue) {
069 return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
070 }
071
072 private final static double _fromTemperature(int toId, double fromValue) {
073 if (toId == 0) {
074 return fromValue;
075 }
076 else if (toId == 1) {
077 return fromValue - 273.15;
078 }
079 else if (toId == 2) {
080 return (1.8 * fromValue) - 459.67;
081 }
082 else if (toId == 3) {
083 return 1.8 * fromValue;
084 }
085 else if (toId == 4) {
086 return .8 * (fromValue - 273.15);
087 }
088 else {
089 return 0;
090 }
091 }
092
093 private final static double _toTemperature(int fromId, double fromValue) {
094 if (fromId == 0) {
095 return fromValue;
096 }
097 else if (fromId == 1) {
098 return fromValue + 273.15;
099 }
100 else if (fromId == 2) {
101 return .5555555555 * (fromValue + 459.67);
102 }
103 else if (fromId == 3) {
104 return .5555555555 * fromValue;
105 }
106 else if (fromId == 4) {
107 return (1.25 * fromValue) + 273.15;
108 }
109 else {
110 return 0;
111 }
112 }
113
114 private final static double _AREA[] = new double[] {
115 1.0,
116 1000000.0,
117 10000000000.0,
118 1000000000000.0,
119 10763910,
120 1550003000,
121 1195990,
122 0.3861022,
123 100,
124 247.1054,
125 };
126
127 private final static double _LENGTH[] = new double[] {
128 1.0,
129 1000.0,
130 100.0,
131 0.001,
132 3.28084,
133 39.37008,
134 1.093613,
135 0.000621,
136 2.187227,
137 4.374453,
138 13.12336
139 };
140
141 private final static double _MASS[] = new double[] {
142 1.0,
143 2.204623,
144 0.00110,
145 0.02939497,
146 1.763698,
147 88.18491,
148 132.2774,
149 176.2698,
150 1763.698,
151 };
152
153 private final static double _VOLUME[] = new double[] {
154 1.0,
155 1000,
156 61.02374,
157 1.816166,
158 0.004729599,
159 0.009459198,
160 0.04729599,
161 0.141888,
162 0.4729599,
163 0.851328,
164 0.04402868,
165 0.2641721,
166 3.170065,
167 };
168
169 }