]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
running totals when filtering transactions by account
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerAccount.java
index c76f2d564970193e29b37740f88a5ec86857c5d4..1be684cf1b0d1bcb700485384a3a068963576610 100644 (file)
@@ -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();
@@ -212,6 +225,21 @@ public class LedgerAccount {
 
         return dbo;
     }
+    @NonNull
+    public AccountWithAmounts toDBOWithAmounts() {
+        AccountWithAmounts dbo = new AccountWithAmounts();
+        dbo.account = toDBO();
+
+        dbo.amounts = new ArrayList<>();
+        for (LedgerAmount amt : getAmounts()) {
+            AccountValue val = new AccountValue();
+            val.setCurrency(amt.getCurrency());
+            val.setValue(amt.getAmount());
+            dbo.amounts.add(val);
+        }
+
+        return dbo;
+    }
     public long getId() {
         return dbId;
     }