From 190dcaa67bc34713f5c8169e6121d1e7d8531b4d Mon Sep 17 00:00:00 2001 From: bsc7 <118875465+bsc7@users.noreply.github.com> Date: Mon, 21 Oct 2024 21:43:13 +0200 Subject: [PATCH] Refact: m/account: Nullable -> Optional #1631 see also https://github.com/bisq-network/bisq2/pull/1106 --- .../java/bisq/account/AccountService.java | 24 ++--- .../accounts/AchTransferAccountPayload.java | 38 ++++---- .../account/accounts/BankAccountPayload.java | 97 +++++++++---------- .../accounts/CashDepositAccountPayload.java | 33 ++++--- .../accounts/NationalBankAccountPayload.java | 37 ++++--- .../AchTransferAccountPayloadTest.java | 9 +- .../accounts/AchTransferAccountTest.java | 6 +- .../accounts/CashDepositAccountTest.java | 8 +- .../NationalBankAccountPayloadTest.java | 13 +-- .../accounts/NationalBankAccountTest.java | 8 +- .../trade_state/states/BaseState.java | 11 ++- .../trade_state/states/SellerState1.java | 2 +- .../accounts/PaymentAccountsController.java | 30 +++--- .../user/accounts/PaymentAccountsModel.java | 17 +--- .../user/accounts/PaymentAccountsView.java | 4 +- .../java/bisq/common/util/OptionalUtils.java | 16 +++ 16 files changed, 196 insertions(+), 157 deletions(-) create mode 100644 common/src/main/java/bisq/common/util/OptionalUtils.java diff --git a/account/src/main/java/bisq/account/AccountService.java b/account/src/main/java/bisq/account/AccountService.java index 21cb735604..56d5998de6 100644 --- a/account/src/main/java/bisq/account/AccountService.java +++ b/account/src/main/java/bisq/account/AccountService.java @@ -33,19 +33,17 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import javax.annotation.Nullable; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @Slf4j +@Getter public class AccountService implements PersistenceClient, Service { - @Getter + private final AccountStore persistableStore = new AccountStore(); - @Getter private final Persistence persistence; - @Getter - private transient final ObservableSet>> accounts = new ObservableSet<>(); + private final transient ObservableSet>> accounts = new ObservableSet<>(); public AccountService(PersistenceService persistenceService) { persistence = persistenceService.getOrCreatePersistence(this, DbSubDirectory.PRIVATE, persistableStore); @@ -91,9 +89,11 @@ public void addPaymentAccount(Account> account) { public void removePaymentAccount(Account> account) { getAccountByNameMap().remove(account.getAccountName()); accounts.remove(account); - if (account.equals(getSelectedAccount())) { - setSelectedAccount(null); - } + getSelectedAccount().ifPresent(s -> { + if (s.equals(account)) { + setSelectedAccount(null); + } + }); persist(); } @@ -105,9 +105,8 @@ public void removePaymentAccount(Account> account) return persistableStore.getSelectedAccount(); } - @Nullable - public Account> getSelectedAccount() { - return selectedAccountAsObservable().get(); + public Optional>> getSelectedAccount() { + return Optional.ofNullable(selectedAccountAsObservable().get()); } public void setSelectedAccount(Account> account) { @@ -117,7 +116,8 @@ public void setSelectedAccount(Account> account) { public List>> getMatchingAccounts(TradeProtocolType protocolTyp, String currencyCode) { - Set paymentMethods = new HashSet<>(PaymentMethodUtil.getPaymentRails(protocolTyp, currencyCode)); + Set paymentMethods = + new HashSet<>(PaymentMethodUtil.getPaymentRails(protocolTyp, currencyCode)); return persistableStore.getAccountByName().values().stream() .filter(account -> paymentMethods.contains(account.getPaymentMethod().getPaymentRail())) .filter(account -> account.getTradeCurrencyCodes().contains(currencyCode)) diff --git a/account/src/main/java/bisq/account/accounts/AchTransferAccountPayload.java b/account/src/main/java/bisq/account/accounts/AchTransferAccountPayload.java index d44186617c..5f2463b86d 100644 --- a/account/src/main/java/bisq/account/accounts/AchTransferAccountPayload.java +++ b/account/src/main/java/bisq/account/accounts/AchTransferAccountPayload.java @@ -7,23 +7,25 @@ import java.util.Optional; -@Getter +import static bisq.common.util.OptionalUtils.*; + @Slf4j @ToString +@Getter @EqualsAndHashCode(callSuper = true) public final class AchTransferAccountPayload extends BankAccountPayload { - private final String holderAddress; + private final Optional holderAddress; public AchTransferAccountPayload(String id, String paymentMethodName, String countryCode, - String holderName, - String bankName, - String branchId, - String accountNr, - String accountType, - String holderAddress) { + Optional holderName, + Optional bankName, + Optional branchId, + Optional accountNr, + Optional accountType, + Optional holderAddress) { super( id, paymentMethodName, @@ -36,7 +38,7 @@ public AchTransferAccountPayload(String id, null, null, null); - this.holderAddress = Optional.ofNullable(holderAddress).orElse(""); + this.holderAddress = normalize(holderAddress); } @Override @@ -50,7 +52,9 @@ private bisq.account.protobuf.AchTransferAccountPayload toAchTransferAccountPayl } private bisq.account.protobuf.AchTransferAccountPayload.Builder getAchTransferAccountPayloadBuilder(boolean serializeForHash) { - return bisq.account.protobuf.AchTransferAccountPayload.newBuilder().setHolderAddress(holderAddress); + var builder = bisq.account.protobuf.AchTransferAccountPayload.newBuilder(); + this.holderAddress.ifPresent(builder::setHolderAddress); + return builder; } public static AchTransferAccountPayload fromProto(bisq.account.protobuf.AccountPayload proto) { @@ -61,11 +65,11 @@ public static AchTransferAccountPayload fromProto(bisq.account.protobuf.AccountP proto.getId(), proto.getPaymentMethodName(), countryBasedPaymentAccountPayload.getCountryCode(), - bankAccountPayload.getHolderName(), - bankAccountPayload.getBankName().isEmpty() ? null : bankAccountPayload.getBankName(), - bankAccountPayload.getBranchId().isEmpty() ? null : bankAccountPayload.getBranchId(), - bankAccountPayload.getAccountNr().isEmpty() ? null : bankAccountPayload.getAccountNr(), - bankAccountPayload.getAccountType().isEmpty() ? null : bankAccountPayload.getAccountType(), - accountPayload.getHolderAddress().isEmpty() ? null : accountPayload.getHolderAddress()); + toOptional(bankAccountPayload.getHolderName()), + toOptional(bankAccountPayload.getBankName()), + toOptional(bankAccountPayload.getBranchId()), + toOptional(bankAccountPayload.getAccountNr()), + toOptional(bankAccountPayload.getAccountType()), + toOptional(accountPayload.getHolderAddress())); } -} +} \ No newline at end of file diff --git a/account/src/main/java/bisq/account/accounts/BankAccountPayload.java b/account/src/main/java/bisq/account/accounts/BankAccountPayload.java index e515978f53..3e2921dff1 100644 --- a/account/src/main/java/bisq/account/accounts/BankAccountPayload.java +++ b/account/src/main/java/bisq/account/accounts/BankAccountPayload.java @@ -8,65 +8,61 @@ import lombok.ToString; import lombok.extern.slf4j.Slf4j; -import javax.annotation.Nullable; import java.util.Optional; -//fixme (low prio) use Optional instead of Nullable +import static bisq.common.util.OptionalUtils.normalize; +import static bisq.common.validation.NetworkDataValidation.validateText; + @EqualsAndHashCode(callSuper = true) -@Setter @Getter +@Setter @ToString @Slf4j public abstract class BankAccountPayload extends CountryBasedAccountPayload { - protected String holderName; - protected String bankName; - protected String branchId; - protected String accountNr; - protected String accountType; - @Nullable - protected String holderTaxId; - protected String bankId; - @Nullable - protected String nationalAccountId; + + protected Optional holderName; + protected Optional bankName; + protected Optional branchId; + protected Optional accountNr; + protected Optional accountType; + protected Optional holderTaxId; + protected Optional bankId; + protected Optional nationalAccountId; protected BankAccountPayload(String id, String paymentMethodName, String countryCode, - String holderName, - @Nullable String bankName, - @Nullable String branchId, - @Nullable String accountNr, - @Nullable String accountType, - @Nullable String holderTaxId, - @Nullable String bankId, - @Nullable String nationalAccountId) { + Optional holderName, + Optional bankName, + Optional branchId, + Optional accountNr, + Optional accountType, + Optional holderTaxId, + Optional bankId, + Optional nationalAccountId) { super(id, paymentMethodName, countryCode); - this.holderName = Optional.ofNullable(holderName).orElse(""); - this.bankName = Optional.ofNullable(bankName).orElse(""); - this.branchId = Optional.ofNullable(branchId).orElse(""); - this.accountNr = Optional.ofNullable(accountNr).orElse(""); - this.accountType = Optional.ofNullable(accountType).orElse(""); - this.holderTaxId = holderTaxId; - this.bankId = Optional.ofNullable(bankId).orElse(""); - this.nationalAccountId = nationalAccountId; + this.holderName = normalize(holderName); + this.bankName = normalize(bankName); + this.branchId = normalize(branchId); + this.accountNr = normalize(accountNr); + this.accountType = normalize(accountType); + this.holderTaxId = normalize(holderTaxId); + this.bankId = normalize(bankId); + this.nationalAccountId = normalize(nationalAccountId); } @Override public void verify() { super.verify(); - NetworkDataValidation.validateText(holderName, 100); - NetworkDataValidation.validateText(bankName, 100); - NetworkDataValidation.validateText(branchId, 30); - NetworkDataValidation.validateText(accountNr, 30); - NetworkDataValidation.validateText(accountType, 20); - if (holderTaxId != null) { - NetworkDataValidation.validateText(holderTaxId, 50); - } - NetworkDataValidation.validateText(bankId, 50); - if (nationalAccountId != null) { - NetworkDataValidation.validateText(nationalAccountId, 50); - } + validateText(holderName, 100); + validateText(bankName, 100); + validateText(branchId, 30); + validateText(accountNr, 30); + validateText(accountType, 20); + validateText(holderTaxId, 50); + validateText(bankId, 50); + validateText(nationalAccountId, 50); } @Override @@ -80,16 +76,15 @@ protected bisq.account.protobuf.BankAccountPayload toBankAccountPayloadProto(boo } protected bisq.account.protobuf.BankAccountPayload.Builder getBankAccountPayloadBuilder(boolean serializeForHash) { - var builder = bisq.account.protobuf.BankAccountPayload.newBuilder() - .setHolderName(holderName) - .setBankName(bankName) - .setBranchId(branchId) - .setAccountNr(accountNr) - .setAccountType(accountType) - .setBranchId(branchId) - .setBankId(bankId); - Optional.ofNullable(holderTaxId).ifPresent(builder::setHolderTaxId); - Optional.ofNullable(nationalAccountId).ifPresent(builder::setNationalAccountId); + var builder = bisq.account.protobuf.BankAccountPayload.newBuilder(); + holderName.ifPresent(builder::setHolderName); + bankName.ifPresent(builder::setBankName); + branchId.ifPresent(builder::setBranchId); + accountNr.ifPresent(builder::setAccountNr); + accountType.ifPresent(builder::setAccountType); + holderTaxId.ifPresent(builder::setHolderTaxId); + bankId.ifPresent(builder::setBankId); + nationalAccountId.ifPresent(builder::setNationalAccountId); return builder; } diff --git a/account/src/main/java/bisq/account/accounts/CashDepositAccountPayload.java b/account/src/main/java/bisq/account/accounts/CashDepositAccountPayload.java index 62fdca282c..433411df9a 100644 --- a/account/src/main/java/bisq/account/accounts/CashDepositAccountPayload.java +++ b/account/src/main/java/bisq/account/accounts/CashDepositAccountPayload.java @@ -7,6 +7,10 @@ import lombok.ToString; import lombok.extern.slf4j.Slf4j; +import java.util.Optional; + +import static bisq.common.util.OptionalUtils.toOptional; + @Getter @Slf4j @ToString @@ -16,13 +20,11 @@ public final class CashDepositAccountPayload extends BankAccountPayload { private final String requirements; public CashDepositAccountPayload(String id, String paymentMethodName, String countryCode, - String holderName, String bankName, String branchId, - String accountNr, String accountType, - String holderTaxId, String bankId, - String nationalAccountId, String requirements) { - super(id, paymentMethodName, countryCode, - holderName, bankName, branchId, - accountNr, accountType, holderTaxId, + Optional holderName, Optional bankName, Optional branchId, + Optional accountNr, Optional accountType, + Optional holderTaxId, Optional bankId, + Optional nationalAccountId, String requirements) { + super(id, paymentMethodName, countryCode, holderName, bankName, branchId, accountNr, accountType, holderTaxId, bankId, nationalAccountId); this.requirements = requirements; @@ -35,6 +37,7 @@ public void verify() { NetworkDataValidation.validateText(requirements, 500); } + @Override protected bisq.account.protobuf.BankAccountPayload.Builder getBankAccountPayloadBuilder(boolean serializeForHash) { return super.getBankAccountPayloadBuilder(serializeForHash).setCashDepositAccountPayload( @@ -56,14 +59,14 @@ public static CashDepositAccountPayload fromProto(AccountPayload proto) { proto.getId(), proto.getPaymentMethodName(), countryBasedPaymentAccountPayload.getCountryCode(), - bankAccountPayload.getHolderName(), - bankAccountPayload.getBankName(), - bankAccountPayload.getBranchId(), - bankAccountPayload.getAccountNr(), - bankAccountPayload.getAccountType(), - bankAccountPayload.getHolderTaxId(), - bankAccountPayload.getBankId(), - bankAccountPayload.getNationalAccountId(), + toOptional(bankAccountPayload.getHolderName()), + toOptional(bankAccountPayload.getBankName()), + toOptional(bankAccountPayload.getBranchId()), + toOptional(bankAccountPayload.getAccountNr()), + toOptional(bankAccountPayload.getAccountType()), + toOptional(bankAccountPayload.getHolderTaxId()), + toOptional(bankAccountPayload.getBankId()), + toOptional(bankAccountPayload.getNationalAccountId()), bankAccountPayload.getCashDepositAccountPayload().getRequirements()); } } diff --git a/account/src/main/java/bisq/account/accounts/NationalBankAccountPayload.java b/account/src/main/java/bisq/account/accounts/NationalBankAccountPayload.java index e8bd3c168f..e42f5e23cb 100644 --- a/account/src/main/java/bisq/account/accounts/NationalBankAccountPayload.java +++ b/account/src/main/java/bisq/account/accounts/NationalBankAccountPayload.java @@ -6,7 +6,9 @@ import lombok.ToString; import lombok.extern.slf4j.Slf4j; -import javax.annotation.Nullable; +import java.util.Optional; + +import static bisq.common.util.OptionalUtils.toOptional; @Getter @Slf4j @@ -14,16 +16,23 @@ @EqualsAndHashCode(callSuper = true) public class NationalBankAccountPayload extends BankAccountPayload { - protected NationalBankAccountPayload(String id, String paymentMethodName, String countryCode, - String holderName, @Nullable String bankName, @Nullable String branchId, - @Nullable String accountNr, @Nullable String accountType, - @Nullable String holderTaxId, @Nullable String bankId, - @Nullable String nationalAccountId) { + protected NationalBankAccountPayload(String id, + String paymentMethodName, + String countryCode, + Optional holderName, + Optional bankName, + Optional branchId, + Optional accountNr, + Optional accountType, + Optional holderTaxId, + Optional bankId, + Optional nationalAccountId) { super(id, paymentMethodName, countryCode, holderName, bankName, branchId, accountNr, accountType, holderTaxId, bankId, nationalAccountId); } + @Override protected bisq.account.protobuf.BankAccountPayload.Builder getBankAccountPayloadBuilder(boolean serializeForHash) { return super.getBankAccountPayloadBuilder(serializeForHash).setNationalBankAccountPayload( @@ -45,13 +54,13 @@ public static NationalBankAccountPayload fromProto(AccountPayload proto) { proto.getId(), proto.getPaymentMethodName(), countryBasedPaymentAccountPayload.getCountryCode(), - bankAccountPayload.getHolderName(), - bankAccountPayload.getBankName().isEmpty() ? null : bankAccountPayload.getBankName(), - bankAccountPayload.getBranchId().isEmpty() ? null : bankAccountPayload.getBranchId(), - bankAccountPayload.getAccountNr().isEmpty() ? null : bankAccountPayload.getAccountNr(), - bankAccountPayload.getAccountType().isEmpty() ? null : bankAccountPayload.getAccountType(), - bankAccountPayload.getHolderTaxId().isEmpty() ? null : bankAccountPayload.getHolderTaxId(), - bankAccountPayload.getBankId().isEmpty() ? null : bankAccountPayload.getBankId(), - bankAccountPayload.getNationalAccountId().isEmpty() ? null : bankAccountPayload.getNationalAccountId()); + toOptional(bankAccountPayload.getHolderName()), + toOptional(bankAccountPayload.getBankName()), + toOptional(bankAccountPayload.getBranchId()), + toOptional(bankAccountPayload.getAccountNr()), + toOptional(bankAccountPayload.getAccountType()), + toOptional(bankAccountPayload.getHolderTaxId()), + toOptional(bankAccountPayload.getBankId()), + toOptional(bankAccountPayload.getNationalAccountId())); } } diff --git a/account/src/test/java/bisq/account/accounts/AchTransferAccountPayloadTest.java b/account/src/test/java/bisq/account/accounts/AchTransferAccountPayloadTest.java index 82e7fbf7d9..e1e40b7c9b 100644 --- a/account/src/test/java/bisq/account/accounts/AchTransferAccountPayloadTest.java +++ b/account/src/test/java/bisq/account/accounts/AchTransferAccountPayloadTest.java @@ -7,6 +7,8 @@ import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.assertEquals; @Slf4j @@ -40,11 +42,14 @@ class AchTransferAccountPayloadTest { private static final bisq.account.accounts.AchTransferAccountPayload PAYLOAD = new bisq.account.accounts.AchTransferAccountPayload( - "id", "paymentMethodName", "countryCode", "holderName", "bankName", "branchId", "accountNr", "accountType", "holderAddress" + "id", "paymentMethodName", "countryCode", + Optional.of("holderName"), Optional.of("bankName"), Optional.of("branchId"), + Optional.of("accountNr"), Optional.of("accountType"), Optional.of("holderAddress") ); private static final bisq.account.accounts.AchTransferAccountPayload PAYLOAD_OPTIONALS_NOT_SET = new bisq.account.accounts.AchTransferAccountPayload( - "id", "paymentMethodName", "countryCode", "holderName", null, null, null, null, null + "id", "paymentMethodName", "countryCode", + Optional.of("holderName"), null, null, null, null, null ); @Test diff --git a/account/src/test/java/bisq/account/accounts/AchTransferAccountTest.java b/account/src/test/java/bisq/account/accounts/AchTransferAccountTest.java index 8fd8260c2a..c65dc43d8f 100644 --- a/account/src/test/java/bisq/account/accounts/AchTransferAccountTest.java +++ b/account/src/test/java/bisq/account/accounts/AchTransferAccountTest.java @@ -11,6 +11,8 @@ import bisq.common.protobuf.Region; import org.junit.jupiter.api.Test; +import java.util.Optional; + import static java.lang.System.currentTimeMillis; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.data.Offset.offset; @@ -46,7 +48,9 @@ class AchTransferAccountTest { private static final AchTransferAccount ACCOUNT = new AchTransferAccount( "accountName", - new AchTransferAccountPayload("id", "ACH_TRANSFER", "countryCode", null, "bankName", null, null, null, null), + new AchTransferAccountPayload("id", "ACH_TRANSFER", + "countryCode", null, Optional.of("bankName"), + null, null, null, null), new bisq.common.locale.Country( "countryCode", "countryName", diff --git a/account/src/test/java/bisq/account/accounts/CashDepositAccountTest.java b/account/src/test/java/bisq/account/accounts/CashDepositAccountTest.java index 71a47efd2a..46fabd4b99 100644 --- a/account/src/test/java/bisq/account/accounts/CashDepositAccountTest.java +++ b/account/src/test/java/bisq/account/accounts/CashDepositAccountTest.java @@ -13,6 +13,8 @@ import bisq.common.protobuf.Region; import org.junit.jupiter.api.Test; +import java.util.Optional; + import static java.lang.System.currentTimeMillis; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.data.Offset.offset; @@ -58,9 +60,9 @@ class CashDepositAccountTest { "accountName", new bisq.account.accounts.CashDepositAccountPayload( "id", "CASH_DEPOSIT", "US", - "holderName", "bankName", "branchId", - "accountNr", "accountType", "holderTaxId", - "bankId", "nationalAccountId", "requirements"), + Optional.of("holderName"), Optional.of("bankName"), Optional.of("branchId"), + Optional.of("accountNr"), Optional.of("accountType"), Optional.of("holderTaxId"), + Optional.of("bankId"), Optional.of("nationalAccountId"), "requirements"), new bisq.common.locale.Country( "US", "countryName", diff --git a/account/src/test/java/bisq/account/accounts/NationalBankAccountPayloadTest.java b/account/src/test/java/bisq/account/accounts/NationalBankAccountPayloadTest.java index 8ce62651be..eb0ae14267 100644 --- a/account/src/test/java/bisq/account/accounts/NationalBankAccountPayloadTest.java +++ b/account/src/test/java/bisq/account/accounts/NationalBankAccountPayloadTest.java @@ -5,6 +5,8 @@ import bisq.account.protobuf.CountryBasedAccountPayload; import org.junit.jupiter.api.Test; +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.assertEquals; class NationalBankAccountPayloadTest { @@ -39,16 +41,15 @@ class NationalBankAccountPayloadTest { private static final bisq.account.accounts.NationalBankAccountPayload PAYLOAD = new bisq.account.accounts.NationalBankAccountPayload( "id", "paymentMethodName", "countryCode", - "holderName", "bankName", "branchId", - "accountNr", "accountType", "holderTaxId", - "bankId", "nationalAccountId" + Optional.of("holderName"), Optional.of("bankName"), Optional.of("branchId"), + Optional.of("accountNr"), Optional.of("accountType"), Optional.of("holderTaxId"), + Optional.of("bankId"), Optional.of("nationalAccountId") ); private static final bisq.account.accounts.NationalBankAccountPayload PAYLOAD_OPTIONALS_NOT_SET = new bisq.account.accounts.NationalBankAccountPayload( "id", "paymentMethodName", "countryCode", - "holderName", null, null, - null, null, null, - null, null); + Optional.of("holderName"), null, null, null, null, + null, null, null); @Test void testToProto() { diff --git a/account/src/test/java/bisq/account/accounts/NationalBankAccountTest.java b/account/src/test/java/bisq/account/accounts/NationalBankAccountTest.java index 91fcfa2808..51f3c6ad37 100644 --- a/account/src/test/java/bisq/account/accounts/NationalBankAccountTest.java +++ b/account/src/test/java/bisq/account/accounts/NationalBankAccountTest.java @@ -11,6 +11,8 @@ import bisq.common.protobuf.Region; import org.junit.jupiter.api.Test; +import java.util.Optional; + import static java.lang.System.currentTimeMillis; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.data.Offset.offset; @@ -48,9 +50,9 @@ class NationalBankAccountTest { "accountName", new NationalBankAccountPayload( "id", "NATIONAL_BANK", "countryCode", - null, "bankName", null, - null, null, null, - null, null), + Optional.empty(), Optional.of("bankName"), Optional.empty(), + Optional.empty(), Optional.empty(), Optional.empty(), + Optional.empty(), Optional.empty()), new bisq.common.locale.Country( "countryCode", "countryName", diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/BaseState.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/BaseState.java index 7a672530ea..dcd5474b26 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/BaseState.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/BaseState.java @@ -55,7 +55,9 @@ protected static abstract class Controller protected final BisqEasyOpenTradeChannelService channelService; protected final LeavePrivateChatManager leavePrivateChatManager; - protected Controller(ServiceProvider serviceProvider, BisqEasyTrade bisqEasyTrade, BisqEasyOpenTradeChannel channel) { + protected Controller(ServiceProvider serviceProvider, + BisqEasyTrade bisqEasyTrade, + BisqEasyOpenTradeChannel channel) { chatService = serviceProvider.getChatService(); bisqEasyTradeService = serviceProvider.getTradeService().getBisqEasyTradeService(); accountService = serviceProvider.getAccountService(); @@ -89,9 +91,10 @@ public void onDeactivate() { } protected Optional findUsersAccountData() { - return Optional.ofNullable(accountService.getSelectedAccount()).stream() - .filter(account -> account instanceof UserDefinedFiatAccount) - .map(account -> (UserDefinedFiatAccount) account) + return accountService + .getSelectedAccount().stream() + .filter(UserDefinedFiatAccount.class::isInstance) + .map(UserDefinedFiatAccount.class::cast) .map(account -> account.getAccountPayload().getAccountData()) .findFirst(); } diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/SellerState1.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/SellerState1.java index 961ed6a6f0..056587917b 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/SellerState1.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/bisq_easy/open_trades/trade_state/states/SellerState1.java @@ -134,7 +134,7 @@ private void onSelectAccount(UserDefinedFiatAccount account) { } private void maybeSelectFirstAccount() { - if (!model.getSortedAccounts().isEmpty() && accountService.getSelectedAccount() == null) { + if (!model.getSortedAccounts().isEmpty() && accountService.getSelectedAccount().isEmpty()) { accountService.setSelectedAccount(model.getSortedAccounts().getFirst()); } } diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsController.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsController.java index b841c0b183..bb5c29a897 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsController.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsController.java @@ -35,6 +35,7 @@ import org.fxmisc.easybind.Subscription; import java.util.Comparator; +import java.util.NoSuchElementException; import static bisq.bisq_easy.NavigationTarget.CREATE_BISQ_EASY_PAYMENT_ACCOUNT; import static com.google.common.base.Preconditions.*; @@ -107,37 +108,38 @@ void onCreateAccount() { } void onSaveAccount() { - checkNotNull(model.getSelectedAccount()); - String accountName = model.getSelectedAccount().getAccountName(); - String accountData = model.getAccountData(); - if (accountData != null) { + var selectedAccount = this.getSelectedAccount(); + String accountName = selectedAccount.getAccountName(); + model.getAccountData().ifPresent(accountData -> { checkArgument(accountData.length() <= UserDefinedFiatAccountPayload.MAX_DATA_LENGTH, "Account data must not be longer than 1000 characters"); UserDefinedFiatAccount newAccount = new UserDefinedFiatAccount(accountName, accountData); - accountService.removePaymentAccount(model.getSelectedAccount()); + accountService.removePaymentAccount(selectedAccount); accountService.addPaymentAccount(newAccount); accountService.setSelectedAccount(newAccount); - } + }); } void onDeleteAccount() { - checkNotNull(model.getSelectedAccount()); - accountService.removePaymentAccount(model.getSelectedAccount()); + accountService.removePaymentAccount(this.getSelectedAccount()); maybeSelectFirstAccount(); } private void updateButtonStates() { - model.setSaveButtonDisabled(model.getSelectedAccount() == null - || model.getAccountData() == null + model.setSaveButtonDisabled(model.getSelectedAccount().isEmpty() || model.getAccountData().isEmpty() - || model.getSelectedAccount() == null - || ((UserDefinedFiatAccount) model.getSelectedAccount()).getAccountPayload().getAccountData().equals(model.getAccountData())); + || ((UserDefinedFiatAccount) model.getSelectedAccount().get()).getAccountPayload().getAccountData() + .equals(model.getAccountData().get())); - model.setDeleteButtonDisabled(model.getSelectedAccount() == null); + model.setDeleteButtonDisabled(model.getSelectedAccount().isEmpty()); } private void maybeSelectFirstAccount() { - if (!model.getSortedAccounts().isEmpty() && accountService.getSelectedAccount() == null) { + if (!model.getSortedAccounts().isEmpty() && accountService.getSelectedAccount().isEmpty()) { accountService.setSelectedAccount(model.getSortedAccounts().getFirst()); } } + + private Account> getSelectedAccount() throws NoSuchElementException { + return model.getSelectedAccount().orElseThrow(() -> new NoSuchElementException("There is no account selected.")); + } } diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsModel.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsModel.java index 94366cd1ff..60b721b4dd 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsModel.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsModel.java @@ -27,8 +27,8 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import javax.annotation.Nullable; import java.util.Collection; +import java.util.Optional; @Getter @Slf4j @@ -43,9 +43,8 @@ public class PaymentAccountsModel implements Model { private final SortedList>> sortedAccounts = new SortedList<>(accounts); // selectedAccount - @Nullable - public Account> getSelectedAccount() { - return selectedAccount.get(); + public Optional>> getSelectedAccount() { + return Optional.ofNullable(selectedAccount.get()); } public ObjectProperty>> selectedAccountProperty() { @@ -56,10 +55,8 @@ public void setSelectedAccount(Account> selectedAc this.selectedAccount.set(selectedAccount); } - // accountData - @Nullable - public String getAccountData() { - return accountData.get(); + public Optional getAccountData() { + return Optional.ofNullable(accountData.get()); } public StringProperty accountDataProperty() { @@ -98,8 +95,4 @@ public void setDeleteButtonDisabled(boolean deleteButtonDisabled) { public void setAllAccounts(Collection>> collection) { accounts.setAll(collection); } - - public SortedList>> getSortedAccounts() { - return sortedAccounts; - } } diff --git a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsView.java b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsView.java index 8a3dcfb0ca..2fe7f806da 100644 --- a/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsView.java +++ b/apps/desktop/desktop/src/main/java/bisq/desktop/main/content/user/accounts/PaymentAccountsView.java @@ -128,7 +128,7 @@ protected void onViewAttached() { accountSelection.setOnChangeConfirmed(e -> { if (accountSelection.getSelectionModel().getSelectedItem() == null) { - accountSelection.getSelectionModel().select(model.getSelectedAccount()); + model.getSelectedAccount().ifPresent(accountSelection.getSelectionModel()::select); return; } controller.onSelectAccount(accountSelection.getSelectionModel().getSelectedItem()); @@ -158,7 +158,7 @@ protected void onViewAttached() { @Override protected void onViewDetached() { headline.textProperty().unbind(); - accountData.textProperty().unbindBidirectional(model.getAccountData()); + model.getAccountData().ifPresent(accountData.textProperty()::unbindBidirectional); saveButton.disableProperty().unbind(); deletedButton.disableProperty().unbind(); diff --git a/common/src/main/java/bisq/common/util/OptionalUtils.java b/common/src/main/java/bisq/common/util/OptionalUtils.java new file mode 100644 index 0000000000..a2c7e379c9 --- /dev/null +++ b/common/src/main/java/bisq/common/util/OptionalUtils.java @@ -0,0 +1,16 @@ +package bisq.common.util; + +import java.util.Optional; + +import static bisq.common.util.StringUtils.toNullIfEmpty; + +public class OptionalUtils { + + public static Optional toOptional(String value) { + return bisq.common.util.StringUtils.toOptional(value); + } + + public static Optional normalize(Optional optional) { + return Optional.ofNullable(optional).orElse(Optional.empty()); + } +}