| ConverterUtil.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 */
14
15 package com.liferay.portlet.unitconverter.util;
16
17 import com.liferay.portlet.unitconverter.model.Conversion;
18
19 /**
20 * <a href="ConverterUtil.java.html"><b><i>View Source</i></b></a>
21 *
22 * @author James Lefeu
23 */
24 public class ConverterUtil {
25
26 public static int TEMPERATURE_CELSIUS = 1;
27
28 public static int TEMPERATURE_FAHRENHEIHT = 2;
29
30 public static Conversion getConversion(int type, int fromId,
31 int toId, double fromValue) {
32 double toValue = 0;
33
34 if (type == 0) {
35 toValue = convertLength(fromId, toId, fromValue);
36 }
37 else if (type == 1) {
38 toValue = convertArea(fromId, toId, fromValue);
39 }
40 else if (type == 2) {
41 toValue = convertVolume(fromId, toId, fromValue);
42 }
43 else if (type == 3) {
44 toValue = convertMass(fromId, toId, fromValue);
45 }
46 else if (type == 4) {
47 toValue = convertTemperature(fromId, toId, fromValue);
48 }
49
50 return new Conversion(type, fromId, toId, fromValue, toValue);
51 }
52
53 public static double convertArea(int fromId, int toId, double fromValue) {
54 return (fromValue / _AREA[fromId]) * _AREA[toId];
55 }
56
57 public static double convertLength(int fromId, int toId, double fromValue) {
58 return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
59 }
60
61 public static double convertMass(int fromId, int toId, double fromValue) {
62 return (fromValue / _MASS[fromId]) * _MASS[toId];
63 }
64
65 public static double convertTemperature(int fromId, int toId,
66 double fromValue) {
67 return _fromTemperature(toId, _toTemperature(fromId, fromValue));
68 }
69
70 public static double convertVolume(int fromId, int toId, double fromValue) {
71 return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
72 }
73
74 private final static double _fromTemperature(int toId, double fromValue) {
75 if (toId == 0) {
76 return fromValue; // Kelvin
77 }
78 else if (toId == 1) {
79 return fromValue - 273.15; // Celsius
80 }
81 else if (toId == 2) {
82 return (1.8 * fromValue) - 459.67; // Fahrenheit
83 }
84 else if (toId == 3) {
85 return 1.8 * fromValue; // Rankine
86 }
87 else if (toId == 4) {
88 return .8 * (fromValue - 273.15); // R?aumure
89 }
90 else {
91 return 0;
92 }
93 }
94
95 private final static double _toTemperature(int fromId, double fromValue) {
96 if (fromId == 0) { // Kelvin
97 return fromValue;
98 }
99 else if (fromId == 1) { // Celsius
100 return fromValue + 273.15;
101 }
102 else if (fromId == 2) { // Fahrenheit
103 return .5555555555 * (fromValue + 459.67);
104 }
105 else if (fromId == 3) { // Rankine
106 return .5555555555 * fromValue;
107 }
108 else if (fromId == 4) {
109 return (1.25 * fromValue) + 273.15; // R?aumure
110 }
111 else {
112 return 0;
113 }
114 }
115
116 private final static double _AREA[] = new double[] {
117 1.0, // Square Kilometer
118 1000000.0, // Square Meter
119 10000000000.0, // Square Centimeter
120 1000000000000.0, // Square Millimeter
121 10763910, // Square Foot
122 1550003000, // Square Inch
123 1195990, // Square Yard
124 0.3861022, // Square Mile
125 100, // Hectare
126 247.1054, // Acre
127 };
128
129 private final static double _LENGTH[] = new double[] {
130 1.0, // Meter
131 1000.0, // Millimeter
132 100.0, // Centimeter
133 0.001, // Kilometer
134 3.28084, // Foot
135 39.37008, // Inch
136 1.093613, // Yard
137 0.000621, // Mile
138 2.187227, // Cubit
139 4.374453, // Talent
140 13.12336 // Handbreath
141 };
142
143 private final static double _MASS[] = new double[] {
144 1.0, // Kilogram
145 2.204623, // Pound
146 0.00110, // Ton
147 0.02939497, // Talent
148 1.763698, // Mina
149 88.18491, // Shekel
150 132.2774, // Pim
151 176.2698, // Beka
152 1763.698, // Gerah
153 };
154
155 private final static double _VOLUME[] = new double[] {
156 1.0, // Liter
157 1000, // Cubic Centimeter
158 61.02374, // Cubic Inch (Liquid Measure)
159 1.816166, // Pint (Dry Measure)
160 0.004729599, // Cor (Homer)
161 0.009459198, // Lethek
162 0.04729599, // Ephah
163 0.141888, // Seah
164 0.4729599, // Omer
165 0.851328, // Cab
166 0.04402868, // Bath
167 0.2641721, // Hin
168 3.170065, // Log
169 };
170
171 }