From acdedb28266ec0bde0a3b6775837a70455c339fe Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 26 Mar 2021 19:19:28 +0200 Subject: [PATCH] specialized sub-classes of AccountListItem --- .../mobileledger/model/AccountListItem.java | 39 ++++++++++++------- .../net/ktnx/mobileledger/ui/MainModel.java | 4 +- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java b/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java index 5fd95171..a96a9a3e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -22,25 +22,34 @@ import androidx.annotation.NonNull; import org.jetbrains.annotations.NotNull; public class AccountListItem { - private final Type type; - private LedgerAccount account; - public AccountListItem(@NotNull LedgerAccount account) { - this.type = Type.ACCOUNT; - this.account = account; - } - public AccountListItem() { - this.type = Type.HEADER; - } + private AccountListItem() {} @NonNull public Type getType() { - return type; + if (this instanceof Account) + return Type.ACCOUNT; + else if (this instanceof Header) + return Type.HEADER; + else + throw new RuntimeException("Unsupported sub-class " + this); } @NotNull public LedgerAccount getAccount() { - if (type != Type.ACCOUNT) - throw new IllegalStateException( - String.format("Item type is not %s, but %s", Type.ACCOUNT, type)); - return account; + if (this instanceof Account) + return ((Account) this).account; + + throw new IllegalStateException(String.format("Item type is not Account, but %s", this)); } public enum Type {ACCOUNT, HEADER} + + public static class Account extends AccountListItem { + private final LedgerAccount account; + public Account(@NotNull LedgerAccount account) { + this.account = account; + } + } + + public static class Header extends AccountListItem { + public Header() { + } + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java index 59cad326..845242bf 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java @@ -363,7 +363,7 @@ public class MainModel extends ViewModel { Logger.debug("dFilter", "waiting for synchronized block"); Logger.debug("dFilter", String.format(Locale.US, "entered synchronized block (about to examine %d accounts)", list.size())); - newDisplayed.add(new AccountListItem()); // header + newDisplayed.add(new AccountListItem.Header()); // header int count = 0; for (LedgerAccount a : list) { @@ -371,7 +371,7 @@ public class MainModel extends ViewModel { return; if (a.isVisible()) { - newDisplayed.add(new AccountListItem(a)); + newDisplayed.add(new AccountListItem.Account(a)); count++; } } -- 2.39.2