001    /**
002     * Copyright (c) 2000-2013 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 final int TEMPERATURE_CELSIUS = 1;
025    
026            public static final int TEMPERATURE_FAHRENHEIHT = 2;
027    
028            public static double convertArea(int fromId, int toId, double fromValue) {
029                    return (fromValue / _AREA[fromId]) * _AREA[toId];
030            }
031    
032            public static double convertLength(int fromId, int toId, double fromValue) {
033                    return (fromValue / _LENGTH[fromId]) * _LENGTH[toId];
034            }
035    
036            public static double convertMass(int fromId, int toId, double fromValue) {
037                    return (fromValue / _MASS[fromId]) * _MASS[toId];
038            }
039    
040            public static double convertTemperature(
041                    int fromId, int toId, double fromValue) {
042    
043                    return _fromTemperature(toId, _toTemperature(fromId, fromValue));
044            }
045    
046            public static double convertVolume(int fromId, int toId, double fromValue) {
047                    return (fromValue / _VOLUME[fromId]) * _VOLUME[toId];
048            }
049    
050            public static Conversion getConversion(
051                    int type, int fromId, int toId, double fromValue) {
052    
053                    double toValue = 0;
054    
055                    if (type == 0) {
056                            toValue = convertLength(fromId, toId, fromValue);
057                    }
058                    else if (type == 1) {
059                            toValue = convertArea(fromId, toId, fromValue);
060                    }
061                    else if (type == 2) {
062                            toValue = convertVolume(fromId, toId, fromValue);
063                    }
064                    else if (type == 3) {
065                            toValue = convertMass(fromId, toId, fromValue);
066                    }
067                    else if (type == 4) {
068                            toValue = convertTemperature(fromId, toId, fromValue);
069                    }
070    
071                    return new Conversion(type, fromId, toId, fromValue, toValue);
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 static final 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 static final 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 static final 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 static final 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    }