]> git.ktnx.net Git - mobile-ledger-staging.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/Currency.java
fix many lint errors/warnings
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / model / Currency.java
index e32513f2170a9ecba710631ea86a168df6f14ef7..f332f001667cf1e83cff65571271dcb141847c2c 100644 (file)
@@ -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
@@ -42,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;
@@ -60,51 +60,22 @@ 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.profile.getValue();
+        MobileLedgerProfile profile = Data.getProfile();
         return profile.loadCurrencyByName(name);
     }
-    public int getId() {
-        return id;
-    }
-    public String getName() {
-        return name;
-    }
-    public void setName(String name) {
-        this.name = name;
-    }
-    public Position getPosition() {
-        return position;
-    }
-    public void setPosition(Position position) {
-        this.position = position;
-    }
-    public boolean hasGap() {
-        return hasGap;
-    }
-    public void setHasGap(boolean hasGap) {
-        this.hasGap = hasGap;
-    }
     static public boolean equal(Currency left, Currency right) {
         if (left == null) {
             return right == null;
@@ -126,11 +97,30 @@ public class Currency {
                 return leftName.equals(right);
         }
     }
+    public int getId() {
+        return id;
+    }
+    public String getName() {
+        return name;
+    }
+    public void setName(String name) {
+        this.name = name;
+    }
+    public Position getPosition() {
+        return position;
+    }
+    public void setPosition(Position position) {
+        this.position = position;
+    }
+    public boolean hasGap() {
+        return hasGap;
+    }
+    public void setHasGap(boolean hasGap) {
+        this.hasGap = hasGap;
+    }
     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) {