/*
- * Copyright © 2021 Damyan Ivanov.
+ * Copyright © 2024 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
import java.util.regex.Pattern;
public class LedgerAccount {
+ private static final char ACCOUNT_DELIMITER = ':';
static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
private final LedgerAccount parent;
private long dbId;
}
@Nullable
public static String extractParentName(@NonNull String accName) {
- int colonPos = accName.lastIndexOf(':');
+ int colonPos = accName.lastIndexOf(ACCOUNT_DELIMITER);
if (colonPos < 0)
return null; // no parent account -- this is a top-level account
else
return accName.substring(0, colonPos);
}
+ public static boolean isParentOf(@NonNull String possibleParent, @NonNull String accountName) {
+ return accountName.startsWith(possibleParent + ':');
+ }
@NonNull
static public LedgerAccount fromDBO(AccountWithAmounts in, LedgerAccount parent) {
LedgerAccount res = new LedgerAccount(in.account.getName(), parent);
return res;
}
+ public static int determineLevel(String accName) {
+ int level = 0;
+ int delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER);
+ while (delimiterPosition >= 0) {
+ level++;
+ delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER, delimiterPosition + 1);
+ }
+ return level;
+ }
@Override
public int hashCode() {
return name.hashCode();
if (amounts != null)
amounts.clear();
}
- public boolean amountsExpanded() { return amountsExpanded; }
- public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
- public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
+ public boolean amountsExpanded() {return amountsExpanded;}
+ public void setAmountsExpanded(boolean flag) {amountsExpanded = flag;}
+ public void toggleAmountsExpanded() {amountsExpanded = !amountsExpanded;}
public void propagateAmountsTo(LedgerAccount acc) {
for (LedgerAmount a : amounts)
a.propagateToAccount(acc);
}
+ public boolean allAmountsAreZero() {
+ for (LedgerAmount a : amounts) {
+ if (a.getAmount() != 0)
+ return false;
+ }
+
+ return true;
+ }
public List<LedgerAmount> getAmounts() {
return amounts;
}