2 * Copyright © 2019 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 java.util.ArrayList;
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
28 public class LedgerAccount {
29 static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
31 private String shortName;
33 private String parentName;
34 private boolean hiddenByStarToBe;
35 private boolean expanded;
36 private List<LedgerAmount> amounts;
37 private boolean hasSubAccounts;
38 private boolean amountsExpanded;
40 public LedgerAccount(String name) {
44 public LedgerAccount(String name, float amount) {
47 this.amounts = new ArrayList<LedgerAmount>();
48 this.addAmount(amount);
51 public int hashCode() {
52 return name.hashCode();
55 public boolean equals(@Nullable Object obj) {
56 if (obj == null) return false;
58 return obj.getClass().equals(this.getClass()) &&
59 name.equals(((LedgerAccount) obj).getName());
61 // an account is visible if:
62 // - it is starred (not hidden by a star)
63 // - and it has an expanded parent or is a top account
64 public boolean isVisible() {
65 if (level == 0) return true;
67 return isVisible(Data.accounts);
69 public boolean isVisible(List<LedgerAccount> list) {
70 for (LedgerAccount acc : list) {
71 if (acc.isParentOf(this)) {
72 if (!acc.isExpanded()) return false;
77 public boolean isParentOf(LedgerAccount potentialChild) {
78 return potentialChild.getName().startsWith(name + ":");
80 private void stripName() {
83 StringBuilder parentBuilder = new StringBuilder();
85 Matcher m = reHigherAccount.matcher(shortName);
88 parentBuilder.append(m.group(0));
89 shortName = m.replaceFirst("");
93 if (parentBuilder.length() > 0)
94 parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
95 else parentName = null;
97 public String getName() {
100 public void setName(String name) {
104 public void addAmount(float amount, String currency) {
105 if (amounts == null) amounts = new ArrayList<>();
106 amounts.add(new LedgerAmount(amount, currency));
108 public void addAmount(float amount) {
109 this.addAmount(amount, null);
111 public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
112 public String getAmountsString() {
113 if ((amounts == null) || amounts.isEmpty()) return "";
115 StringBuilder builder = new StringBuilder();
116 for (LedgerAmount amount : amounts) {
117 String amt = amount.toString();
118 if (builder.length() > 0) builder.append('\n');
122 return builder.toString();
124 public String getAmountsString(int limit) {
125 if ((amounts == null) || amounts.isEmpty()) return "";
128 StringBuilder builder = new StringBuilder();
129 for (LedgerAmount amount : amounts) {
130 String amt = amount.toString();
131 if (builder.length() > 0) builder.append('\n');
134 if (included == limit) break;
137 return builder.toString();
139 public int getLevel() {
144 public String getShortName() {
148 public String getParentName() {
151 public boolean isHiddenByStarToBe() {
152 return hiddenByStarToBe;
154 public void setHiddenByStarToBe(boolean hiddenByStarToBe) {
155 this.hiddenByStarToBe = hiddenByStarToBe;
157 public void toggleHiddenToBe() {
158 setHiddenByStarToBe(!hiddenByStarToBe);
160 public boolean hasSubAccounts() {
161 return hasSubAccounts;
163 public void setHasSubAccounts(boolean hasSubAccounts) {
164 this.hasSubAccounts = hasSubAccounts;
166 public boolean isExpanded() {
169 public void setExpanded(boolean expanded) {
170 this.expanded = expanded;
172 public void toggleExpanded() {
173 expanded = !expanded;
175 public void removeAmounts() {
176 if (amounts != null) amounts.clear();
178 public boolean amountsExpanded() { return amountsExpanded; }
179 public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
180 public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }