mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
fix: handle Null Dereference in UnitsConverter
(#5460)
This commit is contained in:
@ -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() {
|
||||
|
Reference in New Issue
Block a user