Expanding the Converter class in order to be able to use it with Price instances

master
Nelson R. Perez 2017-01-10 14:22:47 -05:00
parent 1929e18e06
commit bd9cbc6880
1 changed files with 48 additions and 1 deletions

View File

@ -1,12 +1,14 @@
package de.bitsharesmunich.graphenej;
import java.math.BigDecimal;
import java.math.MathContext;
import com.google.common.primitives.UnsignedLong;
import de.bitsharesmunich.graphenej.errors.IncompleteAssetError;
import de.bitsharesmunich.graphenej.models.BucketObject;
/**
* Converter class used to translate the market information contained in a BucketObject instance.
* Generic converter class used to translate the market information contained in a BucketObject and/or Price instances.
*
* Created by nelson on 12/23/16.
*/
@ -24,6 +26,18 @@ public class Converter {
private Asset quote;
private BucketObject bucket;
/**
* Constructor meant to be used trying to perform a conversion and in possession of a Price object.
*/
public Converter(){}
/**
* Constructor meant to be used when trying to perform a conversion and in possession of
* a BucketObject, typically resulting from a 'get_market_history' API call.
* @param base
* @param quote
* @param bucket
*/
public Converter(Asset base, Asset quote, BucketObject bucket){
this.base = base;
this.quote = quote;
@ -109,4 +123,37 @@ public class Converter {
long convertedBaseValue = (long) (assetAmountValue * conversionRate * precisionFactor);
return convertedBaseValue;
}
/**
* Method used to obtain the conversion rate between two assets given in a Price instance as recovered by the
* 'get_limit_orders' API call.
*
* The same rules that apply for the {@link #getConversionRate(int bucketAttribute, int direction) getConversionRate}
* are valid for the 'direction' argument.
*
* @param price: The Price object instance
* @param direction: The direction from which to perform the conversion, can be only one of BASE_TO_QUOTE or
* QUOTE_TO_BASE.
* @return: A double representing the exchange rate.
*/
public double getConversionRate(Price price, int direction){
Asset base = price.base.getAsset();
Asset quote = price.quote.getAsset();
if(base.getPrecision() == -1 || quote.getPrecision() == -1){
throw new IncompleteAssetError("The given asset instance must provide precision information");
}
double conversionRate = 0;
double precisionFactor = 0.0;
MathContext mathContext = new MathContext(Math.max(base.getPrecision(), quote.getPrecision()));
BigDecimal baseValue = BigDecimal.valueOf(price.base.getAmount().longValue());
BigDecimal quoteValue = BigDecimal.valueOf(price.quote.getAmount().doubleValue());
if(direction == BASE_TO_QUOTE){
conversionRate = quoteValue.divide(baseValue, mathContext).doubleValue();
precisionFactor = Math.pow(10, base.getPrecision()) / Math.pow(10, quote.getPrecision());
}else{
conversionRate = baseValue.divide(quoteValue, mathContext).doubleValue();
precisionFactor = Math.pow(10, quote.getPrecision()) / Math.pow(10, base.getPrecision());
}
return conversionRate * precisionFactor;
}
}