/*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 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
package net.ktnx.mobileledger.json;
import com.fasterxml.jackson.databind.MappingIterator;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.ObjectReader;
+
+import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
+import net.ktnx.mobileledger.model.LedgerAccount;
import java.io.IOException;
import java.io.InputStream;
+import java.util.HashMap;
-public class AccountListParser {
-
- private final MappingIterator<ParsedLedgerAccount> iter;
+import static net.ktnx.mobileledger.utils.Logger.debug;
- public AccountListParser(InputStream input) throws IOException {
- ObjectMapper mapper = new ObjectMapper();
- ObjectReader reader = mapper.readerFor(ParsedLedgerAccount.class);
+abstract public class AccountListParser {
+ protected MappingIterator<net.ktnx.mobileledger.json.ParsedLedgerAccount> iterator;
+ public static AccountListParser forApiVersion(API version, InputStream input)
+ throws IOException {
+ switch (version) {
+ case v1_14:
+ return new net.ktnx.mobileledger.json.v1_14.AccountListParser(input);
+ case v1_15:
+ return new net.ktnx.mobileledger.json.v1_15.AccountListParser(input);
+ case v1_19_1:
+ return new net.ktnx.mobileledger.json.v1_19_1.AccountListParser(input);
+ default:
+ throw new RuntimeException("Unsupported version " + version.toString());
+ }
- iter = reader.readValues(input);
}
- public ParsedLedgerAccount nextAccount() throws IOException {
- return iter.hasNext() ? iter.next() : null;
+ public abstract API getApiVersion();
+ public LedgerAccount nextAccount(RetrieveTransactionsTask task,
+ HashMap<String, LedgerAccount> map) {
+ if (!iterator.hasNext())
+ return null;
+
+ LedgerAccount next = iterator.next()
+ .toLedgerAccount(task, map);
+
+ if (next.getName()
+ .equalsIgnoreCase("root"))
+ return nextAccount(task, map);
+
+ debug("accounts", String.format("Got account '%s' [%s]", next.getName(),
+ getApiVersion().getDescription()));
+ return next;
}
+
}