Skip to content

Commit

Permalink
Merge pull request #2947 from bsc7/feature/1631/replace-nullable-with…
Browse files Browse the repository at this point in the history
…-optional

Refact: m/account: Nullable -> Optional #1631
  • Loading branch information
HenrikJannsen authored Oct 23, 2024
2 parents ea2fbd8 + 190dcaa commit 7e782fe
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 157 deletions.
24 changes: 12 additions & 12 deletions account/src/main/java/bisq/account/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountStore>, Service {
@Getter

private final AccountStore persistableStore = new AccountStore();
@Getter
private final Persistence<AccountStore> persistence;
@Getter
private transient final ObservableSet<Account<?, ? extends PaymentMethod<?>>> accounts = new ObservableSet<>();
private final transient ObservableSet<Account<?, ? extends PaymentMethod<?>>> accounts = new ObservableSet<>();

public AccountService(PersistenceService persistenceService) {
persistence = persistenceService.getOrCreatePersistence(this, DbSubDirectory.PRIVATE, persistableStore);
Expand Down Expand Up @@ -91,9 +89,11 @@ public void addPaymentAccount(Account<?, ? extends PaymentMethod<?>> account) {
public void removePaymentAccount(Account<?, ? extends PaymentMethod<?>> account) {
getAccountByNameMap().remove(account.getAccountName());
accounts.remove(account);
if (account.equals(getSelectedAccount())) {
setSelectedAccount(null);
}
getSelectedAccount().ifPresent(s -> {
if (s.equals(account)) {
setSelectedAccount(null);
}
});
persist();
}

Expand All @@ -105,9 +105,8 @@ public void removePaymentAccount(Account<?, ? extends PaymentMethod<?>> account)
return persistableStore.getSelectedAccount();
}

@Nullable
public Account<?, ? extends PaymentMethod<?>> getSelectedAccount() {
return selectedAccountAsObservable().get();
public Optional<Account<?, ? extends PaymentMethod<?>>> getSelectedAccount() {
return Optional.ofNullable(selectedAccountAsObservable().get());
}

public void setSelectedAccount(Account<?, ? extends PaymentMethod<?>> account) {
Expand All @@ -117,7 +116,8 @@ public void setSelectedAccount(Account<?, ? extends PaymentMethod<?>> account) {

public List<Account<?, ? extends PaymentMethod<?>>> getMatchingAccounts(TradeProtocolType protocolTyp,
String currencyCode) {
Set<? extends PaymentRail> paymentMethods = new HashSet<>(PaymentMethodUtil.getPaymentRails(protocolTyp, currencyCode));
Set<? extends PaymentRail> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> holderAddress;

public AchTransferAccountPayload(String id,
String paymentMethodName,
String countryCode,
String holderName,
String bankName,
String branchId,
String accountNr,
String accountType,
String holderAddress) {
Optional<String> holderName,
Optional<String> bankName,
Optional<String> branchId,
Optional<String> accountNr,
Optional<String> accountType,
Optional<String> holderAddress) {
super(
id,
paymentMethodName,
Expand All @@ -36,7 +38,7 @@ public AchTransferAccountPayload(String id,
null,
null,
null);
this.holderAddress = Optional.ofNullable(holderAddress).orElse("");
this.holderAddress = normalize(holderAddress);
}

@Override
Expand All @@ -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) {
Expand All @@ -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()));
}
}
}
97 changes: 46 additions & 51 deletions account/src/main/java/bisq/account/accounts/BankAccountPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> holderName;
protected Optional<String> bankName;
protected Optional<String> branchId;
protected Optional<String> accountNr;
protected Optional<String> accountType;
protected Optional<String> holderTaxId;
protected Optional<String> bankId;
protected Optional<String> 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<String> holderName,
Optional<String> bankName,
Optional<String> branchId,
Optional<String> accountNr,
Optional<String> accountType,
Optional<String> holderTaxId,
Optional<String> bankId,
Optional<String> 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
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> holderName, Optional<String> bankName, Optional<String> branchId,
Optional<String> accountNr, Optional<String> accountType,
Optional<String> holderTaxId, Optional<String> bankId,
Optional<String> nationalAccountId, String requirements) {
super(id, paymentMethodName, countryCode, holderName, bankName, branchId, accountNr, accountType, holderTaxId,
bankId, nationalAccountId);
this.requirements = requirements;

Expand All @@ -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(
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,33 @@
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
@ToString
@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<String> holderName,
Optional<String> bankName,
Optional<String> branchId,
Optional<String> accountNr,
Optional<String> accountType,
Optional<String> holderTaxId,
Optional<String> bankId,
Optional<String> 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(
Expand All @@ -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()));
}
}
Loading

0 comments on commit 7e782fe

Please sign in to comment.