]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/HledgerVersion.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / HledgerVersion.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 androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22
23 import net.ktnx.mobileledger.json.API;
24
25 import java.util.Locale;
26
27 public class HledgerVersion {
28     private final int major;
29     private final int minor;
30     private final int patch;
31     private final boolean isPre_1_20_1;
32     private final boolean hasPatch;
33     public HledgerVersion(int major, int minor) {
34         this.major = major;
35         this.minor = minor;
36         this.patch = 0;
37         this.isPre_1_20_1 = false;
38         this.hasPatch = false;
39     }
40     public HledgerVersion(int major, int minor, int patch) {
41         this.major = major;
42         this.minor = minor;
43         this.patch = patch;
44         this.isPre_1_20_1 = false;
45         this.hasPatch = true;
46     }
47     public HledgerVersion(boolean pre_1_20_1) {
48         if (!pre_1_20_1)
49             throw new IllegalArgumentException("pre_1_20_1 argument must be true");
50         this.major = this.minor = this.patch = 0;
51         this.isPre_1_20_1 = true;
52         this.hasPatch = false;
53     }
54     public HledgerVersion(HledgerVersion origin) {
55         this.major = origin.major;
56         this.minor = origin.minor;
57         this.isPre_1_20_1 = origin.isPre_1_20_1;
58         this.patch = origin.patch;
59         this.hasPatch = origin.hasPatch;
60     }
61     @Override
62     public boolean equals(@Nullable Object obj) {
63         if (obj == null)
64             return false;
65         if (!(obj instanceof HledgerVersion))
66             return false;
67         HledgerVersion that = (HledgerVersion) obj;
68
69         return (this.isPre_1_20_1 == that.isPre_1_20_1 && this.major == that.major &&
70                 this.minor == that.minor && this.patch == that.patch &&
71                 this.hasPatch == that.hasPatch);
72     }
73     public boolean isPre_1_20_1() {
74         return isPre_1_20_1;
75     }
76     public int getMajor() {
77         return major;
78     }
79     public int getMinor() {
80         return minor;
81     }
82     public int getPatch() {
83         return patch;
84     }
85     @NonNull
86     @Override
87     public String toString() {
88         if (isPre_1_20_1)
89             return "(before 1.20)";
90         return hasPatch ? String.format(Locale.ROOT, "%d.%d.%d", major, minor, patch)
91                         : String.format(Locale.ROOT, "%d.%d", major, minor);
92     }
93     public boolean atLeast(int major, int minor) {
94         return ((this.major == major) && (this.minor >= minor)) || (this.major > major);
95     }
96     @org.jetbrains.annotations.Nullable
97     public API getSuitableApiVersion() {
98         if (isPre_1_20_1)
99             return null;
100
101         return API.v1_19_1;
102     }
103 }