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;
|
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 class Asset extends GrapheneObject {
|
||||||
public final static String TAG = "Asset";
|
public final static String TAG = "Asset";
|
||||||
|
@ -83,6 +83,22 @@ public class Asset extends GrapheneObject {
|
||||||
this.issuer = issuer;
|
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(){
|
public String getSymbol(){
|
||||||
return this.symbol;
|
return this.symbol;
|
||||||
}
|
}
|
||||||
|
@ -111,6 +127,14 @@ public class Asset extends GrapheneObject {
|
||||||
return description;
|
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){
|
public void setAssetOptions(AssetOptions options){
|
||||||
this.options = options;
|
this.options = options;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ import cy.agorise.graphenej.interfaces.ByteSerializable;
|
||||||
import cy.agorise.graphenej.interfaces.JsonSerializable;
|
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 {
|
public class AssetAmount implements ByteSerializable, JsonSerializable {
|
||||||
/**
|
/**
|
||||||
|
@ -37,11 +37,25 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
||||||
private UnsignedLong amount;
|
private UnsignedLong amount;
|
||||||
private Asset asset;
|
private Asset asset;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
* @param amount The amount
|
||||||
|
* @param asset The asset
|
||||||
|
*/
|
||||||
public AssetAmount(UnsignedLong amount, Asset asset){
|
public AssetAmount(UnsignedLong amount, Asset asset){
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
this.asset = asset;
|
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.
|
* Adds two asset amounts. They must refer to the same Asset type.
|
||||||
* @param other: The other AssetAmount to add to this.
|
* @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){
|
public AssetAmount multiplyBy(double factor, RoundingMode roundingMode){
|
||||||
BigDecimal originalAmount = new BigDecimal(amount.bigIntegerValue());
|
BigDecimal originalAmount = new BigDecimal(amount.bigIntegerValue());
|
||||||
BigDecimal decimalResult = originalAmount.multiply(new BigDecimal(factor));
|
BigDecimal decimalResult = originalAmount.multiply(new BigDecimal(factor));
|
||||||
this.amount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalResult.doubleValue(), roundingMode));
|
UnsignedLong resultingAmount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalResult.doubleValue(), roundingMode));
|
||||||
return this;
|
return new AssetAmount(resultingAmount, new Asset(asset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,11 +127,11 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
||||||
* @param divisor: The divisor
|
* @param divisor: The divisor
|
||||||
* @return: The same AssetAMount instance, but with the divided amount value
|
* @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 originalAmount = new BigDecimal(amount.bigIntegerValue());
|
||||||
BigDecimal decimalAmount = originalAmount.divide(new BigDecimal(divisor), 18, RoundingMode.HALF_UP);
|
BigDecimal decimalAmount = originalAmount.divide(new BigDecimal(divisor), 18, RoundingMode.HALF_UP);
|
||||||
this.amount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalAmount.doubleValue(), roundingMode));
|
UnsignedLong resultingAmount = UnsignedLong.valueOf(DoubleMath.roundToBigInteger(decimalAmount.doubleValue(), roundingMode));
|
||||||
return this;
|
return new AssetAmount(resultingAmount, new Asset(asset));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -127,8 +141,8 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
||||||
* @param divisor: The divisor
|
* @param divisor: The divisor
|
||||||
* @return: The same AssetAMount instance, but with the divided amount value
|
* @return: The same AssetAMount instance, but with the divided amount value
|
||||||
*/
|
*/
|
||||||
public AssetAmount dividedBy(double divisor){
|
public AssetAmount divideBy(double divisor){
|
||||||
return this.dividedBy(divisor, RoundingMode.HALF_DOWN);
|
return this.divideBy(divisor, RoundingMode.HALF_DOWN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmount(UnsignedLong amount){
|
public void setAmount(UnsignedLong amount){
|
||||||
|
@ -141,6 +155,10 @@ public class AssetAmount implements ByteSerializable, JsonSerializable {
|
||||||
|
|
||||||
public Asset getAsset(){ return this.asset; }
|
public Asset getAsset(){ return this.asset; }
|
||||||
|
|
||||||
|
public void setAsset(Asset asset){
|
||||||
|
this.asset = asset;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] toBytes() {
|
public byte[] toBytes() {
|
||||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
|
|
|
@ -6,6 +6,7 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Testing AssetAmount operations.
|
* Testing AssetAmount operations.
|
||||||
|
@ -39,6 +40,8 @@ public class AssetAmountTest {
|
||||||
AssetAmount max = new AssetAmount(UnsignedLong.valueOf(Long.MAX_VALUE), testAsset);
|
AssetAmount max = new AssetAmount(UnsignedLong.valueOf(Long.MAX_VALUE), testAsset);
|
||||||
AssetAmount overMaxLong = max.multiplyBy(1.5);
|
AssetAmount overMaxLong = max.multiplyBy(1.5);
|
||||||
assertEquals("13835058055282163712", overMaxLong.getAmount().toString(10));
|
assertEquals("13835058055282163712", overMaxLong.getAmount().toString(10));
|
||||||
|
|
||||||
|
assertNotSame("Making sure the result and original references point to different instances",result, large);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -51,5 +54,7 @@ public class AssetAmountTest {
|
||||||
AssetAmount max = new AssetAmount(UnsignedLong.valueOf(Long.MAX_VALUE), testAsset);
|
AssetAmount max = new AssetAmount(UnsignedLong.valueOf(Long.MAX_VALUE), testAsset);
|
||||||
AssetAmount overMaxLong = max.dividedBy(0.8);
|
AssetAmount overMaxLong = max.dividedBy(0.8);
|
||||||
assertEquals("11529215046068469760", overMaxLong.getAmount().toString());
|
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