| ConverterUtil.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions 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 */
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; // Kelvin
86 }
87 else if (toId == 1) {
88 return fromValue - 273.15; // Celsius
89 }
90 else if (toId == 2) {
91 return (1.8 * fromValue) - 459.67; // Fahrenheit
92 }
93 else if (toId == 3) {
94 return 1.8 * fromValue; // Rankine
95 }
96 else if (toId == 4) {
97 return .8 * (fromValue - 273.15); // R�aumure
98 }
99 else {
100 return 0;
101 }
102 }
103
104 private final static double _toTemperature(int fromId, double fromValue) {
105 if (fromId == 0) { // Kelvin
106 return fromValue;
107 }
108 else if (fromId == 1) { // Celsius
109 return fromValue + 273.15;
110 }
111 else if (fromId == 2) { // Fahrenheit
112 return .5555555555 * (fromValue + 459.67);
113 }
114 else if (fromId == 3) { // Rankine
115 return .5555555555 * fromValue;
116 }
117 else if (fromId == 4) {
118 return (1.25 * fromValue) + 273.15; // R�aumure
119 }
120 else {
121 return 0;
122 }
123 }
124
125 private final static double _AREA[] = new double[] {
126 1.0, // Square Kilometer
127 1000000.0, // Square Meter
128 10000000000.0, // Square Centimeter
129 1000000000000.0, // Square Millimeter
130 10763910, // Square Foot
131 1550003000, // Square Inch
132 1195990, // Square Yard
133 0.3861022, // Square Mile
134 100, // Hectare
135 247.1054, // Acre
136 };
137
138 private final static double _LENGTH[] = new double[] {
139 1.0, // Meter
140 1000.0, // Millimeter
141 100.0, // Centimeter
142 0.001, // Kilometer
143 3.28084, // Foot
144 39.37008, // Inch
145 1.093613, // Yard
146 0.000621, // Mile
147 2.187227, // Cubit
148 4.374453, // Talent
149 13.12336 // Handbreath
150 };
151
152 private final static double _MASS[] = new double[] {
153 1.0, // Kilogram
154 2.204623, // Pound
155 0.00110, // Ton
156 0.02939497, // Talent
157 1.763698, // Mina
158 88.18491, // Shekel
159 132.2774, // Pim
160 176.2698, // Beka
161 1763.698, // Gerah
162 };
163
164 private final static double _VOLUME[] = new double[] {
165 1.0, // Liter
166 1000, // Cubic Centimeter
167 61.02374, // Cubic Inch (Liquid Measure)
168 1.816166, // Pint (Dry Measure)
169 0.004729599, // Cor (Homer)
170 0.009459198, // Lethek
171 0.04729599, // Ephah
172 0.141888, // Seah
173 0.4729599, // Omer
174 0.851328, // Cab
175 0.04402868, // Bath
176 0.2641721, // Hin
177 3.170065, // Log
178 };
179
180 }