aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/domain/Version.java
blob: 39e83c3c29e887125b55e5e970b5a659242b2098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package github.daneren2005.dsub.domain;

import androidx.annotation.NonNull;

import java.io.Serializable;

/**
 * Represents the version number of the Subsonic Android app.
 *
 * @author Sindre Mehus
 * @version $Revision: 1.3 $ $Date: 2006/01/20 21:25:16 $
 */
public class Version implements Comparable<Version>, Serializable {
	private int major;
	private int minor;
	private int beta;
	private int bugfix;

	public Version() {
		// For Kryo
	}

	/**
	 * Creates a new version instance by parsing the given string.
	 * @param version A string of the format "1.27", "1.27.2" or "1.27.beta3".
	 */
	public Version(String version) {
		String[] s = version.split("\\.");
		major = Integer.parseInt(s[0]);
		minor = Integer.parseInt(s[1]);

		if (s.length > 2) {
			if (s[2].contains("beta")) {
				beta = Integer.parseInt(s[2].replace("beta", ""));
			} else {
				bugfix = Integer.parseInt(s[2]);
			}
		}
	}

	public int getMajor() {
		return major;
	}

	public int getMinor() {
		return minor;
	}

	public String getVersion() {
		if (major == 1) {
			switch (minor) {
				case 0:
					return "3.8";
				case 1:
					return "3.9";
				case 2:
					return "4.0";
				case 3:
					return "4.1";
				case 4:
					return "4.2";
				case 5:
					return "4.3.1";
				case 6:
					return "4.5";
				case 7:
					return "4.6";
				case 8:
					return "4.7";
				case 9:
					return "4.8";
				case 10:
					return "4.9";
				case 11:
					return "5.1";
				case 12:
					return "5.2";
				case 13:
					return "5.3";
				case 14:
					return "6.0";
			}
		}
		return "";
	}

	/**
	 * Return whether this object is equal to another.
	 * @param o Object to compare to.
	 * @return Whether this object is equals to another.
	 */
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		final Version version = (Version) o;

		if (beta != version.beta) return false;
		if (bugfix != version.bugfix) return false;
		if (major != version.major) return false;
		return minor == version.minor;
	}

	/**
	 * Returns a hash code for this object.
	 * @return A hash code for this object.
	 */
	public int hashCode() {
		int result;
		result = major;
		result = 29 * result + minor;
		result = 29 * result + beta;
		result = 29 * result + bugfix;
		return result;
	}

	/**
	 * Returns a string representation of the form "1.27", "1.27.2" or "1.27.beta3".
	 * @return A string representation of the form "1.27", "1.27.2" or "1.27.beta3".
	 */
	@NonNull
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append(major).append('.').append(minor);
		if (beta != 0) {
			buf.append(".beta").append(beta);
		} else if (bugfix != 0) {
			buf.append('.').append(bugfix);
		}

		return buf.toString();
	}

	/**
	 * Compares this object with the specified object for order.
	 * @param version The object to compare to.
	 * @return A negative integer, zero, or a positive integer as this object is less than, equal to, or
	 * greater than the specified object.
	 */
	@Override
	public int compareTo(Version version) {
		if (major < version.major) {
			return -1;
		} else if (major > version.major) {
			return 1;
		}

		if (minor < version.minor) {
			return -1;
		} else if (minor > version.minor) {
			return 1;
		}

		if (bugfix < version.bugfix) {
			return -1;
		} else if (bugfix > version.bugfix) {
			return 1;
		}

		int thisBeta = beta == 0 ? Integer.MAX_VALUE : beta;
		int otherBeta = version.beta == 0 ? Integer.MAX_VALUE : version.beta;

		if (thisBeta < otherBeta) {
			return -1;
		} else if (thisBeta > otherBeta) {
			return 1;
		}

		return 0;
	}
}