001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.unitconverter.util;
022:
023: import com.liferay.portlet.unitconverter.model.Conversion;
024:
025: /**
026: * <a href="ConverterUtil.java.html"><b><i>View Source</i></b></a>
027: *
028: * @author James Lefeu
029: *
030: */
031: public class ConverterUtil {
032:
033: public static int TEMPERATURE_CELSIUS = 1;
034:
035: public static int TEMPERATURE_FAHRENHEIHT = 2;
036:
037: public static Conversion getConversion(int type, int fromId,
038: int toId, double fromValue) {
039: double toValue = 0;
040:
041: if (type == 0) {
042: toValue = convertLength(fromId, toId, fromValue);
043: } else if (type == 1) {
044: toValue = convertArea(fromId, toId, fromValue);
045: } else if (type == 2) {
046: toValue = convertVolume(fromId, toId, fromValue);
047: } else if (type == 3) {
048: toValue = convertMass(fromId, toId, fromValue);
049: } else if (type == 4) {
050: toValue = convertTemperature(fromId, toId, fromValue);
051: }
052:
053: return new Conversion(type, fromId, toId, fromValue, toValue);
054: }
055:
056: public static double convertArea(int fromId, int toId,
057: double fromValue) {
058: return (fromValue / _AREA[fromId]) * _AREA[toId];
059: }
060:
061: public static double convertLength(int fromId, int toId,
062: double fromValue) {
063: return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
064: }
065:
066: public static double convertMass(int fromId, int toId,
067: double fromValue) {
068: return (fromValue / _MASS[fromId]) * _MASS[toId];
069: }
070:
071: public static double convertTemperature(int fromId, int toId,
072: double fromValue) {
073: return _fromTemperature(toId, _toTemperature(fromId, fromValue));
074: }
075:
076: public static double convertVolume(int fromId, int toId,
077: double fromValue) {
078: return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
079: }
080:
081: private final static double _fromTemperature(int toId,
082: double fromValue) {
083: if (toId == 0) {
084: return fromValue; // Kelvin
085: } else if (toId == 1) {
086: return fromValue - 273.15; // Celsius
087: } else if (toId == 2) {
088: return (1.8 * fromValue) - 459.67; // Fahrenheit
089: } else if (toId == 3) {
090: return 1.8 * fromValue; // Rankine
091: } else if (toId == 4) {
092: return .8 * (fromValue - 273.15); // Réaumure
093: } else {
094: return 0;
095: }
096: }
097:
098: private final static double _toTemperature(int fromId,
099: double fromValue) {
100: if (fromId == 0) { // Kelvin
101: return fromValue;
102: } else if (fromId == 1) { // Celsius
103: return fromValue + 273.15;
104: } else if (fromId == 2) { // Fahrenheit
105: return .5555555555 * (fromValue + 459.67);
106: } else if (fromId == 3) { // Rankine
107: return .5555555555 * fromValue;
108: } else if (fromId == 4) {
109: return (1.25 * fromValue) + 273.15; // Réaumure
110: } else {
111: return 0;
112: }
113: }
114:
115: private final static double _AREA[] = new double[] { 1.0, // Square Kilometer
116: 1000000.0, // Square Meter
117: 10000000000.0, // Square Centimeter
118: 1000000000000.0, // Square Millimeter
119: 10763910, // Square Foot
120: 1550003000, // Square Inch
121: 1195990, // Square Yard
122: 0.3861022, // Square Mile
123: 100, // Hectare
124: 247.1054, // Acre
125: };
126:
127: private final static double _LENGTH[] = new double[] { 1.0, // Meter
128: 1000.0, // Millimeter
129: 100.0, // Centimeter
130: 0.001, // Kilometer
131: 3.28084, // Foot
132: 39.37008, // Inch
133: 1.093613, // Yard
134: 0.000621, // Mile
135: 2.187227, // Cubit
136: 4.374453, // Talent
137: 13.12336 // Handbreath
138: };
139:
140: private final static double _MASS[] = new double[] { 1.0, // Kilogram
141: 2.204623, // Pound
142: 0.00110, // Ton
143: 0.02939497, // Talent
144: 1.763698, // Mina
145: 88.18491, // Shekel
146: 132.2774, // Pim
147: 176.2698, // Beka
148: 1763.698, // Gerah
149: };
150:
151: private final static double _VOLUME[] = new double[] { 1.0, // Liter
152: 1000, // Cubic Centimeter
153: 61.02374, // Cubic Inch (Liquid Measure)
154: 1.816166, // Pint (Dry Measure)
155: 0.004729599, // Cor (Homer)
156: 0.009459198, // Lethek
157: 0.04729599, // Ephah
158: 0.141888, // Seah
159: 0.4729599, // Omer
160: 0.851328, // Cab
161: 0.04402868, // Bath
162: 0.2641721, // Hin
163: 3.170065, // Log
164: };
165:
166: }
|