From cae390ea75f66f55b31a9846dc734553d04846e5 Mon Sep 17 00:00:00 2001 From: Christoph Atteneder Date: Thu, 3 May 2018 14:54:05 +0200 Subject: [PATCH 1/2] Use percentage value for sorting not absolut price spread --- .../java/bisq/desktop/main/market/spread/SpreadItem.java | 5 ++++- .../java/bisq/desktop/main/market/spread/SpreadView.java | 8 +++----- .../bisq/desktop/main/market/spread/SpreadViewModel.java | 7 ++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/bisq/desktop/main/market/spread/SpreadItem.java b/src/main/java/bisq/desktop/main/market/spread/SpreadItem.java index 28f60559358..09ef65f46a0 100644 --- a/src/main/java/bisq/desktop/main/market/spread/SpreadItem.java +++ b/src/main/java/bisq/desktop/main/market/spread/SpreadItem.java @@ -35,15 +35,18 @@ public class SpreadItem { @Nullable public final Price priceSpread; public final String percentage; + public final double percentageValue; public final Coin totalAmount; - public SpreadItem(String currencyCode, int numberOfBuyOffers, int numberOfSellOffers, int numberOfOffers, @Nullable Price priceSpread, String percentage, Coin totalAmount) { + public SpreadItem(String currencyCode, int numberOfBuyOffers, int numberOfSellOffers, int numberOfOffers, + @Nullable Price priceSpread, String percentage, double percentageValue, Coin totalAmount) { this.currencyCode = currencyCode; this.numberOfBuyOffers = numberOfBuyOffers; this.numberOfSellOffers = numberOfSellOffers; this.numberOfOffers = numberOfOffers; this.priceSpread = priceSpread; this.percentage = percentage; + this.percentageValue = percentageValue; this.totalAmount = totalAmount; } } diff --git a/src/main/java/bisq/desktop/main/market/spread/SpreadView.java b/src/main/java/bisq/desktop/main/market/spread/SpreadView.java index 4fcdf629843..54923d55459 100644 --- a/src/main/java/bisq/desktop/main/market/spread/SpreadView.java +++ b/src/main/java/bisq/desktop/main/market/spread/SpreadView.java @@ -48,6 +48,8 @@ import javafx.util.Callback; +import java.util.Comparator; + @FxmlView public class SpreadView extends ActivatableViewAndModel { private final BSFormatter formatter; @@ -98,11 +100,7 @@ public void initialize() { numberOfBuyOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfBuyOffers).compareTo(o2.numberOfBuyOffers)); numberOfSellOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfSellOffers).compareTo(o2.numberOfSellOffers)); totalAmountColumn.setComparator((o1, o2) -> o1.totalAmount.compareTo(o2.totalAmount)); - spreadColumn.setComparator((o1, o2) -> { - Long spreadO1 = o1.priceSpread != null ? o1.priceSpread.getValue() : 0; - Long spreadO2 = o2.priceSpread != null ? o2.priceSpread.getValue() : 0; - return spreadO1.compareTo(spreadO2); - }); + spreadColumn.setComparator(Comparator.comparingDouble(o -> o.percentageValue)); numberOfOffersColumn.setSortType(TableColumn.SortType.DESCENDING); tableView.getSortOrder().add(numberOfOffersColumn); diff --git a/src/main/java/bisq/desktop/main/market/spread/SpreadViewModel.java b/src/main/java/bisq/desktop/main/market/spread/SpreadViewModel.java index 547c3a7a7db..932ed8c99ce 100644 --- a/src/main/java/bisq/desktop/main/market/spread/SpreadViewModel.java +++ b/src/main/java/bisq/desktop/main/market/spread/SpreadViewModel.java @@ -154,6 +154,7 @@ private void update(ObservableList offerBookListItems) { Price spread = null; String percentage = ""; + double percentageValue = 0; Price bestSellOfferPrice = sellOffers.isEmpty() ? null : sellOffers.get(0).getPrice(); Price bestBuyOfferPrice = buyOffers.isEmpty() ? null : buyOffers.get(0).getPrice(); if (bestBuyOfferPrice != null && bestSellOfferPrice != null) { @@ -180,11 +181,11 @@ private void update(ObservableList offerBookListItems) { BigDecimal marketPriceAsBigDecimal = BigDecimal.valueOf(marketPriceAsDouble) .multiply(BigDecimal.valueOf(precision)); // We multiply with 10000 because we use precision of 2 at % (100.00%) - double result = BigDecimal.valueOf(spread.getValue()) + percentageValue = BigDecimal.valueOf(spread.getValue()) .multiply(BigDecimal.valueOf(10000)) .divide(marketPriceAsBigDecimal, RoundingMode.HALF_UP) .doubleValue() / 10000; - percentage = formatter.formatPercentagePrice(result); + percentage = formatter.formatPercentagePrice(percentageValue); } } catch (Throwable t) { try { @@ -210,7 +211,7 @@ private void update(ObservableList offerBookListItems) { totalAmount = Coin.valueOf(offers.stream().mapToLong(offer -> offer.getAmount().getValue()).sum()); spreadItems.add(new SpreadItem(currencyCode, buyOffers.size(), sellOffers.size(), - uniqueOffers.size(), spread, percentage, totalAmount)); + uniqueOffers.size(), spread, percentage, percentageValue, totalAmount)); } maxPlacesForAmount.set(formatAmount(totalAmount, false).length()); From 12662c578d2ab6b96ee651d0eb2730fa555d1e28 Mon Sep 17 00:00:00 2001 From: Christoph Atteneder Date: Thu, 3 May 2018 14:55:19 +0200 Subject: [PATCH 2/2] Use Comparator for better readability --- src/main/java/bisq/desktop/main/market/spread/SpreadView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/bisq/desktop/main/market/spread/SpreadView.java b/src/main/java/bisq/desktop/main/market/spread/SpreadView.java index 54923d55459..c49cd2da6a3 100644 --- a/src/main/java/bisq/desktop/main/market/spread/SpreadView.java +++ b/src/main/java/bisq/desktop/main/market/spread/SpreadView.java @@ -95,11 +95,11 @@ public void initialize() { tableView.getColumns().add(spreadColumn); tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); - currencyColumn.setComparator((o1, o2) -> CurrencyUtil.getNameByCode(o1.currencyCode).compareTo(CurrencyUtil.getNameByCode(o2.currencyCode))); + currencyColumn.setComparator(Comparator.comparing(o -> CurrencyUtil.getNameByCode(o.currencyCode))); numberOfOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfOffers).compareTo(o2.numberOfOffers)); numberOfBuyOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfBuyOffers).compareTo(o2.numberOfBuyOffers)); numberOfSellOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfSellOffers).compareTo(o2.numberOfSellOffers)); - totalAmountColumn.setComparator((o1, o2) -> o1.totalAmount.compareTo(o2.totalAmount)); + totalAmountColumn.setComparator(Comparator.comparing(o -> o.totalAmount)); spreadColumn.setComparator(Comparator.comparingDouble(o -> o.percentageValue)); numberOfOffersColumn.setSortType(TableColumn.SortType.DESCENDING);