2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.model;
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
23 import java.lang.ref.WeakReference;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Pattern;
28 public class LedgerAccount {
29 static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
31 private String shortName;
33 private final LedgerAccount parent;
34 private boolean expanded;
35 private List<LedgerAmount> amounts;
36 private boolean hasSubAccounts;
37 private boolean amountsExpanded;
38 private final WeakReference<MobileLedgerProfile> profileWeakReference;
40 public LedgerAccount(MobileLedgerProfile profile, String name, @Nullable LedgerAccount parent) {
41 this.profileWeakReference = new WeakReference<>(profile);
43 if (parent != null && !name.startsWith(parent.getName() + ":"))
44 throw new IllegalStateException(
45 String.format("Account name '%s' doesn't match parent account '%s'", name,
50 public static String extractParentName(@NonNull String accName) {
51 int colonPos = accName.lastIndexOf(':');
53 return null; // no parent account -- this is a top-level account
55 return accName.substring(0, colonPos);
58 MobileLedgerProfile getProfile() {
59 return profileWeakReference.get();
62 public int hashCode() {
63 return name.hashCode();
66 public boolean equals(@Nullable Object obj) {
70 if (!(obj instanceof LedgerAccount))
73 LedgerAccount acc = (LedgerAccount) obj;
74 if (!name.equals(acc.name))
77 if (!getAmountsString().equals(acc.getAmountsString()))
80 return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
82 // an account is visible if:
83 // - it has an expanded visible parent or is a top account
84 public boolean isVisible() {
88 return (parent.isExpanded() && parent.isVisible());
90 public boolean isParentOf(LedgerAccount potentialChild) {
91 return potentialChild.getName()
92 .startsWith(name + ":");
94 private void stripName() {
100 level = parent.level + 1;
101 shortName = name.substring(parent.getName()
105 public String getName() {
108 public void setName(String name) {
112 public void addAmount(float amount, @NonNull String currency) {
114 amounts = new ArrayList<>();
115 amounts.add(new LedgerAmount(amount, currency));
117 public void addAmount(float amount) {
118 this.addAmount(amount, "");
120 public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
121 public String getAmountsString() {
122 if ((amounts == null) || amounts.isEmpty())
125 StringBuilder builder = new StringBuilder();
126 for (LedgerAmount amount : amounts) {
127 String amt = amount.toString();
128 if (builder.length() > 0)
129 builder.append('\n');
133 return builder.toString();
135 public String getAmountsString(int limit) {
136 if ((amounts == null) || amounts.isEmpty())
140 StringBuilder builder = new StringBuilder();
141 for (LedgerAmount amount : amounts) {
142 String amt = amount.toString();
143 if (builder.length() > 0)
144 builder.append('\n');
147 if (included == limit)
151 return builder.toString();
153 public int getLevel() {
158 public String getShortName() {
162 public String getParentName() {
163 return (parent == null) ? null : parent.getName();
165 public boolean hasSubAccounts() {
166 return hasSubAccounts;
168 public void setHasSubAccounts(boolean hasSubAccounts) {
169 this.hasSubAccounts = hasSubAccounts;
171 public boolean isExpanded() {
174 public void setExpanded(boolean expanded) {
175 this.expanded = expanded;
177 public void toggleExpanded() {
178 expanded = !expanded;
180 public void removeAmounts() {
184 public boolean amountsExpanded() { return amountsExpanded; }
185 public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
186 public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
188 public void propagateAmountsTo(LedgerAccount acc) {
189 for (LedgerAmount a : amounts)
190 a.propagateToAccount(acc);
192 public List<LedgerAmount> getAmounts() {