forked from ipfs-shipyard/java-ipfs-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersion.java
More file actions
52 lines (44 loc) · 1.69 KB
/
Version.java
File metadata and controls
52 lines (44 loc) · 1.69 KB
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
package io.ipfs.api;
public class Version implements Comparable<Version> {
public final int major, minor, patch;
public final String suffix;
public Version(int major, int minor, int patch, String suffix) {
this.major = major;
this.minor = minor;
this.patch = patch;
this.suffix = suffix;
}
public String toString() {
return major + "." + minor + "." + patch + (suffix.length() > 0 ? "-" + suffix : "");
}
public boolean isBefore(Version other) {
return this.compareTo(other) < 0;
}
@Override
public int compareTo(Version other) {
int major = Integer.compare(this.major, other.major);
if (major != 0)
return major;
int minor = Integer.compare(this.minor, other.minor);
if (minor != 0)
return minor;
int patch = Integer.compare(this.patch, other.patch);
if (patch != 0)
return patch;
if (suffix.length() == 0)
return 1;
if (other.suffix.length() == 0)
return -1;
return suffix.compareTo(other.suffix);
}
public static Version parse(String version) {
int first = version.indexOf(".");
int second = version.indexOf(".", first + 1);
int third = version.contains("-") ? version.indexOf("-") : version.length();
int major = Integer.parseInt(version.substring(0, first));
int minor = Integer.parseInt(version.substring(first + 1, second));
int patch = Integer.parseInt(version.substring(second + 1, third));
String suffix = third < version.length() ? version.substring(third + 1) : "";
return new Version(major, minor, patch, suffix);
}
}