diff --git a/gui/src/main/java/io/bisq/gui/util/BSFormatter.java b/gui/src/main/java/io/bisq/gui/util/BSFormatter.java index 7636a36344..abc5662080 100644 --- a/gui/src/main/java/io/bisq/gui/util/BSFormatter.java +++ b/gui/src/main/java/io/bisq/gui/util/BSFormatter.java @@ -36,6 +36,7 @@ import org.bitcoinj.core.Coin; import org.bitcoinj.core.Monetary; import org.bitcoinj.utils.Fiat; import org.bitcoinj.utils.MonetaryFormat; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -251,6 +252,24 @@ public class BSFormatter { // Volume /////////////////////////////////////////////////////////////////////////////////////////// + public String formatVolume(Volume volume, Boolean decimalAligned) { + String formattedVolume = formatVolume(volume); + + if(decimalAligned) { + formattedVolume = fillUpPlacesWithEmptyStrings(formattedVolume, 5); + } + return formattedVolume; + } + + @NotNull + private String fillUpPlacesWithEmptyStrings(String formattedVolume, int maxNumberOfDigits) { + int numberOfPlacesToFill = maxNumberOfDigits - formattedVolume.split("\\.")[0].length(); + for (int i = 0; i < numberOfPlacesToFill; i++) { + formattedVolume = " " + formattedVolume; + } + return formattedVolume; + } + public String formatVolume(Volume volume) { return formatVolume(volume, fiatVolumeFormat, false); } @@ -301,7 +320,6 @@ public class BSFormatter { return Res.get("formatter.formatVolumeLabel", currencyCode, postFix); } - public String formatMinVolumeAndVolume(Offer offer) { return offer.isRange() ? formatVolume(offer.getMinVolume()) + " - " + formatVolume(offer.getVolume()) : formatVolume(offer.getVolume()); } @@ -341,6 +359,15 @@ public class BSFormatter { return formatPrice(price, fiatPriceFormat, false); } + public String formatPrice(Price price, Boolean decimalAligned) { + String formattedPrice = formatPrice(price); + + if(decimalAligned) { + formattedPrice = fillUpPlacesWithEmptyStrings(formattedPrice, 5); + } + return formattedPrice; + } + public String formatPriceWithCode(Price price) { Monetary monetary = price.getMonetary(); if (monetary instanceof Fiat) diff --git a/gui/src/test/java/io/bisq/common/monetary/PriceMaker.java b/gui/src/test/java/io/bisq/common/monetary/PriceMaker.java new file mode 100644 index 0000000000..6027dbcd1f --- /dev/null +++ b/gui/src/test/java/io/bisq/common/monetary/PriceMaker.java @@ -0,0 +1,23 @@ +package io.bisq.common.monetary; + +import com.natpryce.makeiteasy.Instantiator; +import com.natpryce.makeiteasy.Maker; +import com.natpryce.makeiteasy.Property; +import org.bitcoinj.utils.Fiat; + +import static com.natpryce.makeiteasy.MakeItEasy.a; + +public class PriceMaker { + + public static final Property currencyCode = new Property<>(); + public static final Property priceString = new Property<>(); + + public static final Instantiator FiatPrice = lookup -> + new Price(Fiat.parseFiat(lookup.valueOf(currencyCode, "USD"), lookup.valueOf(priceString, "100"))); + + public static final Instantiator AltcoinPrice = lookup -> + new Price(Altcoin.parseAltcoin(lookup.valueOf(currencyCode, "LTC"), lookup.valueOf(priceString, "100"))); + + public static final Maker usdPrice = a(FiatPrice); + public static final Maker ltcPrice = a(AltcoinPrice); +} diff --git a/gui/src/test/java/io/bisq/common/monetary/VolumeMaker.java b/gui/src/test/java/io/bisq/common/monetary/VolumeMaker.java new file mode 100644 index 0000000000..4e764f9d2f --- /dev/null +++ b/gui/src/test/java/io/bisq/common/monetary/VolumeMaker.java @@ -0,0 +1,23 @@ +package io.bisq.common.monetary; + +import com.natpryce.makeiteasy.Instantiator; +import com.natpryce.makeiteasy.Maker; +import com.natpryce.makeiteasy.Property; +import org.bitcoinj.utils.Fiat; + +import static com.natpryce.makeiteasy.MakeItEasy.a; + +public class VolumeMaker { + + public static final Property currencyCode = new Property<>(); + public static final Property volumeString = new Property<>(); + + public static final Instantiator FiatVolume = lookup -> + new Volume(Fiat.parseFiat(lookup.valueOf(currencyCode, "USD"), lookup.valueOf(volumeString,"100"))); + + public static final Instantiator AltcoinVolume = lookup -> + new Volume(Altcoin.parseAltcoin(lookup.valueOf(currencyCode, "LTC"), lookup.valueOf(volumeString, "100"))); + + public static final Maker usdVolume = a(FiatVolume); + public static final Maker ltcVolume = a(AltcoinVolume); +} diff --git a/gui/src/test/java/io/bisq/gui/util/BSFormatterTest.java b/gui/src/test/java/io/bisq/gui/util/BSFormatterTest.java index 56e430574b..416e335bba 100644 --- a/gui/src/test/java/io/bisq/gui/util/BSFormatterTest.java +++ b/gui/src/test/java/io/bisq/gui/util/BSFormatterTest.java @@ -31,13 +31,20 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.util.Locale; import java.util.concurrent.TimeUnit; +import static com.natpryce.makeiteasy.MakeItEasy.make; +import static com.natpryce.makeiteasy.MakeItEasy.with; +import static io.bisq.common.monetary.PriceMaker.priceString; +import static io.bisq.common.monetary.PriceMaker.usdPrice; +import static io.bisq.common.monetary.VolumeMaker.usdVolume; +import static io.bisq.common.monetary.VolumeMaker.volumeString; +import static org.bitcoinj.core.CoinMaker.oneBitcoin; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) -@PrepareForTest({Offer.class,OfferPayload.class}) +@PrepareForTest({Offer.class, OfferPayload.class}) public class BSFormatterTest { private BSFormatter formatter; @@ -79,6 +86,25 @@ public class BSFormatterTest { assertTrue(formatter.formatDurationAsWords(0).isEmpty()); } + @Test + public void testFormatPrice() { + assertEquals("100.0000", formatter.formatPrice(make(usdPrice))); + assertEquals(" 100.0000", formatter.formatPrice(make(usdPrice), true)); + assertEquals("7098.4700", formatter.formatPrice(make(usdPrice.but(with(priceString, "7098.4700"))))); + } + + @Test + public void testFormatCoin() { + assertEquals("1.00", formatter.formatCoin(oneBitcoin)); + } + + @Test + public void testFormatVolume() { + assertEquals(" 100.00", formatter.formatVolume(make(usdVolume), true)); + assertEquals("100.00", formatter.formatVolume(make(usdVolume))); + assertEquals("1774.62", formatter.formatVolume(make(usdVolume.but(with(volumeString, "1774.62"))))); + } + @Test public void testFormatSameVolume() { Offer offer = mock(Offer.class); diff --git a/gui/src/test/java/org/bitcoinj/core/CoinMaker.java b/gui/src/test/java/org/bitcoinj/core/CoinMaker.java new file mode 100644 index 0000000000..a3c56cc911 --- /dev/null +++ b/gui/src/test/java/org/bitcoinj/core/CoinMaker.java @@ -0,0 +1,19 @@ +package org.bitcoinj.core; + +import com.natpryce.makeiteasy.Instantiator; +import com.natpryce.makeiteasy.Property; + +import static com.natpryce.makeiteasy.MakeItEasy.a; +import static com.natpryce.makeiteasy.MakeItEasy.make; +import static org.bitcoinj.core.Coin.*; + +public class CoinMaker { + + + private static final Property satoshis = new Property<>(); + + public static final Instantiator Coin = lookup -> + valueOf(lookup.valueOf(satoshis, 100000000L)); + + public static final Coin oneBitcoin = make(a(Coin)); +}