X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FCurrency.java;h=f332f001667cf1e83cff65571271dcb141847c2c;hb=20c03b7a5eb152d42fbbe9ecbaae27530563b398;hp=a4854c05219c146608326b1acf8f2556f5aebd25;hpb=5545ddea3574103c2a7eea552fff0d43a0587fac;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Currency.java b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java index a4854c05..f332f001 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Currency.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java @@ -1,5 +1,5 @@ /* - * 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 @@ -24,6 +24,7 @@ import androidx.annotation.NonNull; import androidx.recyclerview.widget.DiffUtil; import net.ktnx.mobileledger.App; +import net.ktnx.mobileledger.utils.Misc; public class Currency { public static final DiffUtil.ItemCallback DIFF_CALLBACK = @@ -41,7 +42,7 @@ public class Currency { (oldItem.hasGap == newItem.hasGap); } }; - private int id; + private final int id; private String name; private Position position; private boolean hasGap; @@ -59,26 +60,43 @@ public class Currency { } public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) { SQLiteDatabase db = App.getDatabase(); - int attempts = 0; - while (true) { - if (++attempts > 10) - throw new RuntimeException("Giving up getting next ID after 10 attempts"); - try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) { - c.moveToNext(); - int nextId = c.getInt(0) + 1; - db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)", - new Object[]{nextId, name, position.toString(), hasGap}); - - this.id = nextId; - break; - } + try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) { + c.moveToNext(); + this.id = c.getInt(0) + 1; } + db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)", + new Object[]{this.id, name, position.toString(), hasGap}); this.name = name; this.position = position; this.hasGap = hasGap; } + public static Currency loadByName(String name) { + MobileLedgerProfile profile = Data.getProfile(); + return profile.loadCurrencyByName(name); + } + static public boolean equal(Currency left, Currency right) { + if (left == null) { + return right == null; + } + else + return left.equals(right); + } + static public boolean equal(Currency left, String right) { + right = Misc.emptyIsNull(right); + if (left == null) { + return right == null; + } + else { + String leftName = Misc.emptyIsNull(left.getName()); + if (leftName == null) { + return right == null; + } + else + return leftName.equals(right); + } + } public int getId() { return id; } @@ -102,9 +120,7 @@ public class Currency { } public enum Position { before(-1), after(1), unknown(0), none(-2); - private int value; Position(int value) { - this.value = value; } static Position valueOf(int value) { switch (value) {