X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FLedgerAccount.java;h=1be684cf1b0d1bcb700485384a3a068963576610;hb=64413271ef4ed943ae29e9cf9115c1bb77053278;hp=1e2a1a4f6834ed306d1ffb948715f26379c5df7f;hpb=5df10dc0b58df4d4be4e9ab34f1e0f477ca46766;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java index 1e2a1a4f..1be684cf 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java @@ -29,6 +29,7 @@ import java.util.List; 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; @@ -51,12 +52,15 @@ public class LedgerAccount { } @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); @@ -73,6 +77,15 @@ public class LedgerAccount { 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();