Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache finances balance #6028

Merged
merged 5 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions MekHQ/src/mekhq/campaign/finances/Finances.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public class Finances {
private int failedCollateral;
private LocalDate wentIntoDebt;

private Money balance;
private int transactionSize = -1;

public Finances() {
transactions = new ArrayList<>();
loans = new ArrayList<>();
Expand Down Expand Up @@ -126,9 +129,39 @@ public void setWentIntoDebt(final @Nullable LocalDate wentIntoDebt) {
this.wentIntoDebt = wentIntoDebt;
}

/**
* Current campaign balance. Will calculate the current campaign balance
* based on the campaign's transactions. Cached using the current transaction count.
* @see #clearCachedBalance()
* @return current balance (Money)
*/
public Money getBalance() {
Money balance = Money.zero();
return balance.plus(transactions.stream().map(Transaction::getAmount).collect(Collectors.toList()));
Money newBalance = Money.zero();

// If our # of transactions matches what we expect, and the balance isn't null, we should return the cached balance:
if (transactions.size() == transactionSize && balance != null) {
return newBalance.plus(balance);
}

// Recalculate the current balance
newBalance = newBalance.plus(transactions.stream().map(Transaction::getAmount).collect(Collectors.toList()));

// Update our cached balance & note the transactions size.
balance = Money.zero();
balance = balance.plus(newBalance);
transactionSize = transactions.size();

return newBalance;
}

/**
* Next time getBalance() is called force it to recalculate the current balance
* Should be called if transactions are modified or deleted. Should not be needed
* when adding new transactions - the balance should automatically recalculate.
* @see #getBalance()
*/
public void clearCachedBalance() {
transactionSize = -1;
}

public Money getLoanBalance() {
Expand Down Expand Up @@ -246,6 +279,7 @@ public void newFiscalYear(final Campaign campaign) {

Money carryover = getBalance();
transactions = new ArrayList<>();
clearCachedBalance();

credit(
TransactionType.FINANCIAL_TERM_END_CARRYOVER,
Expand Down
2 changes: 2 additions & 0 deletions MekHQ/src/mekhq/gui/adapter/FinanceTableMouseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ public void actionPerformed(ActionEvent action) {
if (command.equalsIgnoreCase("DELETE")) {
gui.getCampaign().addReport(transaction.voidTransaction());
financeModel.deleteTransaction(row);
gui.getCampaign().getFinances().clearCachedBalance();
MekHQ.triggerEvent(new TransactionVoidedEvent(transaction));
} else if (command.contains("EDIT")) {
EditTransactionDialog dialog = new EditTransactionDialog(gui.getFrame(), transaction, true);
dialog.setVisible(true);
if (!transaction.equals(dialog.getOldTransaction())) {
financeModel.setTransaction(row, transaction);
gui.getCampaign().getFinances().clearCachedBalance();
MekHQ.triggerEvent(new TransactionChangedEvent(dialog.getOldTransaction(), transaction));
gui.getCampaign().addReport(transaction.updateTransaction(dialog.getOldTransaction()));
}
Expand Down
Loading