-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
UnitConversions.java
51 lines (47 loc) · 1.92 KB
/
UnitConversions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.thealgorithms.conversions;
import static java.util.Map.entry;
import java.util.Map;
import org.apache.commons.lang3.tuple.Pair;
/**
* A utility class to perform unit conversions between different measurement systems.
*
* <p>Currently, the class supports temperature conversions between several scales:
* Celsius, Fahrenheit, Kelvin, Réaumur, Delisle, and Rankine.
*
* <h2>Example Usage</h2>
* <pre>
* double result = UnitConversions.TEMPERATURE.convert("Celsius", "Fahrenheit", 100.0);
* // Output: 212.0 (Celsius to Fahrenheit conversion of 100°C)
* </pre>
*
* <p>This class makes use of an {@link UnitsConverter} that handles the conversion logic
* based on predefined affine transformations. These transformations include scaling factors
* and offsets for temperature conversions.
*
* <h2>Temperature Scales Supported</h2>
* <ul>
* <li>Celsius</li>
* <li>Fahrenheit</li>
* <li>Kelvin</li>
* <li>Réaumur</li>
* <li>Delisle</li>
* <li>Rankine</li>
* </ul>
*/
public final class UnitConversions {
private UnitConversions() {
}
/**
* A preconfigured instance of {@link UnitsConverter} for temperature conversions.
* The converter handles conversions between the following temperature units:
* <ul>
* <li>Kelvin to Celsius</li>
* <li>Celsius to Fahrenheit</li>
* <li>Réaumur to Celsius</li>
* <li>Delisle to Celsius</li>
* <li>Rankine to Kelvin</li>
* </ul>
*/
public static final UnitsConverter TEMPERATURE = new UnitsConverter(Map.ofEntries(entry(Pair.of("Kelvin", "Celsius"), new AffineConverter(1.0, -273.15)), entry(Pair.of("Celsius", "Fahrenheit"), new AffineConverter(9.0 / 5.0, 32.0)),
entry(Pair.of("Réaumur", "Celsius"), new AffineConverter(5.0 / 4.0, 0.0)), entry(Pair.of("Delisle", "Celsius"), new AffineConverter(-2.0 / 3.0, 100.0)), entry(Pair.of("Rankine", "Kelvin"), new AffineConverter(5.0 / 9.0, 0.0))));
}