Skip to content

Commit

Permalink
Don't rely on a Java 1.8 feature during number conversion.
Browse files Browse the repository at this point in the history
Increases compatibility, e.g. for Anrdoid.
  • Loading branch information
jimblacklercorp committed Apr 2, 2021
1 parent f9e0d1a commit 59a7fda
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ afterEvaluate {
release(MavenPublication) {
group = 'net.jimblackler'
artifactId = 'usejson'
version = '0.2.0'
version = '0.2.1'
artifact(sourceJar)
from components.java
}
Expand Down
27 changes: 15 additions & 12 deletions library/src/main/java/net/jimblackler/usejson/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@

public class NumberUtils {
public static Object toBestObject(BigInteger bigInteger) {
try {
return bigInteger.byteValueExact();
} catch (ArithmeticException ignored) {
if (bigInteger.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) >= 0
&& bigInteger.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) <= 0) {
return bigInteger.byteValue();
}
try {
return bigInteger.shortValueExact();
} catch (ArithmeticException ignored) {

if (bigInteger.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) >= 0
&& bigInteger.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) <= 0) {
return bigInteger.shortValue();
}
try {
return bigInteger.intValueExact();
} catch (ArithmeticException ignored) {

if (bigInteger.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0
&& bigInteger.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0) {
return bigInteger.intValue();
}
try {
return bigInteger.longValueExact();
} catch (ArithmeticException ignored) {

if (bigInteger.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0
&& bigInteger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) {
return bigInteger.longValue();
}
return bigInteger;
}
Expand Down

0 comments on commit 59a7fda

Please sign in to comment.