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/?a=commitdiff_plain;h=f782210abf37116018833b1d9858e087f497f59e;p=mobile-ledger.git 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