Skip to content

Commit

Permalink
Merge pull request #6028 from psikomonkie/cache-finances-balance
Browse files Browse the repository at this point in the history
Cache finances balance
  • Loading branch information
HammerGS authored Feb 15, 2025
2 parents d59798e + 0087e25 commit c2fb924
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
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

0 comments on commit c2fb924

Please sign in to comment.