From aa35925718acc492112bcb4bdf5ecea423380705 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Thu, 6 Apr 2017 12:22:57 -0500 Subject: [PATCH] Added multiplication and division methods for the AssetAmount class --- .../graphenej/AssetAmount.java | 61 +++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java index 76abc69..cd0ea49 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/AssetAmount.java @@ -1,17 +1,27 @@ package de.bitsharesmunich.graphenej; +import com.google.common.math.DoubleMath; import com.google.common.primitives.Bytes; import com.google.common.primitives.UnsignedLong; -import com.google.gson.*; -import de.bitsharesmunich.graphenej.errors.IncompatibleOperation; -import de.bitsharesmunich.graphenej.interfaces.ByteSerializable; -import de.bitsharesmunich.graphenej.interfaces.JsonSerializable; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; import java.io.ByteArrayOutputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import java.lang.reflect.Type; +import java.math.RoundingMode; + +import de.bitsharesmunich.graphenej.errors.IncompatibleOperation; +import de.bitsharesmunich.graphenej.interfaces.ByteSerializable; +import de.bitsharesmunich.graphenej.interfaces.JsonSerializable; /** * Created by nelson on 11/7/16. @@ -73,6 +83,49 @@ public class AssetAmount implements ByteSerializable, JsonSerializable { return new AssetAmount(result, asset); } + /** + * Multiplies the current amount by a factor provided as the first parameter. The second parameter + * specifies the rounding method to be used. + * @param factor: The multiplying factor + * @param roundingMode: The rounding mode as an instance of the {@link RoundingMode} class + * @return The same AssetAmount instance, but with the changed amount value. + */ + public AssetAmount multiplyBy(double factor, RoundingMode roundingMode){ + this.amount = UnsignedLong.valueOf(DoubleMath.roundToLong(this.amount.longValue() * factor, roundingMode)); + return this; + } + + /** + * Multiplies the current amount by a factor, using the {@link RoundingMode#HALF_DOWN} constant. + * @param factor: The multiplying factor + * @return The same AssetAmount instance, but with the changed amount value. + */ + public AssetAmount multiplyBy(double factor){ + return this.multiplyBy(factor, RoundingMode.HALF_DOWN); + } + + /** + * Divides the current amount by a divisor provided as the first parameter. The second parameter + * specifies the rounding method to be used. + * @param divisor: The divisor + * @return: The same AssetAMount instance, but with the divided amount value + */ + public AssetAmount dividedBy(double divisor, RoundingMode roundingMode){ + this.amount = UnsignedLong.valueOf(DoubleMath.roundToLong(this.amount.longValue() / divisor, roundingMode)); + return this; + } + + + /** + * Divides the current amount by a divisor provided as the first parameter, using + * the {@link RoundingMode#HALF_DOWN} constant + * @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 void setAmount(UnsignedLong amount){ this.amount = amount; }