001package com.pi4j.temperature;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  TemperatureConversion.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2019 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033public class TemperatureConversion
034{
035    public static final double ABSOLUTE_ZERO_CELSIUS = -273.15;
036    public static final double ABSOLUTE_ZERO_FARENHEIT = -459.67;
037    public static final double ABSOLUTE_ZERO_KELVIN = 0;
038    public static final double ABSOLUTE_ZERO_RANKINE = 0;
039
040    /**
041     * Convert a temperature value from one temperature scale to another.
042     *
043     * @param from TemperatureScale
044     * @param to TemperatureScale
045     * @param temperature value
046     * @return converted temperature value in the requested to scale
047     */
048    public static double convert(TemperatureScale from, TemperatureScale to, double temperature) {
049
050        switch(from) {
051
052            case FARENHEIT:
053                return convertFromFarenheit(to, temperature);
054            case CELSIUS:
055                return convertFromCelsius(to, temperature);
056            case KELVIN:
057                return convertFromKelvin(to, temperature);
058            case RANKINE:
059                return convertFromRankine(to, temperature);
060            default:
061                throw(new RuntimeException("Invalid termpature conversion"));
062        }
063    }
064
065
066    /**
067     * Convert a temperature value from the Farenheit temperature scale to another.
068     *
069     * @param to TemperatureScale
070     * @param temperature value in degrees Farenheit
071     * @return converted temperature value in the requested to scale
072     */
073    public static double convertFromFarenheit (TemperatureScale to, double temperature) {
074
075        switch(to) {
076
077            case FARENHEIT:
078                return temperature;
079            case CELSIUS:
080                return convertFarenheitToCelsius(temperature);
081            case KELVIN:
082                return convertFarenheitToKelvin(temperature);
083            case RANKINE:
084                return convertFarenheitToRankine(temperature);
085            default:
086                throw(new RuntimeException("Invalid termpature conversion"));
087        }
088    }
089
090    /**
091     * Convert a temperature value from another temperature scale into the Farenheit temperature scale.
092     *
093     * @param from TemperatureScale
094     * @param temperature value from other scale
095     * @return converted temperature value in degrees Farenheit
096     */
097    public static double convertToFarenheit (TemperatureScale from, double temperature) {
098
099        switch(from) {
100
101            case FARENHEIT:
102                return temperature;
103            case CELSIUS:
104                return convertCelsiusToFarenheit(temperature);
105            case KELVIN:
106                return convertKelvinToFarenheit(temperature);
107            case RANKINE:
108                return convertRankineToFarenheit(temperature);
109            default:
110                throw(new RuntimeException("Invalid termpature conversion"));
111        }
112    }
113
114    /**
115     * Convert a temperature value from the Celsius temperature scale to another.
116     *
117     * @param to TemperatureScale
118     * @param temperature value in degrees centigrade
119     * @return converted temperature value in the requested to scale
120     */
121    public static double convertFromCelsius(TemperatureScale to, double temperature) {
122
123        switch(to) {
124
125            case FARENHEIT:
126                return convertCelsiusToFarenheit(temperature);
127            case CELSIUS:
128                return temperature;
129            case KELVIN:
130                return convertCelsiusToKelvin(temperature);
131            case RANKINE:
132                return convertCelsiusToRankine(temperature);
133            default:
134                throw(new RuntimeException("Invalid termpature conversion"));
135        }
136    }
137
138    /**
139     * Convert a temperature value from another temperature scale into the Celsius temperature scale.
140     *
141     * @param from TemperatureScale
142     * @param temperature value from other scale
143     * @return converted temperature value in degrees centigrade
144     */
145    public static double convertToCelsius (TemperatureScale from, double temperature) {
146
147        switch(from) {
148
149            case FARENHEIT:
150                return convertFarenheitToCelsius(temperature);
151            case CELSIUS:
152                return temperature;
153            case KELVIN:
154                return convertKelvinToCelsius(temperature);
155            case RANKINE:
156                return convertRankineToCelsius(temperature);
157            default:
158                throw(new RuntimeException("Invalid termpature conversion"));
159        }
160    }
161
162    /**
163     * Convert a temperature value from the Kelvin temperature scale to another.
164     *
165     * @param to TemperatureScale
166     * @param temperature value in Kelvin
167     * @return converted temperature value in the requested to scale
168     */
169    public static double convertFromKelvin(TemperatureScale to, double temperature) {
170
171        switch(to) {
172
173            case FARENHEIT:
174                return convertKelvinToFarenheit(temperature);
175            case CELSIUS:
176                return convertKelvinToCelsius(temperature);
177            case KELVIN:
178                return temperature;
179            case RANKINE:
180                return convertKelvinToRankine(temperature);
181            default:
182                throw(new RuntimeException("Invalid termpature conversion"));
183        }
184    }
185
186    /**
187     * Convert a temperature value from another temperature scale into the Kelvin temperature scale.
188     *
189     * @param from TemperatureScale
190     * @param temperature value from other scale
191     * @return converted temperature value in Kelvin
192     */
193    public static double convertToKelvin(TemperatureScale from, double temperature) {
194
195        switch(from) {
196
197            case FARENHEIT:
198                return convertFarenheitToKelvin(temperature);
199            case CELSIUS:
200                return convertCelsiusToKelvin(temperature);
201            case KELVIN:
202                return temperature;
203            case RANKINE:
204                return convertRankineToKelvin(temperature);
205            default:
206                throw(new RuntimeException("Invalid termpature conversion"));
207        }
208    }
209
210    /**
211     * Convert a temperature value from the Rankine temperature scale to another.
212     *
213     * @param to TemperatureScale
214     * @param temperature value in degrees Rankine
215     * @return converted temperature value in the requested to scale
216     */
217    public static double convertFromRankine(TemperatureScale to, double temperature) {
218
219        switch(to) {
220
221            case FARENHEIT:
222                return convertRankineToFarenheit(temperature);
223            case CELSIUS:
224                return convertRankineToCelsius(temperature);
225            case KELVIN:
226                return convertRankineToKelvin(temperature);
227            case RANKINE:
228                return temperature;
229            default:
230                throw(new RuntimeException("Invalid termpature conversion"));
231        }
232    }
233
234    /**
235     * Convert a temperature value from another temperature scale into the Rankine temperature scale.
236     *
237     * @param from TemperatureScale
238     * @param temperature value from other scale
239     * @return converted temperature value in degrees Rankine
240     */
241    public static double convertToRankine(TemperatureScale from, double temperature) {
242
243        switch(from) {
244
245            case FARENHEIT:
246                return convertFarenheitToRankine(temperature);
247            case CELSIUS:
248                return convertCelsiusToRankine(temperature);
249            case KELVIN:
250                return convertKelvinToRankine(temperature);
251            case RANKINE:
252                return temperature;
253            default:
254                throw(new RuntimeException("Invalid termpature conversion"));
255        }
256    }
257
258    /**
259     * Convert temperature from Farenheit to Celsius temperature scale
260     *
261     * FORMULA = [°C] = ([°F] − 32) × 5/9
262     *
263     * @param temperature value in degrees Farenheit
264     * @return converted temperature value in degrees Celsius
265     */
266    public static double convertFarenheitToCelsius(double temperature) {
267        return ((temperature - 32) * 5/9);
268    }
269
270    /**
271     * Convert temperature from Farenheit to Kelvin temperature scale
272     *
273     * FORMULA = [K] = ([°F] + 459.67) × 5/9
274     *
275     * @param temperature value in degrees Farenheit
276     * @return converted temperature value in degrees Kelvin
277     */
278    public static double convertFarenheitToKelvin(double temperature) {
279        return (((temperature + 459.67) * 5) / 9);
280    }
281
282    /**
283     * Convert temperature from Farenheit to Rankine temperature scale
284     *
285     * FORMULA = [°R] = [°F] + 459.67
286     *
287     * @param temperature value in degrees Farenheit
288     * @return converted temperature value in degrees Rankine
289     */
290    public static double convertFarenheitToRankine(double temperature) {
291        return temperature + 459.67;
292    }
293
294    /**
295     * Convert temperature from Celsius to Farenheit temperature scale
296     *
297     * FORMULA = [°F] = [°C] × 9/5 + 32
298     *
299     * @param temperature value in degrees Celsius
300     * @return converted temperature value in degrees Farenheit
301     */
302    public static double convertCelsiusToFarenheit(double temperature) {
303        return (((temperature * 9) / 5) + 32);
304    }
305
306    /**
307     * Convert temperature from Celsius to Kelvin temperature scale
308     *
309     * FORMULA = [K] = [°C] + 273.15
310     *
311     * @param temperature value in degrees Celsius
312     * @return converted temperature value in degrees Kelvin
313     */
314    public static double convertCelsiusToKelvin(double temperature) {
315        return (temperature - ABSOLUTE_ZERO_CELSIUS);
316    }
317
318    /**
319     * Convert temperature from Celsius to Rankine temperature scale
320     *
321     * FORMULA = [°R] = ([°C] + 273.15) × 9/5
322     *
323     * @param temperature value in degrees Celsius
324     * @return converted temperature value in degrees Rankine
325     */
326    public static double convertCelsiusToRankine(double temperature) {
327        return (((temperature-ABSOLUTE_ZERO_CELSIUS) * 9) / 5);
328    }
329
330    /**
331     * Convert temperature from Kelvin to Celsius temperature scale
332     *
333     * FORMULA = [°C] = [K] − 273.15
334     *
335     * @param temperature value in degrees Kelvin
336     * @return converted temperature value in degrees Celsius
337     */
338    public static double convertKelvinToCelsius(double temperature) {
339        return (temperature + ABSOLUTE_ZERO_CELSIUS);
340    }
341
342    /**
343     * Convert temperature from Kelvin to Farenheit temperature scale
344     *
345     * FORMULA = [°F] = [K] × 9/5 − 459.67
346     *
347     * @param temperature value in degrees Kelvin
348     * @return converted temperature value in degrees Farenheit
349     */
350    public static double convertKelvinToFarenheit(double temperature) {
351        return (((temperature * 9) / 5) - 459.67);
352    }
353
354    /**
355     * Convert temperature from Kelvin to Rankine temperature scale
356     *
357     * FORMULA = [°R] = [K] × 9/5
358     *
359     * @param temperature value in degrees Kelvin
360     * @return converted temperature value in degrees Rankine
361     */
362    public static double convertKelvinToRankine(double temperature) {
363        return ((temperature * 9) / 5);
364    }
365
366    /**
367     * Convert temperature from Rankine to Farenheit temperature scale
368     *
369     * FORMULA = [°F] = [°R] − 459.67
370     *
371     * @param temperature value in degrees Rankine
372     * @return converted temperature value in degrees Farenheit
373     */
374    public static double convertRankineToFarenheit(double temperature) {
375        return (temperature-(459.67));
376    }
377
378    /**
379     * Convert temperature from Rankine to Celsius temperature scale
380     *
381     * FORMULA = [°C] = ([°R] − 491.67) × 5/9
382     *
383     * @param temperature value in degrees Rankine
384     * @return converted temperature value in degrees Celsius
385     */
386    public static double convertRankineToCelsius(double temperature) {
387        return (((temperature-491.67)* 5) / 9);
388    }
389
390    /**
391     * Convert temperature from Rankine to Kelvin temperature scale
392     *
393     * FORMULA = [K] = [°R] × 5/9
394     *
395     * @param temperature value in degrees Rankine
396     * @return converted temperature value in degrees Kelvin
397     */
398    public static double convertRankineToKelvin(double temperature) {
399        return ((temperature * 5) / 9);
400    }
401}