|
5 | 5 | import java.util.Map;
|
6 | 6 | import org.apache.commons.lang3.tuple.Pair;
|
7 | 7 |
|
| 8 | +/** |
| 9 | + * A utility class to perform unit conversions between different measurement systems. |
| 10 | + * |
| 11 | + * <p>Currently, the class supports temperature conversions between several scales: |
| 12 | + * Celsius, Fahrenheit, Kelvin, Réaumur, Delisle, and Rankine. |
| 13 | + * |
| 14 | + * <h2>Example Usage</h2> |
| 15 | + * <pre> |
| 16 | + * double result = UnitConversions.TEMPERATURE.convert("Celsius", "Fahrenheit", 100.0); |
| 17 | + * // Output: 212.0 (Celsius to Fahrenheit conversion of 100°C) |
| 18 | + * </pre> |
| 19 | + * |
| 20 | + * <p>This class makes use of an {@link UnitsConverter} that handles the conversion logic |
| 21 | + * based on predefined affine transformations. These transformations include scaling factors |
| 22 | + * and offsets for temperature conversions. |
| 23 | + * |
| 24 | + * <h2>Temperature Scales Supported</h2> |
| 25 | + * <ul> |
| 26 | + * <li>Celsius</li> |
| 27 | + * <li>Fahrenheit</li> |
| 28 | + * <li>Kelvin</li> |
| 29 | + * <li>Réaumur</li> |
| 30 | + * <li>Delisle</li> |
| 31 | + * <li>Rankine</li> |
| 32 | + * </ul> |
| 33 | + */ |
8 | 34 | public final class UnitConversions {
|
9 | 35 | private UnitConversions() {
|
10 | 36 | }
|
11 | 37 |
|
| 38 | + /** |
| 39 | + * A preconfigured instance of {@link UnitsConverter} for temperature conversions. |
| 40 | + * The converter handles conversions between the following temperature units: |
| 41 | + * <ul> |
| 42 | + * <li>Kelvin to Celsius</li> |
| 43 | + * <li>Celsius to Fahrenheit</li> |
| 44 | + * <li>Réaumur to Celsius</li> |
| 45 | + * <li>Delisle to Celsius</li> |
| 46 | + * <li>Rankine to Kelvin</li> |
| 47 | + * </ul> |
| 48 | + */ |
12 | 49 | 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)),
|
13 | 50 | 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))));
|
14 | 51 | }
|
0 commit comments