Skip to content

Commit

Permalink
Use BigDecimal instead of Double for Assets (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
neutralleiter committed Jan 2, 2018
1 parent d5192f3 commit cd851c7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
23 changes: 16 additions & 7 deletions core/src/main/java/eu/bittrade/libs/steemj/base/models/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.InvalidParameterException;

import org.apache.commons.lang3.builder.ToStringBuilder;

Expand Down Expand Up @@ -42,7 +44,7 @@ public class Asset implements ByteTransformable {
* {@link eu.bittrade.libs.steemj.enums.AssetSymbolType
* AssetSymbolType}.
*/
public Asset(double amount, AssetSymbolType symbol) {
public Asset(BigDecimal amount, AssetSymbolType symbol) {
this.setSymbol(symbol);
this.setAmount(amount);
}
Expand Down Expand Up @@ -106,8 +108,14 @@ public void setAmount(long amount) {
* @param amount
* The amount.
*/
public void setAmount(double amount) {
this.amount = (long) (amount * Math.pow(10.0, this.getPrecision()));
public void setAmount(BigDecimal amount) {
if (amount.scale() > this.getPrecision()) {
throw new InvalidParameterException("The provided 'amount' has a 'scale' of " + amount.scale()
+ ", but needs to have a 'scale' of " + this.getPrecision() + " when " + this.getSymbol().name()
+ " is used as a AssetSymbolType.");
}

this.amount = amount.multiply(BigDecimal.valueOf(Math.pow(10, this.getPrecision()))).longValue();
}

/**
Expand All @@ -129,12 +137,13 @@ public void setSymbol(AssetSymbolType symbol) {
}

/**
* Transform this asset into its double representation.
* Transform this asset into its {@link BigDecimal} representation.
*
* @return The value of this asset in its double representation.
* @return The value of this asset in its {@link BigDecimal} representation.
*/
public Double toReal() {
return Double.valueOf(this.amount / Math.pow(10.0, this.getPrecision()));
public BigDecimal toReal() {
BigDecimal transformedValue = new BigDecimal(this.getAmount());
return transformedValue.setScale(this.getPrecision());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.bittrade.libs.steemj.base.models.deserializer;

import java.io.IOException;
import java.math.BigDecimal;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
Expand Down Expand Up @@ -29,7 +30,7 @@ public Asset deserialize(JsonParser jasonParser, DeserializationContext deserial
* value for the byte representation so we transform the amount
* into a long value here.
*/
return new Asset(Double.valueOf(assetFields[0]), AssetSymbolType.valueOf(assetFields[1]));
return new Asset(new BigDecimal(assetFields[0]), AssetSymbolType.valueOf(assetFields[1]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.fail;

import java.math.BigDecimal;
import java.security.InvalidParameterException;

import org.junit.Test;

Expand Down Expand Up @@ -145,11 +149,29 @@ public void testTstdAssetToByteArray() throws Exception {
public void testAssetEqualsMethod() {
Asset asset = new Asset(115, AssetSymbolType.SBD);

Asset sameAsset = new Asset(0.115, AssetSymbolType.SBD);
Asset sameAsset = new Asset(new BigDecimal("0.115"), AssetSymbolType.SBD);

Asset differentAsset = new Asset(100, AssetSymbolType.STEEM);

assertThat(asset.equals(sameAsset), equalTo(true));
assertThat(sameAsset.equals(differentAsset), equalTo(false));
}

/**
* Test the {@link eu.bittrade.libs.steemj.base.models.Asset} method for the
* VESTS asset type.
*/
@Test
public void testAssetBigDecimalConstructor() {
new Asset(new BigDecimal("0.115"), AssetSymbolType.SBD);
new Asset(new BigDecimal("200.11"), AssetSymbolType.STEEM);
new Asset(new BigDecimal("2500.145111"), AssetSymbolType.VESTS);

try {
new Asset(new BigDecimal("0.1151"), AssetSymbolType.SBD);
fail();
} catch (InvalidParameterException e) {
// Expected.
}
}
}

0 comments on commit cd851c7

Please sign in to comment.