Handle edge cases of Math.abs on integers.

This commit is contained in:
erik-signal
2022-12-20 12:25:04 -05:00
committed by GitHub
parent 2c2c497c12
commit d138fa45df
5 changed files with 42 additions and 5 deletions

View File

@@ -191,4 +191,29 @@ public class Util {
public static Optional<String> findBestLocale(List<LanguageRange> priorityList, Collection<String> supportedLocales) {
return Optional.ofNullable(Locale.lookupTag(priorityList, supportedLocales));
}
/**
* Map ints to non-negative ints.
* <br>
* Unlike Math.abs this method handles Integer.MIN_VALUE correctly.
*
* @param n any int value
* @return an int value guaranteed to be non-negative
*/
public static int ensureNonNegativeInt(int n) {
return n == Integer.MIN_VALUE ? 0 : Math.abs(n);
}
/**
* Map longs to non-negative longs.
* <br>
* Unlike Math.abs this method handles Long.MIN_VALUE correctly.
*
* @param n any long value
* @return a long value guaranteed to be non-negative
*/
public static long ensureNonNegativeLong(long n) {
return n == Long.MIN_VALUE ? 0 : Math.abs(n);
}
}