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