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