From d9d097d01f26013de7d2e5c44fd30f9ecb59eab9 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Tue, 14 Apr 2020 16:11:18 +0200 Subject: [PATCH 1/4] Secondary sort order for market offer book --- .../offerbook/OfferBookChartViewModel.java | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java index c7c6bf76376..ba6f87c95fc 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java @@ -280,18 +280,7 @@ private void updateChartData() { .map(OfferBookListItem::getOffer) .filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && e.getDirection().equals(OfferPayload.Direction.BUY)) - .sorted((o1, o2) -> { - long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0; - long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0; - if (a != b) { - if (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) - return a > b ? 1 : -1; - else - return a < b ? 1 : -1; - } else { - return 0; - } - }) + .sorted(getComparator(false)) .collect(Collectors.toList()); final Optional highestBuyPriceOffer = allBuyOffers.stream() @@ -320,18 +309,7 @@ private void updateChartData() { .map(OfferBookListItem::getOffer) .filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && e.getDirection().equals(OfferPayload.Direction.SELL)) - .sorted((o1, o2) -> { - long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0; - long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0; - if (a != b) { - if (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) - return a < b ? 1 : -1; - else - return a > b ? 1 : -1; - } else { - return 0; - } - }) + .sorted(getComparator(true)) .collect(Collectors.toList()); final Optional highestSellPriceOffer = allSellOffers.stream() @@ -355,6 +333,26 @@ private void updateChartData() { buildChartAndTableEntries(allSellOffers, OfferPayload.Direction.SELL, sellData, topSellOfferList); } + private Comparator getComparator(boolean reversePrimarySortOrder) { + Comparator primary = Comparator.comparing(Offer::getPrice, (o1, o2) -> { + long a = o1 != null ? o1.getValue() : 0; + long b = o2 != null ? o2.getValue() : 0; + if (a != b) { + if (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) + return a > b ? 1 : -1; + else + return a < b ? 1 : -1; + } else { + return 0; + } + }); + + if (reversePrimarySortOrder) + primary = primary.reversed(); + + return primary.thenComparing(Offer::getAmount); + } + private void buildChartAndTableEntries(List sortedList, OfferPayload.Direction direction, List> data, From 95fdca52d0ec7b0c4ae12cebd3f687041bcaec4c Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Tue, 14 Apr 2020 16:31:06 +0200 Subject: [PATCH 2/4] Fixed possible NPEx This check hasn't been in place before. However, during testing, I encountered it once. Not sure why it can happen and how to react properly. NullPointerException is prohibited. Lets see if sorting issue pop up sometime. --- .../desktop/main/market/offerbook/OfferBookChartViewModel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java index ba6f87c95fc..d2a92f11905 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java @@ -338,7 +338,7 @@ private Comparator getComparator(boolean reversePrimarySortOrder) { long a = o1 != null ? o1.getValue() : 0; long b = o2 != null ? o2.getValue() : 0; if (a != b) { - if (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) + if (o1 != null && CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode())) return a > b ? 1 : -1; else return a < b ? 1 : -1; From 9b006797185956e3937aac8e1ee5405a27f1180e Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Mon, 4 May 2020 18:02:52 +0200 Subject: [PATCH 3/4] Add javadoc --- .../market/offerbook/OfferBookChartViewModel.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java index d2a92f11905..1466de7c73d 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java @@ -280,7 +280,7 @@ private void updateChartData() { .map(OfferBookListItem::getOffer) .filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && e.getDirection().equals(OfferPayload.Direction.BUY)) - .sorted(getComparator(false)) + .sorted(getComparatorWithSecondarySortOrder(false)) .collect(Collectors.toList()); final Optional highestBuyPriceOffer = allBuyOffers.stream() @@ -309,7 +309,7 @@ private void updateChartData() { .map(OfferBookListItem::getOffer) .filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && e.getDirection().equals(OfferPayload.Direction.SELL)) - .sorted(getComparator(true)) + .sorted(getComparatorWithSecondarySortOrder(true)) .collect(Collectors.toList()); final Optional highestSellPriceOffer = allSellOffers.stream() @@ -333,7 +333,14 @@ private void updateChartData() { buildChartAndTableEntries(allSellOffers, OfferPayload.Direction.SELL, sellData, topSellOfferList); } - private Comparator getComparator(boolean reversePrimarySortOrder) { + /** + * Returns a comparator to be used for Offers. Sorts primarily for price and + * secondarily for offer volume. + * + * @param reversePrimarySortOrder + * @return + */ + private Comparator getComparatorWithSecondarySortOrder(boolean reversePrimarySortOrder) { Comparator primary = Comparator.comparing(Offer::getPrice, (o1, o2) -> { long a = o1 != null ? o1.getValue() : 0; long b = o2 != null ? o2.getValue() : 0; From f3dc42e2ed1fc2f2560b541a18cd996a7d23b9ed Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Mon, 11 May 2020 16:09:54 +0200 Subject: [PATCH 4/4] Update desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java Co-authored-by: cd2357 <15956136+cd2357@users.noreply.github.com> --- .../main/market/offerbook/OfferBookChartViewModel.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java index 1466de7c73d..453eeab6f7f 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModel.java @@ -354,10 +354,18 @@ private Comparator getComparatorWithSecondarySortOrder(boolean reversePri } }); + // 1st comparator: sort by price + // - ascending, if showing offers to buyers + // - descending, if showing offers to sellers if (reversePrimarySortOrder) primary = primary.reversed(); - return primary.thenComparing(Offer::getAmount); + // 2nd comparator: sort by amount, in descending order + // The goal is to show the more attractive offers at the top + // Both buyers and sellers would prefer to see higher amounts first (for offers with the same price) + Comparator secondary = Comparator.comparing(Offer::getAmount, Comparator.reverseOrder()); + + return primary.thenComparing(secondary); } private void buildChartAndTableEntries(List sortedList,