Add display strings for price, amount and volume for offer and trade json files

This commit is contained in:
Manfred Karrer 2016-08-23 11:32:53 +02:00
parent b24934f29f
commit b84e21dc31
2 changed files with 113 additions and 1 deletions

View file

@ -1,12 +1,22 @@
package io.bitsquare.trade.offer; package io.bitsquare.trade.offer;
import io.bitsquare.common.util.MathUtils;
import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.payment.PaymentMethod; import io.bitsquare.payment.PaymentMethod;
import org.bitcoinj.core.Coin; import org.bitcoinj.core.Coin;
import org.bitcoinj.utils.ExchangeRate;
import org.bitcoinj.utils.Fiat; import org.bitcoinj.utils.Fiat;
import org.bitcoinj.utils.MonetaryFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date; import java.util.Date;
public class FlatOffer { public class FlatOffer {
private static final Logger log = LoggerFactory.getLogger(FlatOffer.class);
public final Offer.Direction direction; public final Offer.Direction direction;
public final String currencyCode; public final String currencyCode;
public final long minAmount; public final long minAmount;
@ -19,6 +29,12 @@ public class FlatOffer {
public final String id; public final String id;
public final String offerFeeTxID; public final String offerFeeTxID;
// Used in Json to provide same formatting/rounding for price
public String priceDisplayString = "";
public String amountDisplayString = "";
public String minAmountDisplayString = "";
public String volumeDisplayString = "";
public FlatOffer(Offer.Direction direction, public FlatOffer(Offer.Direction direction,
String currencyCode, String currencyCode,
Coin minAmount, Coin minAmount,
@ -42,5 +58,55 @@ public class FlatOffer {
this.marketPriceMargin = marketPriceMargin; this.marketPriceMargin = marketPriceMargin;
this.paymentMethod = paymentMethod.getId(); this.paymentMethod = paymentMethod.getId();
this.offerFeeTxID = offerFeeTxID; this.offerFeeTxID = offerFeeTxID;
setDisplayStrings();
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
try {
in.defaultReadObject();
setDisplayStrings();
} catch (Throwable t) {
log.warn("Cannot be deserialized." + t.getMessage());
}
}
private void setDisplayStrings() {
try {
MonetaryFormat fiatFormat = MonetaryFormat.FIAT.repeatOptionalDecimals(0, 0);
MonetaryFormat coinFormat = MonetaryFormat.BTC.minDecimals(2).repeatOptionalDecimals(1, 6);
final Fiat priceAsFiat = getPriceAsFiat();
if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
DecimalFormat decimalFormat = new DecimalFormat("#.#");
decimalFormat.setMaximumFractionDigits(8);
final double value = priceAsFiat.value != 0 ? 10000D / priceAsFiat.value : 0;
priceDisplayString = decimalFormat.format(MathUtils.roundDouble(value, 8)).replace(",", ".");
} else {
priceDisplayString = fiatFormat.noCode().format(priceAsFiat).toString();
}
amountDisplayString = coinFormat.noCode().format(getAmountAsCoin()).toString();
minAmountDisplayString = coinFormat.noCode().format(getMinAmountAsCoin()).toString();
volumeDisplayString = fiatFormat.noCode().format(getVolumeAsFiat()).toString();
} catch (Throwable t) {
log.error("Error at setDisplayStrings: " + t.getMessage());
}
}
private Fiat getPriceAsFiat() {
return Fiat.valueOf(currencyCode, price);
}
private Coin getAmountAsCoin() {
return Coin.valueOf(amount);
}
private Coin getMinAmountAsCoin() {
return Coin.valueOf(minAmount);
}
private Fiat getVolumeAsFiat() {
return new ExchangeRate(getPriceAsFiat()).coinToFiat(getAmountAsCoin());
} }
} }

View file

@ -4,6 +4,8 @@ import io.bitsquare.app.Capabilities;
import io.bitsquare.app.Version; import io.bitsquare.app.Version;
import io.bitsquare.common.crypto.PubKeyRing; import io.bitsquare.common.crypto.PubKeyRing;
import io.bitsquare.common.util.JsonExclude; import io.bitsquare.common.util.JsonExclude;
import io.bitsquare.common.util.MathUtils;
import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload; import io.bitsquare.p2p.storage.payload.CapabilityRequiringPayload;
import io.bitsquare.p2p.storage.payload.LazyProcessedStoragePayload; import io.bitsquare.p2p.storage.payload.LazyProcessedStoragePayload;
import io.bitsquare.p2p.storage.payload.PersistedStoragePayload; import io.bitsquare.p2p.storage.payload.PersistedStoragePayload;
@ -11,9 +13,14 @@ import io.bitsquare.trade.offer.Offer;
import org.bitcoinj.core.Coin; import org.bitcoinj.core.Coin;
import org.bitcoinj.utils.ExchangeRate; import org.bitcoinj.utils.ExchangeRate;
import org.bitcoinj.utils.Fiat; import org.bitcoinj.utils.Fiat;
import org.bitcoinj.utils.MonetaryFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import java.io.IOException;
import java.security.PublicKey; import java.security.PublicKey;
import java.text.DecimalFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -21,6 +28,8 @@ import java.util.concurrent.TimeUnit;
@Immutable @Immutable
public final class TradeStatistics implements LazyProcessedStoragePayload, CapabilityRequiringPayload, PersistedStoragePayload { public final class TradeStatistics implements LazyProcessedStoragePayload, CapabilityRequiringPayload, PersistedStoragePayload {
private static final Logger log = LoggerFactory.getLogger(TradeStatistics.class);
@JsonExclude @JsonExclude
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
@JsonExclude @JsonExclude
@ -42,6 +51,11 @@ public final class TradeStatistics implements LazyProcessedStoragePayload, Capab
@JsonExclude @JsonExclude
public final PubKeyRing pubKeyRing; public final PubKeyRing pubKeyRing;
// Used in Json to provide same formatting/rounding for price
public String tradePriceDisplayString = "";
public String tradeAmountDisplayString = "";
public String tradeVolumeDisplayString = "";
public TradeStatistics(Offer offer, Fiat tradePrice, Coin tradeAmount, Date tradeDate, String depositTxId, PubKeyRing pubKeyRing) { public TradeStatistics(Offer offer, Fiat tradePrice, Coin tradeAmount, Date tradeDate, String depositTxId, PubKeyRing pubKeyRing) {
this.direction = offer.getDirection(); this.direction = offer.getDirection();
this.currency = offer.getCurrencyCode(); this.currency = offer.getCurrencyCode();
@ -58,6 +72,38 @@ public final class TradeStatistics implements LazyProcessedStoragePayload, Capab
this.tradeDate = tradeDate.getTime(); this.tradeDate = tradeDate.getTime();
this.depositTxId = depositTxId; this.depositTxId = depositTxId;
this.pubKeyRing = pubKeyRing; this.pubKeyRing = pubKeyRing;
setDisplayStrings();
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
try {
in.defaultReadObject();
setDisplayStrings();
} catch (Throwable t) {
log.warn("Cannot be deserialized." + t.getMessage());
}
}
private void setDisplayStrings() {
try {
MonetaryFormat fiatFormat = MonetaryFormat.FIAT.repeatOptionalDecimals(0, 0);
MonetaryFormat coinFormat = MonetaryFormat.BTC.minDecimals(2).repeatOptionalDecimals(1, 6);
final Fiat tradePriceAsFiat = getTradePrice();
if (CurrencyUtil.isCryptoCurrency(currency)) {
DecimalFormat decimalFormat = new DecimalFormat("#.#");
decimalFormat.setMaximumFractionDigits(8);
final double value = tradePriceAsFiat.value != 0 ? 10000D / tradePriceAsFiat.value : 0;
tradePriceDisplayString = decimalFormat.format(MathUtils.roundDouble(value, 8)).replace(",", ".");
} else {
tradePriceDisplayString = fiatFormat.noCode().format(tradePriceAsFiat).toString();
}
tradeAmountDisplayString = coinFormat.noCode().format(getTradeAmount()).toString();
tradeVolumeDisplayString = fiatFormat.noCode().format(getTradeVolume()).toString();
} catch (Throwable t) {
log.error("Error at setDisplayStrings: " + t.getMessage());
}
} }
@Override @Override
@ -116,7 +162,7 @@ public final class TradeStatistics implements LazyProcessedStoragePayload, Capab
return false; return false;
else if ((direction == null && that.direction != null) || (direction != null && that.direction == null)) else if ((direction == null && that.direction != null) || (direction != null && that.direction == null))
return false; return false;
if (paymentMethod != null ? !paymentMethod.equals(that.paymentMethod) : that.paymentMethod != null) if (paymentMethod != null ? !paymentMethod.equals(that.paymentMethod) : that.paymentMethod != null)
return false; return false;
if (offerId != null ? !offerId.equals(that.offerId) : that.offerId != null) return false; if (offerId != null ? !offerId.equals(that.offerId) : that.offerId != null) return false;