From: Damyan Ivanov Date: Sun, 24 Mar 2019 09:40:56 +0000 (+0200) Subject: LedgerAccount: override hashCode() and equals() to enable proper location in collections X-Git-Tag: v0.8.1~17 X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=commitdiff_plain;h=f782210abf37116018833b1d9858e087f497f59e;hp=131b3a80243191d4cebd2df70e8836d7dbebaaa8 LedgerAccount: override hashCode() and equals() to enable proper location in collections --- 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 f632bba9..c2ad2c2f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java @@ -23,6 +23,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; public class LedgerAccount { static Pattern reHigherAccount = Pattern.compile("^[^:]+:"); @@ -48,6 +49,17 @@ public class LedgerAccount { this.amounts = new ArrayList(); this.addAmount(amount); } + @Override + public int hashCode() { + return name.hashCode(); + } + @Override + public boolean equals(@Nullable Object obj) { + if (obj == null) return false; + + return obj.getClass().equals(this.getClass()) && + name.equals(((LedgerAccount) obj).getName()); + } // an account is visible if: // - it is starred (not hidden by a star) // - and it has an expanded parent or is a top account