]> git.ktnx.net Git - mobile-ledger-staging.git/blob - 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
1 /*
2  * Copyright © 2020 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.DiffUtil;
25
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.utils.Misc;
28
29 public class Currency {
30     public static final DiffUtil.ItemCallback<Currency> DIFF_CALLBACK =
31             new DiffUtil.ItemCallback<Currency>() {
32                 @Override
33                 public boolean areItemsTheSame(@NonNull Currency oldItem,
34                                                @NonNull Currency newItem) {
35                     return oldItem.id == newItem.id;
36                 }
37                 @Override
38                 public boolean areContentsTheSame(@NonNull Currency oldItem,
39                                                   @NonNull Currency newItem) {
40                     return oldItem.name.equals(newItem.name) &&
41                            oldItem.position.equals(newItem.position) &&
42                            (oldItem.hasGap == newItem.hasGap);
43                 }
44             };
45     private final int id;
46     private String name;
47     private Position position;
48     private boolean hasGap;
49     public Currency(int id, String name) {
50         this.id = id;
51         this.name = name;
52         position = Position.after;
53         hasGap = true;
54     }
55     public Currency(int id, String name, Position position, boolean hasGap) {
56         this.id = id;
57         this.name = name;
58         this.position = position;
59         this.hasGap = hasGap;
60     }
61     public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) {
62         SQLiteDatabase db = App.getDatabase();
63
64         try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
65             c.moveToNext();
66             this.id = c.getInt(0) + 1;
67         }
68         db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
69                 new Object[]{this.id, name, position.toString(), hasGap});
70
71         this.name = name;
72         this.position = position;
73         this.hasGap = hasGap;
74     }
75     public static Currency loadByName(String name) {
76         MobileLedgerProfile profile = Data.getProfile();
77         return profile.loadCurrencyByName(name);
78     }
79     static public boolean equal(Currency left, Currency right) {
80         if (left == null) {
81             return right == null;
82         }
83         else
84             return left.equals(right);
85     }
86     static public boolean equal(Currency left, String right) {
87         right = Misc.emptyIsNull(right);
88         if (left == null) {
89             return right == null;
90         }
91         else {
92             String leftName = Misc.emptyIsNull(left.getName());
93             if (leftName == null) {
94                 return right == null;
95             }
96             else
97                 return leftName.equals(right);
98         }
99     }
100     public int getId() {
101         return id;
102     }
103     public String getName() {
104         return name;
105     }
106     public void setName(String name) {
107         this.name = name;
108     }
109     public Position getPosition() {
110         return position;
111     }
112     public void setPosition(Position position) {
113         this.position = position;
114     }
115     public boolean hasGap() {
116         return hasGap;
117     }
118     public void setHasGap(boolean hasGap) {
119         this.hasGap = hasGap;
120     }
121     public enum Position {
122         before(-1), after(1), unknown(0), none(-2);
123         Position(int value) {
124         }
125         static Position valueOf(int value) {
126             switch (value) {
127                 case -1:
128                     return before;
129                 case +1:
130                     return after;
131                 case 0:
132                     return unknown;
133                 case -2:
134                     return none;
135                 default:
136                     throw new IllegalStateException(String.format("Unexpected value (%d)", value));
137             }
138         }
139     }
140 }