| ConverterUtil.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.portlet.unitconverter.util;
21
22 import com.liferay.portlet.unitconverter.model.Conversion;
23
24 /**
25 * <a href="ConverterUtil.java.html"><b><i>View Source</i></b></a>
26 *
27 * @author James Lefeu
28 *
29 */
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; // Kelvin
83 }
84 else if (toId == 1) {
85 return fromValue - 273.15; // Celsius
86 }
87 else if (toId == 2) {
88 return (1.8 * fromValue) - 459.67; // Fahrenheit
89 }
90 else if (toId == 3) {
91 return 1.8 * fromValue; // Rankine
92 }
93 else if (toId == 4) {
94 return .8 * (fromValue - 273.15); // R�aumure
95 }
96 else {
97 return 0;
98 }
99 }
100
101 private final static double _toTemperature(int fromId, double fromValue) {
102 if (fromId == 0) { // Kelvin
103 return fromValue;
104 }
105 else if (fromId == 1) { // Celsius
106 return fromValue + 273.15;
107 }
108 else if (fromId == 2) { // Fahrenheit
109 return .5555555555 * (fromValue + 459.67);
110 }
111 else if (fromId == 3) { // Rankine
112 return .5555555555 * fromValue;
113 }
114 else if (fromId == 4) {
115 return (1.25 * fromValue) + 273.15; // R�aumure
116 }
117 else {
118 return 0;
119 }
120 }
121
122 private final static double _AREA[] = new double[] {
123 1.0, // Square Kilometer
124 1000000.0, // Square Meter
125 10000000000.0, // Square Centimeter
126 1000000000000.0, // Square Millimeter
127 10763910, // Square Foot
128 1550003000, // Square Inch
129 1195990, // Square Yard
130 0.3861022, // Square Mile
131 100, // Hectare
132 247.1054, // Acre
133 };
134
135 private final static double _LENGTH[] = new double[] {
136 1.0, // Meter
137 1000.0, // Millimeter
138 100.0, // Centimeter
139 0.001, // Kilometer
140 3.28084, // Foot
141 39.37008, // Inch
142 1.093613, // Yard
143 0.000621, // Mile
144 2.187227, // Cubit
145 4.374453, // Talent
146 13.12336 // Handbreath
147 };
148
149 private final static double _MASS[] = new double[] {
150 1.0, // Kilogram
151 2.204623, // Pound
152 0.00110, // Ton
153 0.02939497, // Talent
154 1.763698, // Mina
155 88.18491, // Shekel
156 132.2774, // Pim
157 176.2698, // Beka
158 1763.698, // Gerah
159 };
160
161 private final static double _VOLUME[] = new double[] {
162 1.0, // Liter
163 1000, // Cubic Centimeter
164 61.02374, // Cubic Inch (Liquid Measure)
165 1.816166, // Pint (Dry Measure)
166 0.004729599, // Cor (Homer)
167 0.009459198, // Lethek
168 0.04729599, // Ephah
169 0.141888, // Seah
170 0.4729599, // Omer
171 0.851328, // Cab
172 0.04402868, // Bath
173 0.2641721, // Hin
174 3.170065, // Log
175 };
176
177 }