That fuck shit the fascists are using
1package org.tm.archive.util;
2
3import androidx.annotation.Nullable;
4
5import com.annimon.stream.ComparatorCompat;
6
7import java.util.Comparator;
8import java.util.Objects;
9import java.util.regex.Matcher;
10import java.util.regex.Pattern;
11
12public final class SemanticVersion implements Comparable<SemanticVersion> {
13
14 private static final Pattern VERSION_PATTERN = Pattern.compile("^([0-9]+)\\.([0-9]+)\\.([0-9]+)$");
15
16 private static final Comparator<SemanticVersion> MAJOR_COMPARATOR = (s1, s2) -> Integer.compare(s1.major, s2.major);
17 private static final Comparator<SemanticVersion> MINOR_COMPARATOR = (s1, s2) -> Integer.compare(s1.minor, s2.minor);
18 private static final Comparator<SemanticVersion> PATCH_COMPARATOR = (s1, s2) -> Integer.compare(s1.patch, s2.patch);
19 private static final Comparator<SemanticVersion> COMPARATOR = ComparatorCompat.chain(MAJOR_COMPARATOR)
20 .thenComparing(MINOR_COMPARATOR)
21 .thenComparing(PATCH_COMPARATOR);
22
23 private final int major;
24 private final int minor;
25 private final int patch;
26
27 public SemanticVersion(int major, int minor, int patch) {
28 this.major = major;
29 this.minor = minor;
30 this.patch = patch;
31 }
32
33 public static @Nullable SemanticVersion parse(@Nullable String value) {
34 if (value == null) {
35 return null;
36 }
37
38 Matcher matcher = VERSION_PATTERN.matcher(value);
39 if (Util.isEmpty(value) || !matcher.matches()) {
40 return null;
41 }
42
43 int major = Integer.parseInt(matcher.group(1));
44 int minor = Integer.parseInt(matcher.group(2));
45 int patch = Integer.parseInt(matcher.group(3));
46
47 return new SemanticVersion(major, minor, patch);
48 }
49
50 @Override
51 public int compareTo(SemanticVersion other) {
52 return COMPARATOR.compare(this, other);
53 }
54
55 @Override
56 public boolean equals(Object o) {
57 if (this == o) return true;
58 if (o == null || getClass() != o.getClass()) return false;
59 SemanticVersion that = (SemanticVersion) o;
60 return major == that.major &&
61 minor == that.minor &&
62 patch == that.patch;
63 }
64
65 @Override
66 public int hashCode() {
67 return Objects.hash(major, minor, patch);
68 }
69}