Skip to content

Commit

Permalink
Changing from BigNumeric to Numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobStocklass committed Mar 29, 2021
1 parent f90e636 commit 20ff9e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,23 @@
import java.math.BigInteger;

public class BigDecimalByteStringEncoder {
private static int BigDecimalScale = 38;
private static final BigDecimal MAX_BIGNUMERIC_VALUE =
new BigDecimal(
"578960446186580977117854925043439539266.34992332820282019728792003956564819967");
private static final BigDecimal MIN_BIGNUMERIC_VALUE =
new BigDecimal(
"-578960446186580977117854925043439539266.34992332820282019728792003956564819968");
private static int NumericScale = 9;
private static final BigDecimal MAX_NUMERIC_VALUE =
new BigDecimal("99999999999999999999999999999.999999999");
private static final BigDecimal MIN_NUMERIC_VALUE =
new BigDecimal("-99999999999999999999999999999.999999999");

public static ByteString encodeToNumericByteString(BigDecimal bigDecimal) {
ByteString byteString =
serializeBigDecimal(
bigDecimal, BigDecimalScale, MAX_BIGNUMERIC_VALUE, MIN_BIGNUMERIC_VALUE, "ByteString");
bigDecimal, NumericScale, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "ByteString");
return byteString;
}

public static BigDecimal decodeToBigDecimal(ByteString byteString) {
public static BigDecimal decodeNumericByteString(ByteString byteString) {
BigDecimal bigDecimal =
deserializeBigDecimal(
byteString, BigDecimalScale, MAX_BIGNUMERIC_VALUE, MIN_BIGNUMERIC_VALUE, "BigDecimal");
byteString, NumericScale, MAX_NUMERIC_VALUE, MIN_NUMERIC_VALUE, "BigDecimal");
return bigDecimal;
}
// Make these private and make public wrapper that internalizes these min/max/scale/type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,35 @@
public class BigDecimalByteStringEncoderTest {
@Test
public void testEncodeBigDecimalandEncodeByteString() {
BigDecimal testBD = BigDecimal.valueOf(0); // expected result bd
BigDecimal testBD = new BigDecimal("0"); // expected result bd
ByteString testBS =
BigDecimalByteStringEncoder.encodeToNumericByteString(testBD); // convert expected to bs
BigDecimal resultBD =
BigDecimalByteStringEncoder.decodeToBigDecimal(testBS); // convert bs to bd
BigDecimalByteStringEncoder.decodeNumericByteString(testBS); // convert bs to bd
Assert.assertEquals(
0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd

testBD = BigDecimal.valueOf(1.2);
testBD = new BigDecimal("1.2");
testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD);
resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS);
resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS);
Assert.assertEquals(
0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd

testBD = BigDecimal.valueOf(-1.2);
testBD = new BigDecimal("-1.2");
testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD);
resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS);
resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS);
Assert.assertEquals(
0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd

testBD =
BigDecimal.valueOf(
578960446186580977117854925043439539266.34992332820282019728792003956564819967);
testBD = new BigDecimal("99999999999999999999999999999.999999999");
testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD);
resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS);
resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS);
Assert.assertEquals(
0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd

testBD =
BigDecimal.valueOf(
-578960446186580977117854925043439539266.34992332820282019728792003956564819967);
testBD = new BigDecimal("-99999999999999999999999999999.999999999");
testBS = BigDecimalByteStringEncoder.encodeToNumericByteString(testBD);
resultBD = BigDecimalByteStringEncoder.decodeToBigDecimal(testBS);
resultBD = BigDecimalByteStringEncoder.decodeNumericByteString(testBS);
Assert.assertEquals(
0, resultBD.compareTo(testBD)); // ensure converted bd is equal to expected bd
}
Expand Down

0 comments on commit 20ff9e1

Please sign in to comment.