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.Matcher;
27 import java.util.regex.Pattern;
29 public class LedgerAccount {
30 static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
32 private String shortName;
34 private String parentName;
35 private boolean expanded;
36 private List<LedgerAmount> amounts;
37 private boolean hasSubAccounts;
38 private boolean amountsExpanded;
39 private WeakReference<MobileLedgerProfile> profileWeakReference;
41 public LedgerAccount(MobileLedgerProfile profile, String name) {
42 this.profileWeakReference = new WeakReference<>(profile);
46 public LedgerAccount(MobileLedgerProfile profile, String name, float amount) {
47 this.profileWeakReference = new WeakReference<>(profile);
50 this.amounts = new ArrayList<>();
51 this.addAmount(amount);
53 public @Nullable MobileLedgerProfile getProfile() {
54 return profileWeakReference.get();
57 public int hashCode() {
58 return name.hashCode();
61 public boolean equals(@Nullable Object obj) {
66 .equals(this.getClass()) && name.equals(((LedgerAccount) obj).getName());
68 // an account is visible if:
69 // - it has an expanded parent or is a top account
70 public boolean isVisible(List<LedgerAccount> list) {
71 for (LedgerAccount acc : list) {
72 if (acc.isParentOf(this)) {
73 if (!acc.isExpanded())
79 public boolean isParentOf(LedgerAccount potentialChild) {
80 return potentialChild.getName()
81 .startsWith(name + ":");
83 private void stripName() {
86 StringBuilder parentBuilder = new StringBuilder();
88 Matcher m = reHigherAccount.matcher(shortName);
91 parentBuilder.append(m.group(0));
92 shortName = m.replaceFirst("");
97 if (parentBuilder.length() > 0)
98 parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
102 public String getName() {
105 public void setName(String name) {
109 public void addAmount(float amount, @NonNull String currency) {
111 amounts = new ArrayList<>();
112 amounts.add(new LedgerAmount(amount, currency));
114 public void addAmount(float amount) {
115 this.addAmount(amount, "");
117 public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
118 public String getAmountsString() {
119 if ((amounts == null) || amounts.isEmpty())
122 StringBuilder builder = new StringBuilder();
123 for (LedgerAmount amount : amounts) {
124 String amt = amount.toString();
125 if (builder.length() > 0)
126 builder.append('\n');
130 return builder.toString();
132 public String getAmountsString(int limit) {
133 if ((amounts == null) || amounts.isEmpty())
137 StringBuilder builder = new StringBuilder();
138 for (LedgerAmount amount : amounts) {
139 String amt = amount.toString();
140 if (builder.length() > 0)
141 builder.append('\n');
144 if (included == limit)
148 return builder.toString();
150 public int getLevel() {
155 public String getShortName() {
159 public String getParentName() {
162 public boolean hasSubAccounts() {
163 return hasSubAccounts;
165 public void setHasSubAccounts(boolean hasSubAccounts) {
166 this.hasSubAccounts = hasSubAccounts;
168 public boolean isExpanded() {
171 public void setExpanded(boolean expanded) {
172 this.expanded = expanded;
174 public void toggleExpanded() {
175 expanded = !expanded;
177 public void removeAmounts() {
181 public boolean amountsExpanded() { return amountsExpanded; }
182 public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
183 public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }