Repository: TheAlgorithms/Java
Pull request #5460 (2024-09-22 08:35:26)

fix: handle Null Dereference in `UnitsConverter`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->
Continuation of #5456.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5460?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 52.19%. Comparing base [(`3fe1de0`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/3fe1de00923ea8cf309eafb84009547440d92d40?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`96d741c`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/96d741c39a603ffd44186342b77610e1c08bb50c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5460      +/-   ##
============================================
- Coverage     52.19%   52.19%   -0.01%     
- Complexity     3244     3245       +1     
============================================
  Files           524      524              
  Lines         15158    15158              
  Branches       2881     2881              
============================================
- Hits           7912     7911       -1     
- Misses         6922     6923       +1     
  Partials        324      324              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5460?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/.inferconfig b/.inferconfig
index 96c34a54..93bd1c35 100644
--- a/.inferconfig
+++ b/.inferconfig
@@ -2,7 +2,6 @@
     "report-block-list-path-regex": [
         "src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java",
         "src/main/java/com/thealgorithms/conversions/RomanToInteger.java",
-        "src/main/java/com/thealgorithms/conversions/UnitsConverter.java",
         "src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java",
         "src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java",
         "src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java",
diff --git a/src/main/java/com/thealgorithms/conversions/UnitsConverter.java b/src/main/java/com/thealgorithms/conversions/UnitsConverter.java
index a19d4028..81c4d456 100644
--- a/src/main/java/com/thealgorithms/conversions/UnitsConverter.java
+++ b/src/main/java/com/thealgorithms/conversions/UnitsConverter.java
@@ -3,6 +3,7 @@ package com.thealgorithms.conversions;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Set;
 import org.apache.commons.lang3.tuple.Pair;
 
@@ -77,7 +78,7 @@ public final class UnitsConverter {
             throw new IllegalArgumentException("inputUnit must be different from outputUnit.");
         }
         final var conversionKey = Pair.of(inputUnit, outputUnit);
-        return conversions.get(conversionKey).convert(value);
+        return conversions.computeIfAbsent(conversionKey, k -> { throw new NoSuchElementException("No converter for: " + k); }).convert(value);
     }
 
     public Set<String> availableUnits() {


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java b/src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java
index 76e48f14..580a66bc 100644
--- a/src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java
+++ b/src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java
@@ -4,6 +4,7 @@ import static java.util.Map.entry;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.util.Map;
+import java.util.NoSuchElementException;
 import org.apache.commons.lang3.tuple.Pair;
 import org.junit.jupiter.api.Test;
 
@@ -15,4 +16,12 @@ public class UnitsConverterTest {
         assertThrows(IllegalArgumentException.class, () -> someConverter.convert("A", "A", 20.0));
         assertThrows(IllegalArgumentException.class, () -> someConverter.convert("B", "B", 20.0));
     }
+
+    @Test
+    void testConvertThrowsForUnknownUnits() {
+        final UnitsConverter someConverter = new UnitsConverter(Map.ofEntries(entry(Pair.of("A", "B"), new AffineConverter(10.0, -20.0))));
+        assertThrows(NoSuchElementException.class, () -> someConverter.convert("A", "X", 20.0));
+        assertThrows(NoSuchElementException.class, () -> someConverter.convert("X", "A", 20.0));
+        assertThrows(NoSuchElementException.class, () -> someConverter.convert("X", "Y", 20.0));
+    }
 }