001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
029                    int type, int fromId, int toId, double fromValue) {
030    
031                    double toValue = 0;
032    
033                    if (type == 0) {
034                            toValue = convertLength(fromId, toId, fromValue);
035                    }
036                    else if (type == 1) {
037                            toValue = convertArea(fromId, toId, fromValue);
038                    }
039                    else if (type == 2) {
040                            toValue = convertVolume(fromId, toId, fromValue);
041                    }
042                    else if (type == 3) {
043                            toValue = convertMass(fromId, toId, fromValue);
044                    }
045                    else if (type == 4) {
046                            toValue = convertTemperature(fromId, toId, fromValue);
047                    }
048    
049                    return new Conversion(type, fromId, toId, fromValue, toValue);
050            }
051    
052            public static double convertArea(int fromId, int toId, double fromValue) {
053                    return (fromValue / _AREA[fromId]) * _AREA[toId];
054            }
055    
056            public static double convertLength(int fromId, int toId, double fromValue) {
057                    return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
058            }
059    
060            public static double convertMass(int fromId, int toId, double fromValue) {
061                    return (fromValue / _MASS[fromId]) * _MASS[toId];
062            }
063    
064            public static double convertTemperature(
065                    int fromId, int toId, double fromValue) {
066    
067                    return _fromTemperature(toId, _toTemperature(fromId, fromValue));
068            }
069    
070            public static double convertVolume(int fromId, int toId, double fromValue) {
071                    return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
072            }
073    
074            private final static double _fromTemperature(int toId, double fromValue) {
075                    if (toId == 0) {
076                            return fromValue;                                  // Kelvin
077                    }
078                    else if (toId == 1) {
079                            return fromValue - 273.15;                 // Celsius
080                    }
081                    else if (toId == 2) {
082                            return (1.8 * fromValue) - 459.67; // Fahrenheit
083                    }
084                    else if (toId == 3) {
085                            return 1.8 * fromValue;                            // Rankine
086                    }
087                    else if (toId == 4) {
088                            return .8 * (fromValue - 273.15);  // R?aumure
089                    }
090                    else {
091                            return 0;
092                    }
093            }
094    
095            private final static double _toTemperature(int fromId, double fromValue) {
096                    if (fromId == 0) {                                         // Kelvin
097                            return fromValue;
098                    }
099                    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    }