import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
+import java.util.List;
public class LedgerTransaction {
private static final String DIGEST_TYPE = "SHA-256";
- public final Comparator<LedgerTransactionAccount> comparator =
- new Comparator<LedgerTransactionAccount>() {
- @Override
- public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount o2) {
- int res = o1.getAccountName()
- .compareTo(o2.getAccountName());
- if (res != 0)
- return res;
- res = o1.getCurrency()
- .compareTo(o2.getCurrency());
- if (res != 0)
- return res;
- res = o1.getComment()
- .compareTo(o2.getComment());
- if (res != 0)
- return res;
- return Float.compare(o1.getAmount(), o2.getAmount());
- }
- };
- private String profile;
- private Integer id;
+ public final Comparator<LedgerTransactionAccount> comparator = (o1, o2) -> {
+ int res = o1.getAccountName()
+ .compareTo(o2.getAccountName());
+ if (res != 0)
+ return res;
+ res = o1.getCurrency()
+ .compareTo(o2.getCurrency());
+ if (res != 0)
+ return res;
+ res = o1.getComment()
+ .compareTo(o2.getComment());
+ if (res != 0)
+ return res;
+ return Float.compare(o1.getAmount(), o2.getAmount());
+ };
+ private final String profile;
+ private final Integer id;
private SimpleDate date;
private String description;
private String comment;
- private ArrayList<LedgerTransactionAccount> accounts;
+ private final List<LedgerTransactionAccount> accounts;
private String dataHash;
private boolean dataLoaded;
public LedgerTransaction(Integer id, String dateString, String description)
dataLoaded = false;
}
public LedgerTransaction(Integer id, SimpleDate date, String description) {
- this(id, date, description, Data.profile.getValue());
+ this(id, date, description, Data.getProfile());
}
public LedgerTransaction(SimpleDate date, String description) {
this(null, date, description);
this.dataHash = null;
this.dataLoaded = false;
}
- public ArrayList<LedgerTransactionAccount> getAccounts() {
+ public List<LedgerTransactionAccount> getAccounts() {
return accounts;
}
public void addAccount(LedgerTransactionAccount item) {
}
@NonNull
public SimpleDate getDate() {
- loadData(App.getDatabase());
if (date == null)
throw new IllegalStateException("Transaction has no date");
return date;
return id;
}
protected void fillDataHash() {
+ loadData(App.getDatabase());
if (dataHash != null)
return;
try {
String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
}
}
- public boolean existsInDb(SQLiteDatabase db) {
- fillDataHash();
- try (Cursor c = db.rawQuery("SELECT 1 from transactions where data_hash = ?",
- new String[]{dataHash}))
- {
- boolean result = c.moveToFirst();
-// debug("db", String.format("Transaction %d (%s) %s", id, dataHash,
-// result ? "already present" : "not present"));
- return result;
- }
- }
- public void loadData(SQLiteDatabase db) {
+ public synchronized void loadData(SQLiteDatabase db) {
if (dataLoaded)
return;
return false;
}
+ public void markDataAsLoaded() {
+ dataLoaded = true;
+ }
}