-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersion.java
More file actions
107 lines (84 loc) · 2.61 KB
/
Version.java
File metadata and controls
107 lines (84 loc) · 2.61 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
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
/**
* @author Imprex-Development
* @see <a href="https://github.com/Imprex-Development/orebfuscator/blob/master/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/Version.java">Version.java</a>
*/
package dev.imprex.zip.common;
import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
public record Version(int major, int minor, int patch) implements Comparable<Version> {
private static final Pattern VERSION_PATTERN = Pattern.compile("(?<major>\\d+)(?:\\.(?<minor>\\d+))?(?:\\.(?<patch>\\d+))?");
public static Version parse(String version) {
Matcher matcher = VERSION_PATTERN.matcher(version);
if (!matcher.find()) {
throw new IllegalArgumentException("can't parse version: " + version);
}
int major = Integer.parseInt(matcher.group("major"));
String minorGroup = matcher.group("minor");
int minor = minorGroup != null
? Integer.parseInt(minorGroup)
: 0;
String patchGroup = matcher.group("patch");
int patch = patchGroup != null
? Integer.parseInt(patchGroup)
: 0;
return new Version(major, minor, patch);
}
public boolean isAbove(Version version) {
return this.compareTo(version) > 0;
}
public boolean isAtOrAbove(Version version) {
return this.compareTo(version) >= 0;
}
public boolean isAtOrBelow(Version version) {
return this.compareTo(version) <= 0;
}
public boolean isBelow(Version version) {
return this.compareTo(version) < 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;
}
return Integer.compare(this.patch, other.patch);
}
@Override
public int hashCode() {
return Objects.hash(major, minor, patch);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Version)) {
return false;
}
Version other = (Version) obj;
return major == other.major && minor == other.minor && patch == other.patch;
}
@Override
public String toString() {
return String.format("%s.%s.%s", this.major, this.minor, this.patch);
}
public static final class Json extends TypeAdapter<Version> {
@Override
public void write(JsonWriter out, Version value) throws IOException {
out.value(value.toString());
}
@Override
public Version read(JsonReader in) throws IOException {
return Version.parse(in.nextString());
}
}
}