-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSemanticVersion.java
185 lines (165 loc) · 6.23 KB
/
SemanticVersion.java
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
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FalsePatternLib 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.dependencies;
import com.falsepattern.lib.StableAPI;
import lombok.Getter;
import lombok.NonNull;
import lombok.val;
import java.util.Objects;
@StableAPI(since = "0.6.0")
public class SemanticVersion extends Version {
@Getter(onMethod_ = @StableAPI.Expose)
private final int majorVersion;
@Getter(onMethod_ = @StableAPI.Expose)
private final int minorVersion;
@Getter(onMethod_ = @StableAPI.Expose)
private final int patchVersion;
@Getter(onMethod_ = @StableAPI.Expose)
private final String preRelease;
@Getter(onMethod_ = @StableAPI.Expose)
private final String build;
@StableAPI.Expose
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, String preRelease, String build) {
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
this.patchVersion = patchVersion;
preRelease = preRelease == null ? null : preRelease.trim();
build = build == null ? null : build.trim();
this.preRelease = "".equals(preRelease) ? null : preRelease;
this.build = "".equals(build) ? null : build;
}
@StableAPI.Expose
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, String preRelease) {
this(majorVersion, minorVersion, patchVersion, preRelease, null);
}
@StableAPI.Expose
public SemanticVersion(int majorVersion, int minorVersion, int patchVersion) {
this(majorVersion, minorVersion, patchVersion, null, null);
}
@StableAPI.Expose(since = "0.10.0")
public SemanticVersion(int majorVersion, int minorVersion) {
this(majorVersion, minorVersion, -1, null, null);
}
@StableAPI.Expose(since = "0.10.0")
public SemanticVersion(int majorVersion) {
this(majorVersion, -1, -1, null, null);
}
@StableAPI.Expose
public static SemanticVersionBuilder builder() {
return new SemanticVersionBuilder();
}
@Override
public int compareTo(@NonNull Version o) {
if (o instanceof ComplexVersion) {
val result = this.compareTo(((ComplexVersion) o).versions[0]);
if (result != 0) {
return result;
} else if (((ComplexVersion) o).versions.length > 1) {
return 1;
} else {
return 0;
}
} else if (o instanceof SemanticVersion) {
val other = (SemanticVersion) o;
if (majorVersion != other.majorVersion) {
return majorVersion - other.majorVersion;
} else if (minorVersion != other.minorVersion) {
return minorVersion - other.minorVersion;
} else if (patchVersion != other.patchVersion) {
return patchVersion - other.patchVersion;
} else if (!Objects.equals(preRelease, other.preRelease)) {
if (preRelease == null) {
return 1;
} else if (other.preRelease == null) {
return -1;
}
return preRelease.compareTo(other.preRelease);
} else {
return 0;
}
}
return 0;
}
@Override
public String toString() {
// @formatter:off
return majorVersion + (minorVersion < 0 ? "" : "." + minorVersion) +
(patchVersion < 0 ? "" : "." + patchVersion) + (preRelease == null ? "" : "-" + preRelease) +
(build == null ? "" : "+" + build);
// @formatter:on
}
@StableAPI(since = "0.10.0")
public static class SemanticVersionBuilder {
private int majorVersion;
private int minorVersion = -1;
private int patchVersion = -1;
private String preRelease;
private String build;
@StableAPI.Internal
SemanticVersionBuilder() {
}
@StableAPI.Expose
public SemanticVersionBuilder majorVersion(int majorVersion) {
this.majorVersion = majorVersion;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder minorVersion(int minorVersion) {
this.minorVersion = minorVersion;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder patchVersion(int patchVersion) {
this.patchVersion = patchVersion;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder preRelease(String preRelease) {
this.preRelease = preRelease;
return this;
}
@StableAPI.Expose
public SemanticVersionBuilder build(String build) {
this.build = build;
return this;
}
@StableAPI.Expose
public SemanticVersion build() {
return new SemanticVersion(majorVersion, minorVersion, patchVersion, preRelease, build);
}
@Override
public String toString() {
return "SemanticVersion.SemanticVersionBuilder(majorVersion="
+ this.majorVersion
+ ", minorVersion="
+ this.minorVersion
+ ", patchVersion="
+ this.patchVersion
+ ", preRelease="
+ this.preRelease
+ ", build="
+ this.build
+ ")";
}
}
}