]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/HledgerVersion.java
HledgerVersion: declare members final
[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 java.util.Locale;
24
25 public class HledgerVersion {
26     private final int major;
27     private final int minor;
28     private final int patch;
29     private final boolean isPre_1_20;
30     private final boolean hasPatch;
31     public HledgerVersion(int major, int minor) {
32         this.major = major;
33         this.minor = minor;
34         this.patch = 0;
35         this.isPre_1_20 = false;
36         this.hasPatch = false;
37     }
38     public HledgerVersion(int major, int minor, int patch) {
39         this.major = major;
40         this.minor = minor;
41         this.patch = patch;
42         this.isPre_1_20 = false;
43         this.hasPatch = true;
44     }
45     public HledgerVersion(boolean pre_1_20) {
46         if (!pre_1_20)
47             throw new IllegalArgumentException("pre_1_20 argument must be true");
48         this.major = this.minor = this.patch = 0;
49         this.isPre_1_20 = true;
50         this.hasPatch = false;
51     }
52     public HledgerVersion(HledgerVersion origin) {
53         this.major = origin.major;
54         this.minor = origin.minor;
55         this.isPre_1_20 = origin.isPre_1_20;
56         this.patch = origin.patch;
57         this.hasPatch = origin.hasPatch;
58     }
59     @Override
60     public boolean equals(@Nullable Object obj) {
61         if (obj == null)
62             return false;
63         if (!(obj instanceof HledgerVersion))
64             return false;
65         HledgerVersion that = (HledgerVersion) obj;
66
67         return (this.isPre_1_20 == that.isPre_1_20 && this.major == that.major &&
68                 this.minor == that.minor && this.patch == that.patch &&
69                 this.hasPatch == that.hasPatch);
70     }
71     public boolean isPre_1_20() {
72         return isPre_1_20;
73     }
74     public int getMajor() {
75         return major;
76     }
77     public int getMinor() {
78         return minor;
79     }
80     public int getPatch() {
81         return patch;
82     }
83     @NonNull
84     @Override
85     public String toString() {
86         if (isPre_1_20)
87             return "(before 1.20)";
88         return hasPatch ? String.format(Locale.ROOT, "%d.%d.%d", major, minor, patch)
89                         : String.format(Locale.ROOT, "%d.%d", major, minor);
90     }
91 }