From 8b9842934135c4f1a5898222f35436de6d756ebc Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Sun, 21 Jan 2018 12:06:38 -0500 Subject: [PATCH] Only add offers if it is not in list --- .../gui/main/offer/offerbook/OfferBook.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBook.java b/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBook.java index c4c8f9eae9..e35474d106 100644 --- a/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBook.java +++ b/gui/src/main/java/io/bisq/gui/main/offer/offerbook/OfferBook.java @@ -53,20 +53,31 @@ public class OfferBook { offerBookService.addOfferBookChangedListener(new OfferBookService.OfferBookChangedListener() { @Override public void onAdded(Offer offer) { - OfferBookListItem offerBookListItem = new OfferBookListItem(offer); - // We don't use the contains method as the equals method in Offer takes state and errorMessage into account. - // If we have an offer with same ID we remove it and add the new offer as it might have a changed state. - Optional candidateToRemove = offerBookListItems.stream() - .filter(item -> item.getOffer().getId().equals(offer.getId())) - .findAny(); - if (candidateToRemove.isPresent()) { - log.info("We had an old offer in the list with the same Offer ID. Might be that the state or errorMessage was different. " + - "old offerBookListItem={}, new offerBookListItem={}", candidateToRemove.get(), offerBookListItem); - offerBookListItems.remove(candidateToRemove.get()); - } + // We get onAdded called every time a new ProtectedStorageEntry is received. + // Mostly it is the same OfferPayload but the ProtectedStorageEntry is different. + // We filter here to only add new offers if the same offer (using equals) was not already added. + boolean hasSameOffer = offerBookListItems.stream() + .filter(item -> item.getOffer().equals(offer)) + .findAny() + .isPresent(); + if (!hasSameOffer) { + OfferBookListItem offerBookListItem = new OfferBookListItem(offer); + // We don't use the contains method as the equals method in Offer takes state and errorMessage into account. + // If we have an offer with same ID we remove it and add the new offer as it might have a changed state. + Optional candidateWithSameId = offerBookListItems.stream() + .filter(item -> item.getOffer().getId().equals(offer.getId())) + .findAny(); + if (candidateWithSameId.isPresent()) { + log.warn("We had an old offer in the list with the same Offer ID. Might be that the state or errorMessage was different. " + + "old offerBookListItem={}, new offerBookListItem={}", candidateWithSameId.get(), offerBookListItem); + offerBookListItems.remove(candidateWithSameId.get()); + } - offerBookListItems.add(offerBookListItem); - Log.logIfStressTests("OfferPayload added: No. of offers = " + offerBookListItems.size()); + offerBookListItems.add(offerBookListItem); + Log.logIfStressTests("OfferPayload added: No. of offers = " + offerBookListItems.size()); + }else{ + log.debug("We have the exact same offer already in our list and ignore the onAdded call. ID={}", offer.getId()); + } } @Override