Introducing some copy constructors and making sure #multiplyBy and #divideBy return different instances
This commit is contained in:
parent
9a1808d127
commit
fb80fe3c02
3 changed files with 56 additions and 9 deletions
|
@ -10,7 +10,7 @@ import com.google.gson.JsonParseException;
|
|||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Created by nelson on 11/9/16.
|
||||
* Class used to represent a specific asset on the Graphene platform
|
||||
*/
|
||||
public class Asset extends GrapheneObject {
|
||||
public final static String TAG = "Asset";
|
||||
|
@ -83,6 +83,22 @@ public class Asset extends GrapheneObject {
|
|||
this.issuer = issuer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
* @param asset Another asset instance
|
||||
*/
|
||||
public Asset(Asset asset){
|
||||
super(asset.getObjectId());
|
||||
this.symbol = asset.getSymbol();
|
||||
this.precision = asset.getPrecision();
|
||||
this.issuer = asset.getIssuer();
|
||||
this.description = asset.getDescription();
|
||||
this.dynamic_asset_data_id = asset.getDynamicAssetDataId();
|
||||
this.options = asset.getAssetOptions();
|
||||
this.bitasset_data_id = asset.getBitassetId();
|
||||
this.mAssetType = asset.getAssetType();
|
||||
}
|
||||
|
||||
public String getSymbol(){
|
||||
return this.symbol;
|
||||
}
|
||||
|
@ -111,6 +127,14 @@ public class Asset extends GrapheneObject {
|
|||
return description;
|
||||
}
|
||||
|
||||
public String getDynamicAssetDataId() {
|
||||
return dynamic_asset_data_id;
|
||||
}
|
||||
|
||||
public void setDynamicAssetDataId(String dynamic_asset_data_id) {
|
||||
this.dynamic_asset_data_id = dynamic_asset_data_id;
|
||||
}
|
||||
|
||||
public void setAssetOptions(AssetOptions options){
|
||||
this.options = options;
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ import cy.agorise.graphenej.interfaces.ByteSerializable;
|
|||
import cy.agorise.graphenej.interfaces.JsonSerializable;
|
||||
|
||||
/**
|
||||
* Created by nelson on 11/7/16.
|
||||
* Class used to represent a specific amount of a certain asset
|
||||
*/
|
||||
public class AssetAmount implements ByteSerializable, JsonSerializable {
|
||||
/**
|
||||
|
@ -37,11 +37,25 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
|||
private UnsignedLong amount;
|
||||
private Asset asset;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
* @param amount The amount
|
||||
* @param asset The asset
|
||||
*/
|
||||
public AssetAmount(UnsignedLong amount, Asset asset){
|
||||
this.amount = amount;
|
||||
this.asset = asset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
* @param assetAmount The other instance
|
||||
*/
|
||||
public AssetAmount(AssetAmount assetAmount){
|
||||
this.amount = UnsignedLong.valueOf(assetAmount.getAmount().toString());
|
||||
this.asset = new Asset(assetAmount.getAsset());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two asset amounts. They must refer to the same Asset type.
|
||||
* @param other: The other AssetAmount to add to this.
|
||||
|
@ -94,8 +108,8 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
|||
public AssetAmount multiplyBy(double factor, RoundingMode roundingMode){
|
||||
BigDecimal originalAmount = new BigDecimal(amount.bigIntegerValue());
|
||||
BigDecimal decimalResult = originalAmount.multiply(new BigDecimal(factor));
|
||||
this.amount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalResult.doubleValue(), roundingMode));
|
||||
return this;
|
||||
UnsignedLong resultingAmount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalResult.doubleValue(), roundingMode));
|
||||
return new AssetAmount(resultingAmount, new Asset(asset));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,11 +127,11 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
|||
* @param divisor: The divisor
|
||||
* @return: The same AssetAMount instance, but with the divided amount value
|
||||
*/
|
||||
public AssetAmount dividedBy(double divisor, RoundingMode roundingMode){
|
||||
public AssetAmount divideBy(double divisor, RoundingMode roundingMode){
|
||||
BigDecimal originalAmount = new BigDecimal(amount.bigIntegerValue());
|
||||
BigDecimal decimalAmount = originalAmount.divide(new BigDecimal(divisor), 18, RoundingMode.HALF_UP);
|
||||
this.amount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalAmount.doubleValue(), roundingMode));
|
||||
return this;
|
||||
UnsignedLong resultingAmount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalAmount.doubleValue(), roundingMode));
|
||||
return new AssetAmount(resultingAmount, new Asset(asset));
|
||||
}
|
||||
|
||||
|
||||
|
@ -127,8 +141,8 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
|||
* @param divisor: The divisor
|
||||
* @return: The same AssetAMount instance, but with the divided amount value
|
||||
*/
|
||||
public AssetAmount dividedBy(double divisor){
|
||||
return this.dividedBy(divisor, RoundingMode.HALF_DOWN);
|
||||
public AssetAmount divideBy(double divisor){
|
||||
return this.divideBy(divisor, RoundingMode.HALF_DOWN);
|
||||
}
|
||||
|
||||
public void setAmount(UnsignedLong amount){
|
||||
|
@ -141,6 +155,10 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
|||
|
||||
public Asset getAsset(){ return this.asset; }
|
||||
|
||||
public void setAsset(Asset asset){
|
||||
this.asset = asset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
/**
|
||||
* Testing AssetAmount operations.
|
||||
|
@ -39,6 +40,8 @@ public class AssetAmountTest {
|
|||
AssetAmount max = new AssetAmount(UnsignedLong.valueOf(Long.MAX_VALUE), testAsset);
|
||||
AssetAmount overMaxLong = max.multiplyBy(1.5);
|
||||
assertEquals("13835058055282163712", overMaxLong.getAmount().toString(10));
|
||||
|
||||
assertNotSame("Making sure the result and original references point to different instances",result, large);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -51,5 +54,7 @@ public class AssetAmountTest {
|
|||
AssetAmount max = new AssetAmount(UnsignedLong.valueOf(Long.MAX_VALUE), testAsset);
|
||||
AssetAmount overMaxLong = max.dividedBy(0.8);
|
||||
assertEquals("11529215046068469760", overMaxLong.getAmount().toString());
|
||||
|
||||
assertNotSame("Making sure the result and original references point to different instances",result, large);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue