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