From 24889c87d700b880dabc5d9ad249214a581d6cc2 Mon Sep 17 00:00:00 2001 From: Steven Barclay Date: Sun, 22 Dec 2019 23:48:57 +0000 Subject: [PATCH 1/2] Remove unused fields passed as constructor params from various classes These are mostly injected objects that are now redundant, such as some CoinFormatter and Preferences fields. Also do some additional minor tidying of TradesChartsViewModel. --- .../java/bisq/core/app/WalletAppSetup.java | 11 +---- .../votereveal/VoteRevealService.java | 9 +--- .../alerts/market/MarketAlerts.java | 8 +--- .../components/paymentmethods/AssetsForm.java | 11 ++--- .../main/java/bisq/desktop/main/MainView.java | 10 +--- .../altcoinaccounts/AltCoinAccountsView.java | 2 +- .../ManageMarketAlertsWindow.java | 24 ++++------ .../MobileNotificationsView.java | 11 ++--- .../bisq/desktop/main/dao/news/NewsView.java | 6 +-- .../funds/transactions/TransactionsView.java | 6 --- .../offerbook/OfferBookChartViewModel.java | 11 +---- .../market/trades/TradesChartsViewModel.java | 47 ++++++------------- .../desktop/main/offer/MutableOfferView.java | 4 -- .../offer/createoffer/CreateOfferView.java | 11 +++-- .../offer/takeoffer/TakeOfferViewModel.java | 7 +-- .../closedtrades/ClosedTradesView.java | 8 +--- .../portfolio/editoffer/EditOfferView.java | 10 ++-- .../presentation/MarketPricePresentation.java | 15 ++---- .../settings/network/NetworkSettingsView.java | 11 ++--- .../settings/network/P2pNetworkListItem.java | 5 +- .../settings/preferences/PreferencesView.java | 4 -- .../OfferBookChartViewModelTest.java | 20 ++++---- .../trades/TradesChartsViewModelTest.java | 9 ++-- 23 files changed, 78 insertions(+), 182 deletions(-) diff --git a/core/src/main/java/bisq/core/app/WalletAppSetup.java b/core/src/main/java/bisq/core/app/WalletAppSetup.java index 229d5626d23..a0df3d194b3 100644 --- a/core/src/main/java/bisq/core/app/WalletAppSetup.java +++ b/core/src/main/java/bisq/core/app/WalletAppSetup.java @@ -23,14 +23,12 @@ import bisq.core.locale.Res; import bisq.core.user.Preferences; import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import org.bitcoinj.core.VersionMessage; import org.bitcoinj.store.BlockStoreException; import org.bitcoinj.store.ChainFileLockedException; import javax.inject.Inject; -import javax.inject.Named; import javax.inject.Singleton; import org.fxmisc.easybind.EasyBind; @@ -60,7 +58,6 @@ public class WalletAppSetup { private final WalletsSetup walletsSetup; private final BisqEnvironment bisqEnvironment; private final Preferences preferences; - private final CoinFormatter formatter; @SuppressWarnings("FieldCanBeLocal") private MonadicBinding btcInfoBinding; @@ -84,13 +81,11 @@ public class WalletAppSetup { public WalletAppSetup(WalletsManager walletsManager, WalletsSetup walletsSetup, BisqEnvironment bisqEnvironment, - Preferences preferences, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter) { + Preferences preferences) { this.walletsManager = walletsManager; this.walletsSetup = walletsSetup; this.bisqEnvironment = bisqEnvironment; this.preferences = preferences; - this.formatter = formatter; this.useTorForBTC.set(preferences.getUseTorForBitcoinJ()); } @@ -156,9 +151,7 @@ void init(@Nullable Consumer chainFileLockedExceptionHandler, return result; }); - btcInfoBinding.subscribe((observable, oldValue, newValue) -> { - getBtcInfo().set(newValue); - }); + btcInfoBinding.subscribe((observable, oldValue, newValue) -> getBtcInfo().set(newValue)); walletsSetup.initialize(null, () -> { diff --git a/core/src/main/java/bisq/core/dao/governance/votereveal/VoteRevealService.java b/core/src/main/java/bisq/core/dao/governance/votereveal/VoteRevealService.java index 8ccc27e36ee..e7b1706011f 100644 --- a/core/src/main/java/bisq/core/dao/governance/votereveal/VoteRevealService.java +++ b/core/src/main/java/bisq/core/dao/governance/votereveal/VoteRevealService.java @@ -38,8 +38,6 @@ import bisq.core.dao.state.model.blockchain.TxType; import bisq.core.dao.state.model.governance.DaoPhase; -import bisq.network.p2p.P2PService; - import bisq.common.util.Utilities; import org.bitcoinj.core.InsufficientMoneyException; @@ -81,7 +79,6 @@ public interface VoteRevealTxPublishedListener { private final MyVoteListService myVoteListService; private final BsqWalletService bsqWalletService; private final BtcWalletService btcWalletService; - private final P2PService p2PService; private final WalletsManager walletsManager; @Getter @@ -99,7 +96,6 @@ public VoteRevealService(DaoStateService daoStateService, MyVoteListService myVoteListService, BsqWalletService bsqWalletService, BtcWalletService btcWalletService, - P2PService p2PService, WalletsManager walletsManager) { this.daoStateService = daoStateService; this.blindVoteListService = blindVoteListService; @@ -107,7 +103,6 @@ public VoteRevealService(DaoStateService daoStateService, this.myVoteListService = myVoteListService; this.bsqWalletService = bsqWalletService; this.btcWalletService = btcWalletService; - this.p2PService = p2PService; this.walletsManager = walletsManager; } @@ -243,7 +238,7 @@ private void revealVote(MyVote myVote, boolean isInVoteRevealPhase) { publishTx(voteRevealTx); // We don't want to wait for a successful broadcast to avoid issues if the broadcast succeeds delayed or at - // next startup but the tx was actually broadcasted. + // next startup but the tx was actually broadcast. myVoteListService.applyRevealTxId(myVote, voteRevealTx.getHashAsString()); } catch (IOException | WalletException | TransactionVerificationException | InsufficientMoneyException e) { @@ -258,7 +253,7 @@ private void publishTx(Transaction voteRevealTx) { walletsManager.publishAndCommitBsqTx(voteRevealTx, TxType.VOTE_REVEAL, new TxBroadcaster.Callback() { @Override public void onSuccess(Transaction transaction) { - log.info("voteRevealTx successfully broadcasted."); + log.info("voteRevealTx successfully broadcast."); voteRevealTxPublishedListeners.forEach(l -> l.onVoteRevealTxPublished(transaction.getHashAsString())); } diff --git a/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java b/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java index eef24d1b660..ee2d092ac83 100644 --- a/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java +++ b/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java @@ -31,7 +31,6 @@ import bisq.core.provider.price.PriceFeedService; import bisq.core.user.User; import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.common.crypto.KeyRing; import bisq.common.util.MathUtils; @@ -39,7 +38,6 @@ import org.bitcoinj.utils.Fiat; import javax.inject.Inject; -import javax.inject.Named; import javax.inject.Singleton; import java.util.List; @@ -55,17 +53,15 @@ public class MarketAlerts { private final User user; private final PriceFeedService priceFeedService; private final KeyRing keyRing; - private final CoinFormatter formatter; @Inject - public MarketAlerts(OfferBookService offerBookService, MobileNotificationService mobileNotificationService, - User user, PriceFeedService priceFeedService, KeyRing keyRing, @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter) { + private MarketAlerts(OfferBookService offerBookService, MobileNotificationService mobileNotificationService, + User user, PriceFeedService priceFeedService, KeyRing keyRing) { this.offerBookService = offerBookService; this.mobileNotificationService = mobileNotificationService; this.user = user; this.priceFeedService = priceFeedService; this.keyRing = keyRing; - this.formatter = formatter; } diff --git a/desktop/src/main/java/bisq/desktop/components/paymentmethods/AssetsForm.java b/desktop/src/main/java/bisq/desktop/components/paymentmethods/AssetsForm.java index 210c22bf3db..d34d649f711 100644 --- a/desktop/src/main/java/bisq/desktop/components/paymentmethods/AssetsForm.java +++ b/desktop/src/main/java/bisq/desktop/components/paymentmethods/AssetsForm.java @@ -35,7 +35,6 @@ import bisq.core.payment.payload.AssetsAccountPayload; import bisq.core.payment.payload.PaymentAccountPayload; import bisq.core.payment.validation.AltCoinAddressValidator; -import bisq.core.user.Preferences; import bisq.core.util.coin.CoinFormatter; import bisq.core.util.validation.InputValidator; @@ -65,7 +64,6 @@ public class AssetsForm extends PaymentMethodForm { private final AltCoinAddressValidator altCoinAddressValidator; private final AssetService assetService; private final FilterManager filterManager; - private final Preferences preferences; private InputTextField addressInputTextField; private CheckBox tradeInstantCheckBox; @@ -88,14 +86,12 @@ public AssetsForm(PaymentAccount paymentAccount, int gridRow, CoinFormatter formatter, AssetService assetService, - FilterManager filterManager, - Preferences preferences) { + FilterManager filterManager) { super(paymentAccount, accountAgeWitnessService, inputValidator, gridPane, gridRow, formatter); this.assetAccount = (AssetAccount) paymentAccount; this.altCoinAddressValidator = altCoinAddressValidator; this.assetService = assetService; this.filterManager = filterManager; - this.preferences = preferences; tradeInstant = paymentAccount instanceof InstantCryptoCurrencyAccount; } @@ -207,9 +203,8 @@ protected void addTradeCurrencyComboBox() { currencyComboBox.setPromptText(Res.get("payment.select.altcoin")); currencyComboBox.setButtonCell(getComboBoxButtonCell(Res.get("payment.select.altcoin"), currencyComboBox)); - currencyComboBox.getEditor().focusedProperty().addListener(observable -> { - currencyComboBox.setPromptText(""); - }); + currencyComboBox.getEditor().focusedProperty().addListener(observable -> + currencyComboBox.setPromptText("")); ((AutocompleteComboBox) currencyComboBox).setAutocompleteItems( CurrencyUtil.getActiveSortedCryptoCurrencies(assetService, filterManager)); diff --git a/desktop/src/main/java/bisq/desktop/main/MainView.java b/desktop/src/main/java/bisq/desktop/main/MainView.java index 2fddde265b1..061ded5025b 100644 --- a/desktop/src/main/java/bisq/desktop/main/MainView.java +++ b/desktop/src/main/java/bisq/desktop/main/MainView.java @@ -47,8 +47,6 @@ import bisq.core.locale.GlobalSettings; import bisq.core.locale.LanguageUtil; import bisq.core.locale.Res; -import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.common.Timer; import bisq.common.UserThread; @@ -57,7 +55,6 @@ import bisq.common.util.Utilities; import javax.inject.Inject; -import javax.inject.Named; import com.jfoenix.controls.JFXBadge; import com.jfoenix.controls.JFXComboBox; @@ -146,7 +143,6 @@ public static void removeEffect() { private final ViewLoader viewLoader; private final Navigation navigation; - private final CoinFormatter formatter; private final ToggleGroup navButtons = new ToggleGroup(); private ChangeListener walletServiceErrorMsgListener; @@ -166,12 +162,10 @@ public MainView(MainViewModel model, CachingViewLoader viewLoader, Navigation navigation, Transitions transitions, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter, DaoStateMonitoringService daoStateMonitoringService) { super(model); this.viewLoader = viewLoader; this.navigation = navigation; - this.formatter = formatter; MainView.transitions = transitions; this.daoStateMonitoringService = daoStateMonitoringService; } @@ -405,9 +399,7 @@ protected Tooltip computeValue() { navigation.navigateToPreviousVisitedView(); - transitions.fadeOutAndRemove(splashScreen, 1500, actionEvent -> { - disposeSplashScreen(); - }); + transitions.fadeOutAndRemove(splashScreen, 1500, actionEvent -> disposeSplashScreen()); } }); diff --git a/desktop/src/main/java/bisq/desktop/main/account/content/altcoinaccounts/AltCoinAccountsView.java b/desktop/src/main/java/bisq/desktop/main/account/content/altcoinaccounts/AltCoinAccountsView.java index 7df3c964b43..fba57a507b0 100644 --- a/desktop/src/main/java/bisq/desktop/main/account/content/altcoinaccounts/AltCoinAccountsView.java +++ b/desktop/src/main/java/bisq/desktop/main/account/content/altcoinaccounts/AltCoinAccountsView.java @@ -241,7 +241,7 @@ private PaymentMethodForm getPaymentMethodForm(PaymentMethod paymentMethod) { private PaymentMethodForm getPaymentMethodForm(PaymentAccount paymentAccount) { return new AssetsForm(paymentAccount, accountAgeWitnessService, altCoinAddressValidator, - inputValidator, root, gridRow, formatter, assetService, filterManager, preferences); + inputValidator, root, gridRow, formatter, assetService, filterManager); } private void removeNewAccountForm() { diff --git a/desktop/src/main/java/bisq/desktop/main/account/content/notifications/ManageMarketAlertsWindow.java b/desktop/src/main/java/bisq/desktop/main/account/content/notifications/ManageMarketAlertsWindow.java index f6f2bba8c15..75493296d84 100644 --- a/desktop/src/main/java/bisq/desktop/main/account/content/notifications/ManageMarketAlertsWindow.java +++ b/desktop/src/main/java/bisq/desktop/main/account/content/notifications/ManageMarketAlertsWindow.java @@ -27,7 +27,6 @@ import bisq.core.notifications.alerts.market.MarketAlertFilter; import bisq.core.notifications.alerts.market.MarketAlerts; import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.common.UserThread; @@ -54,11 +53,9 @@ public class ManageMarketAlertsWindow extends Overlay { private final MarketAlerts marketAlerts; - private final CoinFormatter formatter; - ManageMarketAlertsWindow(MarketAlerts marketAlerts, CoinFormatter formatter) { + ManageMarketAlertsWindow(MarketAlerts marketAlerts) { this.marketAlerts = marketAlerts; - this.formatter = formatter; type = Type.Attention; } @@ -109,11 +106,10 @@ private void setColumns(TableView tableView) { column = new AutoTooltipTableColumn<>(Res.get("account.notifications.marketAlert.manageAlerts.header.paymentAccount")); column.setCellValueFactory((item) -> new ReadOnlyObjectWrapper<>(item.getValue())); column.setCellFactory( - new Callback, TableCell>() { + new Callback<>() { @Override public TableCell call(TableColumn column) { - return new TableCell() { - + return new TableCell<>() { @Override public void updateItem(final MarketAlertFilter item, boolean empty) { super.updateItem(item, empty); @@ -132,11 +128,10 @@ public void updateItem(final MarketAlertFilter item, boolean empty) { column = new AutoTooltipTableColumn<>(Res.get("account.notifications.marketAlert.manageAlerts.header.trigger")); column.setCellValueFactory((item) -> new ReadOnlyObjectWrapper<>(item.getValue())); column.setCellFactory( - new Callback, TableCell>() { + new Callback<>() { @Override public TableCell call(TableColumn column) { - return new TableCell() { - + return new TableCell<>() { @Override public void updateItem(final MarketAlertFilter item, boolean empty) { super.updateItem(item, empty); @@ -155,11 +150,10 @@ public void updateItem(final MarketAlertFilter item, boolean empty) { column = new AutoTooltipTableColumn<>(Res.get("account.notifications.marketAlert.manageAlerts.header.offerType")); column.setCellValueFactory((item) -> new ReadOnlyObjectWrapper<>(item.getValue())); column.setCellFactory( - new Callback, TableCell>() { + new Callback<>() { @Override public TableCell call(TableColumn column) { - return new TableCell() { - + return new TableCell<>() { @Override public void updateItem(final MarketAlertFilter item, boolean empty) { super.updateItem(item, empty); @@ -179,10 +173,10 @@ public void updateItem(final MarketAlertFilter item, boolean empty) { column.setMaxWidth(column.getMinWidth()); column.setCellValueFactory((item) -> new ReadOnlyObjectWrapper<>(item.getValue())); column.setCellFactory( - new Callback, TableCell>() { + new Callback<>() { @Override public TableCell call(TableColumn column) { - return new TableCell() { + return new TableCell<>() { final ImageView icon = ImageUtil.getImageViewById(ImageUtil.REMOVE_ICON); final Button removeButton = new AutoTooltipButton("", icon); diff --git a/desktop/src/main/java/bisq/desktop/main/account/content/notifications/MobileNotificationsView.java b/desktop/src/main/java/bisq/desktop/main/account/content/notifications/MobileNotificationsView.java index db3bf688d14..dca5c2ed38c 100644 --- a/desktop/src/main/java/bisq/desktop/main/account/content/notifications/MobileNotificationsView.java +++ b/desktop/src/main/java/bisq/desktop/main/account/content/notifications/MobileNotificationsView.java @@ -49,7 +49,6 @@ import bisq.core.user.User; import bisq.core.util.FormattingUtils; import bisq.core.util.ParsingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.core.util.validation.InputValidator; import bisq.common.UserThread; @@ -57,7 +56,6 @@ import bisq.common.util.Tuple3; import javax.inject.Inject; -import javax.inject.Named; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; @@ -91,7 +89,6 @@ public class MobileNotificationsView extends ActivatableView { private final PriceFeedService priceFeedService; private final MarketAlerts marketAlerts; private final MobileNotificationService mobileNotificationService; - private final CoinFormatter formatter; private WebCamWindow webCamWindow; private QrCodeReader qrCodeReader; @@ -127,15 +124,13 @@ private MobileNotificationsView(Preferences preferences, User user, PriceFeedService priceFeedService, MarketAlerts marketAlerts, - MobileNotificationService mobileNotificationService, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter) { + MobileNotificationService mobileNotificationService) { super(); this.preferences = preferences; this.user = user; this.priceFeedService = priceFeedService; this.marketAlerts = marketAlerts; this.mobileNotificationService = mobileNotificationService; - this.formatter = formatter; } @Override @@ -325,7 +320,7 @@ private void onSendTestMsg() { try { if (message != null) { mobileNotificationService.sendMessage(message, useSoundToggleButton.isSelected()); - } else if (messages != null) { + } else { messages.forEach(msg -> { try { mobileNotificationService.sendMessage(msg, useSoundToggleButton.isSelected()); @@ -359,7 +354,7 @@ private void onAddMarketAlert() { } private void onManageMarketAlerts() { - new ManageMarketAlertsWindow(marketAlerts, formatter) + new ManageMarketAlertsWindow(marketAlerts) .onClose(this::updateMarketAlertFields) .show(); } diff --git a/desktop/src/main/java/bisq/desktop/main/dao/news/NewsView.java b/desktop/src/main/java/bisq/desktop/main/dao/news/NewsView.java index 53a92c6dca8..aac80e2b164 100644 --- a/desktop/src/main/java/bisq/desktop/main/dao/news/NewsView.java +++ b/desktop/src/main/java/bisq/desktop/main/dao/news/NewsView.java @@ -9,7 +9,6 @@ import bisq.core.btc.wallet.BsqWalletService; import bisq.core.locale.Res; -import bisq.core.user.Preferences; import bisq.core.util.coin.BsqFormatter; import bisq.common.util.Tuple3; @@ -35,15 +34,12 @@ @FxmlView public class NewsView extends ActivatableView { - private final Preferences preferences; private final BsqWalletService bsqWalletService; private final BsqFormatter bsqFormatter; private BsqAddressTextField addressTextField; @Inject - private NewsView(Preferences preferences, BsqWalletService bsqWalletService, - BsqFormatter bsqFormatter) { - this.preferences = preferences; + private NewsView(BsqWalletService bsqWalletService, BsqFormatter bsqFormatter) { this.bsqWalletService = bsqWalletService; this.bsqFormatter = bsqFormatter; } diff --git a/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsView.java b/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsView.java index 78c2f495d76..3ed423c9a63 100644 --- a/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsView.java +++ b/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsView.java @@ -36,8 +36,6 @@ import bisq.core.trade.Tradable; import bisq.core.trade.Trade; import bisq.core.user.Preferences; -import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.network.p2p.P2PService; @@ -54,7 +52,6 @@ import com.googlecode.jcsv.writer.CSVEntryConverter; import javax.inject.Inject; -import javax.inject.Named; import de.jensd.fx.fontawesome.AwesomeIcon; @@ -102,7 +99,6 @@ public class TransactionsView extends ActivatableView { private final BtcWalletService btcWalletService; private final P2PService p2PService; private final WalletsSetup walletsSetup; - private final CoinFormatter formatter; private final Preferences preferences; private final TradeDetailsWindow tradeDetailsWindow; private final OfferDetailsWindow offerDetailsWindow; @@ -119,7 +115,6 @@ public class TransactionsView extends ActivatableView { private TransactionsView(BtcWalletService btcWalletService, P2PService p2PService, WalletsSetup walletsSetup, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter, Preferences preferences, TradeDetailsWindow tradeDetailsWindow, OfferDetailsWindow offerDetailsWindow, @@ -127,7 +122,6 @@ private TransactionsView(BtcWalletService btcWalletService, this.btcWalletService = btcWalletService; this.p2PService = p2PService; this.walletsSetup = walletsSetup; - this.formatter = formatter; this.preferences = preferences; this.tradeDetailsWindow = tradeDetailsWindow; this.offerDetailsWindow = offerDetailsWindow; 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 af09f4cbdc4..c7c6bf76376 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 @@ -38,13 +38,9 @@ import bisq.core.offer.OfferPayload; import bisq.core.provider.price.PriceFeedService; import bisq.core.user.Preferences; -import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import com.google.inject.Inject; -import javax.inject.Named; - import com.google.common.math.LongMath; import javafx.scene.chart.XYChart; @@ -85,7 +81,6 @@ class OfferBookChartViewModel extends ActivatableViewModel { private final ObservableList topBuyOfferList = FXCollections.observableArrayList(); private final ObservableList topSellOfferList = FXCollections.observableArrayList(); private final ChangeListener currenciesUpdatedListener; - private final CoinFormatter formatter; private int selectedTabIndex; public final IntegerProperty maxPlacesForBuyPrice = new SimpleIntegerProperty(); public final IntegerProperty maxPlacesForBuyVolume = new SimpleIntegerProperty(); @@ -96,15 +91,13 @@ class OfferBookChartViewModel extends ActivatableViewModel { // Constructor, lifecycle /////////////////////////////////////////////////////////////////////////////////////////// - @SuppressWarnings("WeakerAccess") @Inject - public OfferBookChartViewModel(OfferBook offerBook, Preferences preferences, PriceFeedService priceFeedService, - AccountAgeWitnessService accountAgeWitnessService, Navigation navigation, @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter) { + OfferBookChartViewModel(OfferBook offerBook, Preferences preferences, PriceFeedService priceFeedService, + AccountAgeWitnessService accountAgeWitnessService, Navigation navigation) { this.offerBook = offerBook; this.preferences = preferences; this.priceFeedService = priceFeedService; this.navigation = navigation; - this.formatter = formatter; this.accountAgeWitnessService = accountAgeWitnessService; String code = preferences.getOfferBookChartScreenCurrencyCode(); diff --git a/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java b/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java index 64513617425..f070b5946b3 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java @@ -37,8 +37,6 @@ import bisq.core.trade.statistics.TradeStatistics2; import bisq.core.trade.statistics.TradeStatisticsManager; import bisq.core.user.Preferences; -import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.common.util.MathUtils; @@ -46,8 +44,6 @@ import com.google.inject.Inject; -import javax.inject.Named; - import com.google.common.annotations.VisibleForTesting; import javafx.scene.chart.XYChart; @@ -70,6 +66,7 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -102,7 +99,6 @@ public enum TickUnit { final Preferences preferences; private PriceFeedService priceFeedService; private Navigation navigation; - private CoinFormatter formatter; private final SetChangeListener setChangeListener; final ObjectProperty selectedTradeCurrencyProperty = new SimpleObjectProperty<>(); @@ -114,7 +110,7 @@ public enum TickUnit { final ObservableList> volumeItems = FXCollections.observableArrayList(); private Map>> itemsPerInterval; - TickUnit tickUnit = TickUnit.DAY; + TickUnit tickUnit; final int maxTicks = 90; private int selectedTabIndex; @@ -122,14 +118,12 @@ public enum TickUnit { // Constructor, lifecycle /////////////////////////////////////////////////////////////////////////////////////////// - @SuppressWarnings("WeakerAccess") @Inject - public TradesChartsViewModel(TradeStatisticsManager tradeStatisticsManager, Preferences preferences, PriceFeedService priceFeedService, Navigation navigation, @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter) { + TradesChartsViewModel(TradeStatisticsManager tradeStatisticsManager, Preferences preferences, PriceFeedService priceFeedService, Navigation navigation) { this.tradeStatisticsManager = tradeStatisticsManager; this.preferences = preferences; this.priceFeedService = priceFeedService; this.navigation = navigation; - this.formatter = formatter; setChangeListener = change -> { updateChartData(); @@ -139,11 +133,9 @@ public TradesChartsViewModel(TradeStatisticsManager tradeStatisticsManager, Pref String tradeChartsScreenCurrencyCode = preferences.getTradeChartsScreenCurrencyCode(); showAllTradeCurrenciesProperty.set(isShowAllEntry(tradeChartsScreenCurrencyCode)); - Optional tradeCurrencyOptional = CurrencyUtil.getTradeCurrency(tradeChartsScreenCurrencyCode); - if (tradeCurrencyOptional.isPresent()) - selectedTradeCurrencyProperty.set(tradeCurrencyOptional.get()); - else - selectedTradeCurrencyProperty.set(GlobalSettings.getDefaultTradeCurrency()); + TradeCurrency tradeCurrency = CurrencyUtil.getTradeCurrency(tradeChartsScreenCurrencyCode) + .orElse(GlobalSettings.getDefaultTradeCurrency()); + selectedTradeCurrencyProperty.set(tradeCurrency); tickUnit = TickUnit.values()[preferences.getTradeStatisticsTickUnitIndex()]; @@ -153,15 +145,7 @@ public TradesChartsViewModel(TradeStatisticsManager tradeStatisticsManager, Pref private void fillTradeCurrencies() { // Don't use a set as we need all entries List tradeCurrencyList = tradeStatisticsManager.getObservableTradeStatisticsSet().stream() - .map(e -> { - Optional tradeCurrencyOptional = CurrencyUtil.getTradeCurrency(e.getCurrencyCode()); - if (tradeCurrencyOptional.isPresent()) - return tradeCurrencyOptional.get(); - else - return null; - - }) - .filter(e -> e != null) + .flatMap(e -> CurrencyUtil.getTradeCurrency(e.getCurrencyCode()).stream()) .collect(Collectors.toList()); currencyListItems.updateWithCurrencies(tradeCurrencyList, showAllCurrencyListItem); @@ -272,7 +256,7 @@ private void updateChartData() { } // Get all entries for the defined time interval - tradeStatisticsByCurrency.stream().forEach(e -> { + tradeStatisticsByCurrency.forEach(e -> { for (long i = maxTicks; i > 0; --i) { Pair> p = itemsPerInterval.get(i); if (e.getTradeDate().after(p.getKey())) { @@ -286,15 +270,13 @@ private void updateChartData() { List candleDataList = itemsPerInterval.entrySet().stream() .filter(entry -> entry.getKey() >= 0 && !entry.getValue().getValue().isEmpty()) .map(entry -> getCandleData(entry.getKey(), entry.getValue().getValue())) + .sorted(Comparator.comparingLong(o -> o.tick)) .collect(Collectors.toList()); - candleDataList.sort((o1, o2) -> (o1.tick < o2.tick ? -1 : (o1.tick == o2.tick ? 0 : 1))); - //noinspection Convert2Diamond priceItems.setAll(candleDataList.stream() .map(e -> new XYChart.Data(e.tick, e.open, e)) .collect(Collectors.toList())); - //noinspection Convert2Diamond volumeItems.setAll(candleDataList.stream() .map(e -> new XYChart.Data(e.tick, e.accumulatedAmount, e)) .collect(Collectors.toList())); @@ -313,19 +295,18 @@ CandleData getCandleData(long tick, Set set) { for (TradeStatistics2 item : set) { long tradePriceAsLong = item.getTradePrice().getValue(); - // Previously a check was done which inverted the low and high for - // crytocurrencies. - low = (low != 0) ? Math.min(low, tradePriceAsLong) : tradePriceAsLong; - high = (high != 0) ? Math.max(high, tradePriceAsLong) : tradePriceAsLong; + // Previously a check was done which inverted the low and high for cryptocurrencies. + low = (low != 0) ? Math.min(low, tradePriceAsLong) : tradePriceAsLong; + high = (high != 0) ? Math.max(high, tradePriceAsLong) : tradePriceAsLong; - accumulatedVolume += (item.getTradeVolume() != null) ? item.getTradeVolume().getValue() : 0; + accumulatedVolume += item.getTradeVolume().getValue(); accumulatedAmount += item.getTradeAmount().getValue(); tradePrices.add(item.getTradePrice().getValue()); } Collections.sort(tradePrices); List list = new ArrayList<>(set); - list.sort((o1, o2) -> (o1.getTradeDate().getTime() < o2.getTradeDate().getTime() ? -1 : (o1.getTradeDate().getTime() == o2.getTradeDate().getTime() ? 0 : 1))); + list.sort(Comparator.comparingLong(o -> o.getTradeDate().getTime())); if (list.size() > 0) { open = list.get(0).getTradePrice().getValue(); close = list.get(list.size() - 1).getTradePrice().getValue(); diff --git a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java index 33ba7f6f54c..eebc78e1a2d 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java @@ -45,7 +45,6 @@ import bisq.desktop.main.portfolio.openoffer.OpenOffersView; import bisq.desktop.util.GUIUtil; import bisq.desktop.util.Layout; -import bisq.desktop.util.Transitions; import bisq.core.locale.CurrencyUtil; import bisq.core.locale.Res; @@ -125,7 +124,6 @@ public abstract class MutableOfferView> exten public static final String BUYER_SECURITY_DEPOSIT_NEWS = "buyerSecurityDepositNews0.9.5"; protected final Navigation navigation; private final Preferences preferences; - private final Transitions transitions; private final OfferDetailsWindow offerDetailsWindow; private final CoinFormatter btcFormatter; private final BsqFormatter bsqFormatter; @@ -183,7 +181,6 @@ public abstract class MutableOfferView> exten public MutableOfferView(M model, Navigation navigation, Preferences preferences, - Transitions transitions, OfferDetailsWindow offerDetailsWindow, CoinFormatter btcFormatter, BsqFormatter bsqFormatter) { @@ -191,7 +188,6 @@ public MutableOfferView(M model, this.navigation = navigation; this.preferences = preferences; - this.transitions = transitions; this.offerDetailsWindow = offerDetailsWindow; this.btcFormatter = btcFormatter; this.bsqFormatter = bsqFormatter; diff --git a/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferView.java b/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferView.java index d81d804e59f..1f326ebc714 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferView.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferView.java @@ -21,7 +21,6 @@ import bisq.desktop.common.view.FxmlView; import bisq.desktop.main.offer.MutableOfferView; import bisq.desktop.main.overlays.windows.OfferDetailsWindow; -import bisq.desktop.util.Transitions; import bisq.core.user.Preferences; import bisq.core.util.FormattingUtils; @@ -36,8 +35,12 @@ public class CreateOfferView extends MutableOfferView { @Inject - public CreateOfferView(CreateOfferViewModel model, Navigation navigation, Preferences preferences, Transitions transitions, OfferDetailsWindow offerDetailsWindow, @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter, BsqFormatter bsqFormatter) { - super(model, navigation, preferences, transitions, offerDetailsWindow, btcFormatter, bsqFormatter); + private CreateOfferView(CreateOfferViewModel model, + Navigation navigation, + Preferences preferences, + OfferDetailsWindow offerDetailsWindow, + @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter, + BsqFormatter bsqFormatter) { + super(model, navigation, preferences, offerDetailsWindow, btcFormatter, bsqFormatter); } } - diff --git a/desktop/src/main/java/bisq/desktop/main/offer/takeoffer/TakeOfferViewModel.java b/desktop/src/main/java/bisq/desktop/main/offer/takeoffer/TakeOfferViewModel.java index 20d0ae06f4a..55cae99dcd0 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/takeoffer/TakeOfferViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/takeoffer/TakeOfferViewModel.java @@ -29,7 +29,6 @@ import bisq.desktop.util.validation.BtcValidator; import bisq.core.account.witness.AccountAgeWitnessService; -import bisq.core.btc.setup.WalletsSetup; import bisq.core.locale.CurrencyUtil; import bisq.core.locale.Res; import bisq.core.monetary.Price; @@ -43,8 +42,8 @@ import bisq.core.provider.price.PriceFeedService; import bisq.core.trade.Trade; import bisq.core.user.Preferences; -import bisq.core.util.coin.BsqFormatter; import bisq.core.util.FormattingUtils; +import bisq.core.util.coin.BsqFormatter; import bisq.core.util.coin.CoinFormatter; import bisq.core.util.validation.InputValidator; @@ -87,7 +86,6 @@ class TakeOfferViewModel extends ActivatableWithDataModel im final TakeOfferDataModel dataModel; private final BtcValidator btcValidator; private final P2PService p2PService; - private final WalletsSetup walletsSetup; private final Preferences preferences; private final PriceFeedService priceFeedService; private AccountAgeWitnessService accountAgeWitnessService; @@ -148,7 +146,6 @@ class TakeOfferViewModel extends ActivatableWithDataModel im public TakeOfferViewModel(TakeOfferDataModel dataModel, BtcValidator btcValidator, P2PService p2PService, - WalletsSetup walletsSetup, Preferences preferences, PriceFeedService priceFeedService, AccountAgeWitnessService accountAgeWitnessService, @@ -160,7 +157,6 @@ public TakeOfferViewModel(TakeOfferDataModel dataModel, this.btcValidator = btcValidator; this.p2PService = p2PService; - this.walletsSetup = walletsSetup; this.preferences = preferences; this.priceFeedService = priceFeedService; this.accountAgeWitnessService = accountAgeWitnessService; @@ -283,7 +279,6 @@ boolean fundFromSavingsWallet() { .show(); return false; } - } public void setIsCurrencyForTakerFeeBtc(boolean isCurrencyForTakerFeeBtc) { diff --git a/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java b/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java index c09da7497ee..5970aa49b50 100644 --- a/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java +++ b/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java @@ -38,16 +38,13 @@ import bisq.core.trade.Tradable; import bisq.core.trade.Trade; import bisq.core.user.Preferences; -import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.network.p2p.NodeAddress; import com.googlecode.jcsv.writer.CSVEntryConverter; -import javax.inject.Named; - import javax.inject.Inject; +import javax.inject.Named; import javafx.fxml.FXML; @@ -99,7 +96,6 @@ public class ClosedTradesView extends ActivatableViewAndModel sortedList; @@ -112,14 +108,12 @@ public ClosedTradesView(ClosedTradesViewModel model, Preferences preferences, TradeDetailsWindow tradeDetailsWindow, PrivateNotificationManager privateNotificationManager, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter, @Named(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS) boolean useDevPrivilegeKeys) { super(model); this.offerDetailsWindow = offerDetailsWindow; this.preferences = preferences; this.tradeDetailsWindow = tradeDetailsWindow; this.privateNotificationManager = privateNotificationManager; - this.formatter = formatter; this.useDevPrivilegeKeys = useDevPrivilegeKeys; } diff --git a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferView.java b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferView.java index 21f65c20199..9beaca27355 100644 --- a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferView.java +++ b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferView.java @@ -24,7 +24,6 @@ import bisq.desktop.main.offer.MutableOfferView; import bisq.desktop.main.overlays.popups.Popup; import bisq.desktop.main.overlays.windows.OfferDetailsWindow; -import bisq.desktop.util.Transitions; import bisq.core.locale.CurrencyUtil; import bisq.core.locale.Res; @@ -64,8 +63,13 @@ public class EditOfferView extends MutableOfferView { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - private EditOfferView(EditOfferViewModel model, Navigation navigation, Preferences preferences, Transitions transitions, OfferDetailsWindow offerDetailsWindow, @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter, BsqFormatter bsqFormatter) { - super(model, navigation, preferences, transitions, offerDetailsWindow, btcFormatter, bsqFormatter); + private EditOfferView(EditOfferViewModel model, + Navigation navigation, + Preferences preferences, + OfferDetailsWindow offerDetailsWindow, + @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter, + BsqFormatter bsqFormatter) { + super(model, navigation, preferences, offerDetailsWindow, btcFormatter, bsqFormatter); } @Override diff --git a/desktop/src/main/java/bisq/desktop/main/presentation/MarketPricePresentation.java b/desktop/src/main/java/bisq/desktop/main/presentation/MarketPricePresentation.java index e62ae61ec0b..38ecd896f6b 100644 --- a/desktop/src/main/java/bisq/desktop/main/presentation/MarketPricePresentation.java +++ b/desktop/src/main/java/bisq/desktop/main/presentation/MarketPricePresentation.java @@ -31,12 +31,10 @@ import bisq.core.provider.price.PriceFeedService; import bisq.core.user.Preferences; import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.common.UserThread; import javax.inject.Inject; -import javax.inject.Named; import javax.inject.Singleton; import org.fxmisc.easybind.EasyBind; @@ -66,7 +64,6 @@ @Singleton public class MarketPricePresentation { private final Preferences preferences; - private final CoinFormatter formatter; private final PriceFeedService priceFeedService; @Getter private final ObservableList priceFeedComboBoxItems = FXCollections.observableArrayList(); @@ -93,11 +90,9 @@ public class MarketPricePresentation { public MarketPricePresentation(BtcWalletService btcWalletService, PriceFeedService priceFeedService, Preferences preferences, - FeeService feeService, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter) { + FeeService feeService) { this.priceFeedService = priceFeedService; this.preferences = preferences; - this.formatter = formatter; TxIdTextField.setPreferences(preferences); @@ -247,10 +242,10 @@ public StringProperty getMarketPrice() { public StringProperty getMarketPrice(String currencyCode) { SimpleStringProperty marketPrice = new SimpleStringProperty(Res.get("shared.na")); - try { - marketPrice.set(String.valueOf(priceFeedService.getMarketPrice(currencyCode).getPrice())); - } catch (NullPointerException e) { - // Market price is not available yet + MarketPrice marketPriceValue = priceFeedService.getMarketPrice(currencyCode); + // Market price might not be available yet: + if (marketPriceValue != null) { + marketPrice.set(String.valueOf(marketPriceValue.getPrice())); } return marketPrice; } diff --git a/desktop/src/main/java/bisq/desktop/main/settings/network/NetworkSettingsView.java b/desktop/src/main/java/bisq/desktop/main/settings/network/NetworkSettingsView.java index 9b37a4aa544..f23c83d4996 100644 --- a/desktop/src/main/java/bisq/desktop/main/settings/network/NetworkSettingsView.java +++ b/desktop/src/main/java/bisq/desktop/main/settings/network/NetworkSettingsView.java @@ -36,7 +36,6 @@ import bisq.core.locale.Res; import bisq.core.user.Preferences; import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.network.p2p.P2PService; import bisq.network.p2p.network.Statistic; @@ -45,7 +44,6 @@ import bisq.common.UserThread; import javax.inject.Inject; -import javax.inject.Named; import javafx.fxml.FXML; @@ -112,7 +110,6 @@ public class NetworkSettingsView extends ActivatableView { private final BisqEnvironment bisqEnvironment; private final TorNetworkSettingsWindow torNetworkSettingsWindow; private final ClockWatcher clockWatcher; - private final CoinFormatter formatter; private final WalletsSetup walletsSetup; private final P2PService p2PService; @@ -142,8 +139,7 @@ public NetworkSettingsView(WalletsSetup walletsSetup, FilterManager filterManager, BisqEnvironment bisqEnvironment, TorNetworkSettingsWindow torNetworkSettingsWindow, - ClockWatcher clockWatcher, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter) { + ClockWatcher clockWatcher) { super(); this.walletsSetup = walletsSetup; this.p2PService = p2PService; @@ -153,7 +149,6 @@ public NetworkSettingsView(WalletsSetup walletsSetup, this.bisqEnvironment = bisqEnvironment; this.torNetworkSettingsWindow = torNetworkSettingsWindow; this.clockWatcher = clockWatcher; - this.formatter = formatter; } public void initialize() { @@ -279,7 +274,7 @@ public void activate() { .actionButtonText(Res.get("shared.applyAndShutDown")) .onAction(() -> { preferences.setUseTorForBitcoinJ(selected); - UserThread.runAfter(BisqApp.getShutDownHandler()::run, 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(BisqApp.getShutDownHandler(), 500, TimeUnit.MILLISECONDS); }) .closeButtonText(Res.get("shared.cancel")) .onClose(() -> useTorForBtcJCheckBox.setSelected(!selected)) @@ -464,7 +459,7 @@ private void updateP2PTable() { p2pPeersTableView.getItems().forEach(P2pNetworkListItem::cleanup); p2pNetworkListItems.clear(); p2pNetworkListItems.setAll(p2PService.getNetworkNode().getAllConnections().stream() - .map(connection -> new P2pNetworkListItem(connection, clockWatcher, formatter)) + .map(connection -> new P2pNetworkListItem(connection, clockWatcher)) .collect(Collectors.toList())); } diff --git a/desktop/src/main/java/bisq/desktop/main/settings/network/P2pNetworkListItem.java b/desktop/src/main/java/bisq/desktop/main/settings/network/P2pNetworkListItem.java index 81f558de24d..e68a504564d 100644 --- a/desktop/src/main/java/bisq/desktop/main/settings/network/P2pNetworkListItem.java +++ b/desktop/src/main/java/bisq/desktop/main/settings/network/P2pNetworkListItem.java @@ -21,7 +21,6 @@ import bisq.core.locale.Res; import bisq.core.util.FormattingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.network.p2p.network.Connection; import bisq.network.p2p.network.OutboundConnection; @@ -47,7 +46,6 @@ public class P2pNetworkListItem { private final Connection connection; private final Subscription sentBytesSubscription, receivedBytesSubscription, onionAddressSubscription, roundTripTimeSubscription; private final ClockWatcher clockWatcher; - private final CoinFormatter formatter; private final StringProperty lastActivity = new SimpleStringProperty(); private final StringProperty sentBytes = new SimpleStringProperty(); @@ -58,10 +56,9 @@ public class P2pNetworkListItem { private final StringProperty onionAddress = new SimpleStringProperty(); private final ClockWatcher.Listener listener; - public P2pNetworkListItem(Connection connection, ClockWatcher clockWatcher, CoinFormatter formatter) { + P2pNetworkListItem(Connection connection, ClockWatcher clockWatcher) { this.connection = connection; this.clockWatcher = clockWatcher; - this.formatter = formatter; this.statistic = connection.getStatistic(); sentBytesSubscription = EasyBind.subscribe(statistic.sentBytesProperty(), diff --git a/desktop/src/main/java/bisq/desktop/main/settings/preferences/PreferencesView.java b/desktop/src/main/java/bisq/desktop/main/settings/preferences/PreferencesView.java index 709c7769318..7f2671da99d 100644 --- a/desktop/src/main/java/bisq/desktop/main/settings/preferences/PreferencesView.java +++ b/desktop/src/main/java/bisq/desktop/main/settings/preferences/PreferencesView.java @@ -48,7 +48,6 @@ import bisq.core.user.Preferences; import bisq.core.util.FormattingUtils; import bisq.core.util.ParsingUtils; -import bisq.core.util.coin.CoinFormatter; import bisq.core.util.validation.IntegerValidator; import bisq.common.UserThread; @@ -124,7 +123,6 @@ public class PreferencesView extends ActivatableViewAndModel fiatCurrenciesListView; private ComboBox fiatCurrenciesComboBox; @@ -161,7 +159,6 @@ public PreferencesView(PreferencesViewModel model, AssetService assetService, FilterManager filterManager, DaoFacade daoFacade, - @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter formatter, @Named(DaoOptionKeys.FULL_DAO_NODE) String fullDaoNode, @Named(DaoOptionKeys.RPC_USER) String rpcUser, @Named(DaoOptionKeys.RPC_PASSWORD) String rpcPassword, @@ -172,7 +169,6 @@ public PreferencesView(PreferencesViewModel model, this.assetService = assetService; this.filterManager = filterManager; this.daoFacade = daoFacade; - this.formatter = formatter; daoOptionsSet = fullDaoNode != null && !fullDaoNode.isEmpty() && rpcUser != null && !rpcUser.isEmpty() && rpcPassword != null && !rpcPassword.isEmpty() && diff --git a/desktop/src/test/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModelTest.java b/desktop/src/test/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModelTest.java index 83ab998426a..18bdbbf0662 100644 --- a/desktop/src/test/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModelTest.java +++ b/desktop/src/test/java/bisq/desktop/main/market/offerbook/OfferBookChartViewModelTest.java @@ -62,7 +62,7 @@ public void testMaxCharactersForBuyPriceWithNoOffers() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null); assertEquals(0, model.maxPlacesForBuyPrice.intValue()); } @@ -81,7 +81,7 @@ public void testMaxCharactersForBuyPriceWithOfflinePriceFeedService() { when(priceFeedService.updateCounterProperty()).thenReturn(new SimpleIntegerProperty()); when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, priceFeedService, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, priceFeedService, null, null); model.activate(); assertEquals(0, model.maxPlacesForBuyPrice.intValue()); } @@ -95,7 +95,7 @@ public void testMaxCharactersForFiatBuyPrice() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null); model.activate(); assertEquals(7, model.maxPlacesForBuyPrice.intValue()); offerBookListItems.addAll(make(btcBuyItem.but(with(OfferBookListItemMaker.price, 94016475L)))); @@ -111,7 +111,7 @@ public void testMaxCharactersForBuyVolumeWithNoOffers() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null); assertEquals(0, model.maxPlacesForBuyVolume.intValue()); } @@ -124,7 +124,7 @@ public void testMaxCharactersForFiatBuyVolume() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null); model.activate(); assertEquals(4, model.maxPlacesForBuyVolume.intValue()); //0.01 offerBookListItems.addAll(make(btcBuyItem.but(with(OfferBookListItemMaker.amount, 100000000L)))); @@ -140,7 +140,7 @@ public void testMaxCharactersForSellPriceWithNoOffers() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null); assertEquals(0, model.maxPlacesForSellPrice.intValue()); } @@ -159,7 +159,7 @@ public void testMaxCharactersForSellPriceWithOfflinePriceFeedService() { when(priceFeedService.updateCounterProperty()).thenReturn(new SimpleIntegerProperty()); when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, priceFeedService, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, priceFeedService, null, null); model.activate(); assertEquals(0, model.maxPlacesForSellPrice.intValue()); } @@ -173,7 +173,7 @@ public void testMaxCharactersForFiatSellPrice() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null); model.activate(); assertEquals(7, model.maxPlacesForSellPrice.intValue()); // 10.0000 default price offerBookListItems.addAll(make(btcSellItem.but(with(OfferBookListItemMaker.price, 94016475L)))); @@ -189,7 +189,7 @@ public void testMaxCharactersForSellVolumeWithNoOffers() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, null); assertEquals(0, model.maxPlacesForSellVolume.intValue()); } @@ -202,7 +202,7 @@ public void testMaxCharactersForFiatSellVolume() { when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems); - final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null, coinFormatter); + final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, service, null, null); model.activate(); assertEquals(4, model.maxPlacesForSellVolume.intValue()); //0.01 offerBookListItems.addAll(make(btcSellItem.but(with(OfferBookListItemMaker.amount, 100000000L)))); diff --git a/desktop/src/test/java/bisq/desktop/main/market/trades/TradesChartsViewModelTest.java b/desktop/src/test/java/bisq/desktop/main/market/trades/TradesChartsViewModelTest.java index 61076cbf8a0..f3223f5572f 100644 --- a/desktop/src/test/java/bisq/desktop/main/market/trades/TradesChartsViewModelTest.java +++ b/desktop/src/test/java/bisq/desktop/main/market/trades/TradesChartsViewModelTest.java @@ -27,7 +27,6 @@ import bisq.core.trade.statistics.TradeStatistics2; import bisq.core.trade.statistics.TradeStatisticsManager; import bisq.core.user.Preferences; -import bisq.core.util.coin.ImmutableCoinFormatter; import bisq.common.crypto.KeyRing; import bisq.common.crypto.KeyStorage; @@ -111,7 +110,7 @@ public class TradesChartsViewModelTest { public void setup() throws IOException { tradeStatisticsManager = mock(TradeStatisticsManager.class); model = new TradesChartsViewModel(tradeStatisticsManager, mock(Preferences.class), mock(PriceFeedService.class), - mock(Navigation.class), mock(ImmutableCoinFormatter.class)); + mock(Navigation.class)); dir = File.createTempFile("temp_tests1", ""); //noinspection ResultOfMethodCallIgnored dir.delete(); @@ -181,7 +180,7 @@ class Trade { // Trade EUR model.selectedTradeCurrencyProperty.setValue(new FiatCurrency("EUR")); - ArrayList trades = new ArrayList(); + ArrayList trades = new ArrayList<>(); // Set predetermined time to use as "now" during test Date test_time = dateFormat.parse("2018-01-01T00:00:05"); // Monday @@ -197,9 +196,7 @@ long currentTimeMillis() { trades.add(new Trade("2018-01-01T00:00:02", "1", "110", "EUR")); Set set = new HashSet<>(); trades.forEach(t -> - { - set.add(new TradeStatistics2(offer, Price.parse(t.cc, t.price), Coin.parseCoin(t.size), t.date, null, null)); - } + set.add(new TradeStatistics2(offer, Price.parse(t.cc, t.price), Coin.parseCoin(t.size), t.date, null, null)) ); ObservableSet tradeStats = FXCollections.observableSet(set); From 0bcf238d7130fd71eb0f630d257ef4cb0956796e Mon Sep 17 00:00:00 2001 From: Steven Barclay Date: Mon, 23 Dec 2019 22:19:58 +0000 Subject: [PATCH 2/2] Optimise CurrencyUtil.get[Fiat|Crypto|Trade]Currency Use a LinkedHashMap in place of a List, for the caching CurrencyUtil fields 'allSortedFiatCurrencies' & 'allSortedCryptoCurrencies', using the same iteration order as before. In this way, we can avoid a linear search in the lookup methods getFiatCurrency & getCryptoCurrency. In particular, this speeds up the activation of TradesChartsView (and to a lesser extent OfferBookChartView), which make a lot of calls to CurrencyUtil.getTradeCurrency in the fillTradeCurrencies/updateChartData methods respectively. --- .../java/bisq/core/locale/CurrencyUtil.java | 46 ++++++++++--------- .../paymentmethods/PaymentMethodForm.java | 2 +- .../java/bisq/desktop/MarketsPrintTool.java | 6 +-- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/bisq/core/locale/CurrencyUtil.java b/core/src/main/java/bisq/core/locale/CurrencyUtil.java index 7030f658d24..982aab54006 100644 --- a/core/src/main/java/bisq/core/locale/CurrencyUtil.java +++ b/core/src/main/java/bisq/core/locale/CurrencyUtil.java @@ -30,14 +30,20 @@ import bisq.common.app.DevEnv; +import com.google.common.base.Suppliers; + import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Comparator; import java.util.Currency; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; -import java.util.Objects; +import java.util.Map; import java.util.Optional; +import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -55,26 +61,26 @@ public static void setup() { private static final AssetRegistry assetRegistry = new AssetRegistry(); private static String baseCurrencyCode = "BTC"; - private static List allSortedFiatCurrencies; - private static List allSortedCryptoCurrencies; + + private static Supplier> fiatCurrencyMapSupplier = Suppliers.memoize( + CurrencyUtil::createFiatCurrencyMap)::get; + private static Supplier> cryptoCurrencyMapSupplier = Suppliers.memoize( + CurrencyUtil::createCryptoCurrencyMap)::get; public static void setBaseCurrencyCode(String baseCurrencyCode) { CurrencyUtil.baseCurrencyCode = baseCurrencyCode; } - public static List getAllSortedFiatCurrencies() { - if (Objects.isNull(allSortedFiatCurrencies)) - allSortedFiatCurrencies = createAllSortedFiatCurrenciesList(); - - return allSortedFiatCurrencies; + public static Collection getAllSortedFiatCurrencies() { + return fiatCurrencyMapSupplier.get().values(); } - private static List createAllSortedFiatCurrenciesList() { + private static Map createFiatCurrencyMap() { return CountryUtil.getAllCountries().stream() .map(country -> getCurrencyByCountryCode(country.code)) .distinct() .sorted(TradeCurrency::compareTo) - .collect(Collectors.toList()); + .collect(Collectors.toMap(TradeCurrency::getCode, Function.identity(), (x, y) -> x, LinkedHashMap::new)); } public static List getMainFiatCurrencies() { @@ -94,23 +100,20 @@ public static List getMainFiatCurrencies() { FiatCurrency defaultFiatCurrency = defaultTradeCurrency instanceof FiatCurrency ? (FiatCurrency) defaultTradeCurrency : null; if (defaultFiatCurrency != null && list.contains(defaultFiatCurrency)) { - //noinspection SuspiciousMethodCalls list.remove(defaultTradeCurrency); list.add(0, defaultFiatCurrency); } return list; } - public static List getAllSortedCryptoCurrencies() { - if (allSortedCryptoCurrencies == null) - allSortedCryptoCurrencies = createAllSortedCryptoCurrenciesList(); - return allSortedCryptoCurrencies; + public static Collection getAllSortedCryptoCurrencies() { + return cryptoCurrencyMapSupplier.get().values(); } - private static List createAllSortedCryptoCurrenciesList() { + private static Map createCryptoCurrencyMap() { return getSortedAssetStream() .map(CurrencyUtil::assetToCryptoCurrency) - .collect(Collectors.toList()); + .collect(Collectors.toMap(TradeCurrency::getCode, Function.identity(), (x, y) -> x, LinkedHashMap::new)); } public static Stream getSortedAssetStream() { @@ -320,10 +323,9 @@ public static boolean isFiatCurrency(String currencyCode) { } public static Optional getFiatCurrency(String currencyCode) { - return getAllSortedFiatCurrencies().stream().filter(e -> e.getCode().equals(currencyCode)).findAny(); + return Optional.ofNullable(fiatCurrencyMapSupplier.get().get(currencyCode)); } - @SuppressWarnings("WeakerAccess") /** * We return true if it is BTC or any of our currencies available in the assetRegistry. * For removed assets it would fail as they are not found but we don't want to conclude that they are fiat then. @@ -361,16 +363,16 @@ public static boolean isCryptoCurrency(String currencyCode) { } public static Optional getCryptoCurrency(String currencyCode) { - return getAllSortedCryptoCurrencies().stream().filter(e -> e.getCode().equals(currencyCode)).findAny(); + return Optional.ofNullable(cryptoCurrencyMapSupplier.get().get(currencyCode)); } public static Optional getTradeCurrency(String currencyCode) { Optional fiatCurrencyOptional = getFiatCurrency(currencyCode); - if (isFiatCurrency(currencyCode) && fiatCurrencyOptional.isPresent()) + if (fiatCurrencyOptional.isPresent() && isFiatCurrency(currencyCode)) return Optional.of(fiatCurrencyOptional.get()); Optional cryptoCurrencyOptional = getCryptoCurrency(currencyCode); - if (isCryptoCurrency(currencyCode) && cryptoCurrencyOptional.isPresent()) + if (cryptoCurrencyOptional.isPresent() && isCryptoCurrency(currencyCode)) return Optional.of(cryptoCurrencyOptional.get()); return Optional.empty(); diff --git a/desktop/src/main/java/bisq/desktop/components/paymentmethods/PaymentMethodForm.java b/desktop/src/main/java/bisq/desktop/components/paymentmethods/PaymentMethodForm.java index 9266f4f2628..1b4206addb3 100644 --- a/desktop/src/main/java/bisq/desktop/components/paymentmethods/PaymentMethodForm.java +++ b/desktop/src/main/java/bisq/desktop/components/paymentmethods/PaymentMethodForm.java @@ -176,7 +176,7 @@ else if (!paymentAccount.getTradeCurrencies().isEmpty()) tradeCurrency = paymentAccount.getTradeCurrencies().get(0); else tradeCurrency = paymentAccount instanceof AssetAccount ? - CurrencyUtil.getAllSortedCryptoCurrencies().get(0) : + CurrencyUtil.getAllSortedCryptoCurrencies().iterator().next() : CurrencyUtil.getDefaultTradeCurrency(); diff --git a/desktop/src/test/java/bisq/desktop/MarketsPrintTool.java b/desktop/src/test/java/bisq/desktop/MarketsPrintTool.java index 4c608c2d3a8..896a492fe7a 100644 --- a/desktop/src/test/java/bisq/desktop/MarketsPrintTool.java +++ b/desktop/src/test/java/bisq/desktop/MarketsPrintTool.java @@ -21,8 +21,8 @@ import bisq.core.locale.CurrencyUtil; import bisq.core.locale.FiatCurrency; +import java.util.Collection; import java.util.Comparator; -import java.util.List; import java.util.Locale; import java.util.stream.Stream; @@ -37,14 +37,14 @@ public static void main(String[] args) { // // - final List allSortedFiatCurrencies = CurrencyUtil.getAllSortedFiatCurrencies(); + final Collection allSortedFiatCurrencies = CurrencyUtil.getAllSortedFiatCurrencies(); final Stream fiatStream = allSortedFiatCurrencies.stream() .filter(e -> !e.getCurrency().getCurrencyCode().equals("BSQ")) .filter(e -> !e.getCurrency().getCurrencyCode().equals("BTC")) .map(e -> new MarketCurrency("btc_" + e.getCode().toLowerCase(), e.getName(), e.getCode())) .distinct(); - final List allSortedCryptoCurrencies = CurrencyUtil.getAllSortedCryptoCurrencies(); + final Collection allSortedCryptoCurrencies = CurrencyUtil.getAllSortedCryptoCurrencies(); final Stream cryptoStream = allSortedCryptoCurrencies.stream() .filter(e -> !e.getCode().equals("BTC")) .map(e -> new MarketCurrency(e.getCode().toLowerCase() + "_btc", e.getName(), e.getCode()))