001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.unitconverter.util;
016    
017    import com.liferay.portlet.unitconverter.model.Conversion;
018    
019    /**
020     * @author James Lefeu
021     */
022    public class ConverterUtil {
023    
024            public static int TEMPERATURE_CELSIUS = 1;
025    
026            public static int TEMPERATURE_FAHRENHEIHT = 2;
027    
028            public static Conversion getConversion(int type, int fromId,
029                                                                                       int toId, double fromValue) {
030                    double toValue = 0;
031    
032                    if (type == 0) {
033                            toValue = convertLength(fromId, toId, fromValue);
034                    }
035                    else if (type == 1) {
036                            toValue = convertArea(fromId, toId, fromValue);
037                    }
038                    else if (type == 2) {
039                            toValue = convertVolume(fromId, toId, fromValue);
040                    }
041                    else if (type == 3) {
042                            toValue = convertMass(fromId, toId, fromValue);
043                    }
044                    else if (type == 4) {
045                            toValue = convertTemperature(fromId, toId, fromValue);
046                    }
047    
048                    return new Conversion(type, fromId, toId, fromValue, toValue);
049            }
050    
051            public static double convertArea(int fromId, int toId, double fromValue) {
052                    return (fromValue / _AREA[fromId]) * _AREA[toId];
053            }
054    
055            public static double convertLength(int fromId, int toId, double fromValue) {
056                    return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
057            }
058    
059            public static double convertMass(int fromId, int toId, double fromValue) {
060                    return (fromValue / _MASS[fromId]) * _MASS[toId];
061            }
062    
063            public static double convertTemperature(int fromId, int toId,
064                                                                                            double fromValue) {
065                    return _fromTemperature(toId, _toTemperature(fromId, fromValue));
066            }
067    
068            public static double convertVolume(int fromId, int toId, double fromValue) {
069                    return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
070            }
071    
072            private final static double _fromTemperature(int toId, double fromValue) {
073                    if (toId == 0) {
074                            return fromValue;                                  // Kelvin
075                    }
076                    else if (toId == 1) {
077                            return fromValue - 273.15;                 // Celsius
078                    }
079                    else if (toId == 2) {
080                            return (1.8 * fromValue) - 459.67; // Fahrenheit
081                    }
082                    else if (toId == 3) {
083                            return 1.8 * fromValue;                            // Rankine
084                    }
085                    else if (toId == 4) {
086                            return .8 * (fromValue - 273.15);  // R?aumure
087                    }
088                    else {
089                            return 0;
090                    }
091            }
092    
093            private final static double _toTemperature(int fromId, double fromValue) {
094                    if (fromId == 0) {                                         // Kelvin
095                            return fromValue;
096                    }
097                    else if (fromId == 1) {                                       // Celsius
098                            return fromValue + 273.15;
099                    }
100                    else if (fromId == 2) {                               // Fahrenheit
101                            return .5555555555 * (fromValue + 459.67);
102                    }
103                    else if (fromId == 3) {                               // Rankine
104                            return .5555555555 * fromValue;
105                    }
106                    else if (fromId == 4) {
107                            return (1.25 * fromValue) + 273.15;        // R?aumure
108                    }
109                    else {
110                            return 0;
111                    }
112            }
113    
114            private final static double _AREA[] = new double[] {
115                    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[] {
128                    1.0,                            // Meter
129                    1000.0,                         // Millimeter
130                    100.0,                          // Centimeter
131                    0.001,                          // Kilometer
132                    3.28084,                        // Foot
133                    39.37008,                       // Inch
134                    1.093613,                       // Yard
135                    0.000621,                       // Mile
136                    2.187227,                       // Cubit
137                    4.374453,                       // Talent
138                    13.12336                        // Handbreath
139            };
140    
141            private final static double _MASS[] = new double[] {
142                    1.0,                            // Kilogram
143                    2.204623,                       // Pound
144                    0.00110,                        // Ton
145                    0.02939497,                     // Talent
146                    1.763698,                       // Mina
147                    88.18491,                       // Shekel
148                    132.2774,                       // Pim
149                    176.2698,                       // Beka
150                    1763.698,                       // Gerah
151            };
152    
153            private final static double _VOLUME[] = new double[] {
154                    1.0,                            // Liter
155                    1000,                           // Cubic Centimeter
156                    61.02374,                       // Cubic Inch (Liquid Measure)
157                    1.816166,                       // Pint (Dry Measure)
158                    0.004729599,            // Cor (Homer)
159                    0.009459198,            // Lethek
160                    0.04729599,                     // Ephah
161                    0.141888,                       // Seah
162                    0.4729599,                      // Omer
163                    0.851328,                       // Cab
164                    0.04402868,                     // Bath
165                    0.2641721,                      // Hin
166                    3.170065,                       // Log
167            };
168    
169    }