Skip to content

Commit

Permalink
#182 BigNumber support: ignore extensions and increase threshold for …
Browse files Browse the repository at this point in the history
…scientific notation

Address remarks by @Sxtanna:
- Do not export values of a type that cannot be imported (BigDecimal and BigInteger are not final)
- Increase value threshold determining whether scientific notation should be used
  • Loading branch information
ljacqu committed Apr 20, 2021
1 parent 17d15a4 commit eb24f44
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
public class BigNumberLeafValueHandler extends AbstractLeafValueHandler {

/** Value after which scientific notation (like "1E+30") might be used when exporting BigDecimal values. */
private static final BigDecimal BIG_DECIMAL_SCIENTIFIC_THRESHOLD = new BigDecimal("1E10");
/** Value after which scientific notation (like "1E+130") might be used when exporting BigDecimal values. */
private static final BigDecimal BIG_DECIMAL_SCIENTIFIC_THRESHOLD = new BigDecimal("1E100");

@Override
protected Object convert(Class<?> clazz, Object value) {
Expand All @@ -28,9 +28,10 @@ protected Object convert(Class<?> clazz, Object value) {

@Override
public Object toExportValue(Object value) {
if (value instanceof BigInteger) {
final Class<?> valueType = value == null ? null : value.getClass();
if (valueType == BigInteger.class) {
return value.toString();
} else if (value instanceof BigDecimal) {
} else if (valueType == BigDecimal.class) {
BigDecimal bigDecimal = (BigDecimal) value;
return bigDecimal.abs().compareTo(BIG_DECIMAL_SCIENTIFIC_THRESHOLD) >= 0
? bigDecimal.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ch.jalu.configme.samples.TestEnum;
import ch.jalu.configme.utils.TypeInformation;
import org.junit.jupiter.api.Test;
import org.w3c.dom.TypeInfo;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -129,7 +128,15 @@ void shouldExportBigDecimalValuesCorrectly() {
assertThat(bigNumberHandler.toExportValue(new BigDecimal("9123456789.43214321")), equalTo("9123456789.43214321"));
assertThat(bigNumberHandler.toExportValue(new BigDecimal("-9999999999.999999")), equalTo("-9999999999.999999"));
assertThat(bigNumberHandler.toExportValue(new BigDecimal("-2E3")), equalTo("-2000"));
assertThat(bigNumberHandler.toExportValue(new BigDecimal("-2.5E+10")), equalTo("-2.5E+10"));
assertThat(bigNumberHandler.toExportValue(new BigDecimal("7.4718329E40")), equalTo("74718329000000000000000000000000000000000"));
assertThat(bigNumberHandler.toExportValue(new BigDecimal("-2.5E+221")), equalTo("-2.5E+221"));

// BigDecimal toString can have odd, unexpected behavior and depends on the way it was created (as seen above).
// For completeness, we check a few export outputs from BigDecimals constructed via other methods.
assertThat(bigNumberHandler.toExportValue(BigDecimal.valueOf(29384723984.9234)), equalTo("29384723984.9234"));
assertThat(bigNumberHandler.toExportValue(BigDecimal.valueOf(8523327856898475L)), equalTo("8523327856898475"));
assertThat(bigNumberHandler.toExportValue(BigDecimal.valueOf(-456, -30)), equalTo("-456000000000000000000000000000000"));
assertThat(bigNumberHandler.toExportValue(BigDecimal.valueOf(-456, -101)), equalTo("-4.56E+103"));
}

@Test
Expand All @@ -141,7 +148,34 @@ void shouldNotExportUnsupportedTypes() {
assertThat(bigNumberHandler.toExportValue(TestEnum.SECOND), nullValue());
}

@Test
void shouldNotHandleExtensionsOfBigNumberClasses() {
// given
BigDecimal bigDecimalExt = new TestBigDecimalDummyExt("1.41421");
BigInteger bigIntegerExt = new TestBigIntegerDummyExt("5432");

// when / then
assertThat(bigNumberHandler.toExportValue(bigIntegerExt), nullValue());
assertThat(bigNumberHandler.toExportValue(bigDecimalExt), nullValue());
assertThat(bigNumberHandler.convert(new TypeInformation(TestBigDecimalDummyExt.class), "2"), nullValue());
assertThat(bigNumberHandler.convert(new TypeInformation(TestBigIntegerDummyExt.class), "7"), nullValue());
}

private static BigInteger newBigInteger(String value) {
return new BigDecimal(value).toBigIntegerExact();
}

private static final class TestBigDecimalDummyExt extends BigDecimal {

public TestBigDecimalDummyExt(String val) {
super(val);
}
}

private static final class TestBigIntegerDummyExt extends BigInteger {

public TestBigIntegerDummyExt(String val) {
super(val);
}
}
}

0 comments on commit eb24f44

Please sign in to comment.