2 * Copyright © 2019 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.
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.
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/>.
18 package net.ktnx.mobileledger.model;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.DiffUtil;
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.utils.Misc;
29 public class Currency {
30 public static final DiffUtil.ItemCallback<Currency> DIFF_CALLBACK =
31 new DiffUtil.ItemCallback<Currency>() {
33 public boolean areItemsTheSame(@NonNull Currency oldItem,
34 @NonNull Currency newItem) {
35 return oldItem.id == newItem.id;
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);
47 private Position position;
48 private boolean hasGap;
49 public Currency(int id, String name) {
52 position = Position.after;
55 public Currency(int id, String name, Position position, boolean hasGap) {
58 this.position = position;
61 public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) {
62 SQLiteDatabase db = App.getDatabase();
66 throw new RuntimeException("Giving up getting next ID after 10 attempts");
68 try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
70 int nextId = c.getInt(0) + 1;
71 db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
72 new Object[]{nextId, name, position.toString(), hasGap});
80 this.position = position;
83 public static Currency loadByName(String name) {
84 MobileLedgerProfile profile = Data.profile.getValue();
85 return profile.loadCurrencyByName(name);
90 public String getName() {
93 public void setName(String name) {
96 public Position getPosition() {
99 public void setPosition(Position position) {
100 this.position = position;
102 public boolean hasGap() {
105 public void setHasGap(boolean hasGap) {
106 this.hasGap = hasGap;
108 static public boolean equal(Currency left, Currency right) {
110 return right == null;
113 return left.equals(right);
115 static public boolean equal(Currency left, String right) {
116 right = Misc.emptyIsNull(right);
118 return right == null;
121 String leftName = Misc.emptyIsNull(left.getName());
122 if (leftName == null) {
123 return right == null;
126 return leftName.equals(right);
129 public enum Position {
130 before(-1), after(1), unknown(0), none(-2);
132 Position(int value) {
135 static Position valueOf(int value) {
146 throw new IllegalStateException(String.format("Unexpected value (%d)", value));