Skip to content

Commit df29960

Browse files
committed
Merge branch 'master' into release/2
# Conflicts: # version
2 parents e769443 + 66722a1 commit df29960

File tree

5 files changed

+604
-0
lines changed

5 files changed

+604
-0
lines changed

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ version = new File('version').text
1717
dependencies {
1818
testCompile 'junit:junit:4.12'
1919
testCompile 'org.mockito:mockito-all:1.9.5'
20+
testCompile 'nl.jqno.equalsverifier:equalsverifier:2.3'
21+
2022
compile 'com.google.guava:guava:20.0'
23+
compile 'org.apache.commons:commons-lang3:3.5'
2124
}
2225

2326
task sourcesJar(type: Jar, dependsOn: classes) {
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.scm4j.commons;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
public class Coords {
6+
7+
private final String artifactId;
8+
private final String commentStr;
9+
private final String extension;
10+
private final String groupId;
11+
private final String classifier;
12+
private final Version version;
13+
private final String coordsString;
14+
15+
public String getComment() {
16+
return commentStr;
17+
}
18+
19+
public Coords(String coordsString) {
20+
this.coordsString = coordsString;
21+
String str = coordsString;
22+
23+
// Comment
24+
{
25+
Integer pos = coordsString.indexOf("#");
26+
27+
if (pos > 0) {
28+
// add spaces between valuable chars and # to comment
29+
commentStr = StringUtils.difference(str.substring(0, pos).trim(), str.substring(0, pos)) + str.substring(pos);
30+
str = str.substring(0, pos).trim();
31+
} else {
32+
commentStr = "";
33+
}
34+
}
35+
36+
// Extension
37+
{
38+
Integer pos = coordsString.indexOf("@");
39+
if (pos > 0) {
40+
extension = str.substring(pos).trim();
41+
str = str.substring(0, pos);
42+
} else {
43+
extension = "";
44+
}
45+
}
46+
47+
String[] strs = str.split(":", -1);
48+
if (strs.length < 2) {
49+
throw new IllegalArgumentException("wrong mdep coord: " + coordsString);
50+
}
51+
52+
groupId = strs[0];
53+
artifactId = strs[1];
54+
55+
classifier = strs.length > 3 ? ":" + strs[3].trim() : "";
56+
57+
version = new Version(strs.length > 2 ? strs[2] : "");
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
final int prime = 31;
63+
int result = 1;
64+
result = prime * result + ((coordsString == null) ? 0 : coordsString.hashCode());
65+
return result;
66+
}
67+
68+
@Override
69+
public boolean equals(Object obj) {
70+
if (this == obj)
71+
return true;
72+
if (obj == null)
73+
return false;
74+
if (getClass() != obj.getClass())
75+
return false;
76+
Coords other = (Coords) obj;
77+
if (coordsString == null) {
78+
if (other.coordsString != null)
79+
return false;
80+
} else if (!coordsString.equals(other.coordsString))
81+
return false;
82+
return true;
83+
}
84+
85+
public Version getVersion() {
86+
return version;
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return toString(version.toString());
92+
}
93+
94+
public String toString(String versionStr) {
95+
String str = versionStr + classifier + extension;
96+
return getName() + (str.isEmpty() ? "" : ":" + str) + commentStr;
97+
}
98+
99+
public String getName() {
100+
return groupId + ":" + artifactId;
101+
}
102+
103+
public String getGroupId() {
104+
return groupId;
105+
}
106+
107+
public String getArtifactId() {
108+
return artifactId;
109+
}
110+
111+
public String getExtension() {
112+
return extension;
113+
}
114+
115+
public String getClassifier() {
116+
return classifier;
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package org.scm4j.commons;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
public class Version {
6+
7+
public static final String SNAPSHOT = "-SNAPSHOT";
8+
9+
private final String minor;
10+
private final String prefix;
11+
private final String snapshot;
12+
private final String patch;
13+
private final String verStr;
14+
private final boolean isEmpty;
15+
private final boolean isSemantic;
16+
17+
public Version(String verStr) {
18+
this.verStr = verStr;
19+
if (verStr.isEmpty()) {
20+
snapshot = "";
21+
prefix = "";
22+
minor = "";
23+
patch = "";
24+
isEmpty = true;
25+
isSemantic = false;
26+
} else {
27+
isEmpty = false;
28+
if (verStr.contains(SNAPSHOT)) {
29+
snapshot = SNAPSHOT;
30+
verStr = verStr.replace(SNAPSHOT, "");
31+
} else {
32+
snapshot = "";
33+
}
34+
if (verStr.lastIndexOf(".") > 0) {
35+
patch = verStr.substring(verStr.lastIndexOf(".") + 1, verStr.length());
36+
verStr = verStr.substring(0, verStr.lastIndexOf("."));
37+
if (verStr.lastIndexOf(".") > 0) {
38+
minor = verStr.substring(verStr.lastIndexOf(".") + 1, verStr.length());
39+
} else {
40+
minor = verStr;
41+
}
42+
prefix = verStr.substring(0, verStr.lastIndexOf(".") + 1);
43+
} else {
44+
prefix = "";
45+
minor = verStr;
46+
patch = "";
47+
}
48+
isSemantic = StringUtils.isNumeric(minor);
49+
}
50+
}
51+
52+
public String getPatch() {
53+
return patch;
54+
}
55+
56+
public String getMinor() {
57+
return minor;
58+
}
59+
60+
public String getSnapshot() {
61+
return snapshot;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
if (!isSemantic) {
67+
return verStr;
68+
}
69+
return prefix + minor + (patch.isEmpty() ? "" : "." + patch) + snapshot;
70+
}
71+
72+
public Version toPreviousPatch() {
73+
if (patch.isEmpty()) {
74+
return new Version(prefix + minor + ".0" + snapshot);
75+
}
76+
if (!StringUtils.isNumeric(patch)) {
77+
return this;
78+
}
79+
int patchInt = Integer.parseInt(patch) - 1;
80+
return new Version(prefix + minor + "." + Integer.toString(patchInt) + snapshot);
81+
}
82+
83+
public Version toNextPatch() {
84+
if (patch.isEmpty()) {
85+
return new Version(prefix + minor + ".1" + snapshot);
86+
}
87+
if (!StringUtils.isNumeric(patch)) {
88+
return this;
89+
}
90+
int patchInt = Integer.parseInt(patch) + 1;
91+
return new Version(prefix + minor + "." + Integer.toString(patchInt) + snapshot);
92+
}
93+
94+
public Version toPreviousMinor() {
95+
checkSemantic();
96+
return new Version(prefix + Integer.toString(Integer.parseInt(minor) - 1) + "." + patch + snapshot);
97+
}
98+
99+
public Version toNextMinor() {
100+
checkSemantic();
101+
return new Version(prefix + Integer.toString(Integer.parseInt(minor) + 1) + "." + patch + snapshot);
102+
}
103+
104+
private void checkSemantic() {
105+
if (!isSemantic) {
106+
throw new IllegalArgumentException("wrong version" + verStr);
107+
}
108+
}
109+
110+
@Override
111+
public boolean equals(Object obj) {
112+
if (this == obj)
113+
return true;
114+
if (obj == null)
115+
return false;
116+
if (getClass() != obj.getClass())
117+
return false;
118+
Version other = (Version) obj;
119+
if (verStr == null) {
120+
if (other.verStr != null)
121+
return false;
122+
} else if (!verStr.equals(other.verStr))
123+
return false;
124+
return true;
125+
}
126+
127+
@Override
128+
public int hashCode() {
129+
final int prime = 31;
130+
int result = 1;
131+
result = prime * result + ((verStr == null) ? 0 : verStr.hashCode());
132+
return result;
133+
}
134+
135+
public Boolean isEmpty() {
136+
return isEmpty;
137+
}
138+
139+
public boolean isExact() {
140+
return isSemantic && !isSnapshot();
141+
}
142+
143+
public boolean isSnapshot() {
144+
return !snapshot.isEmpty();
145+
}
146+
147+
public String toSnapshotString() {
148+
return prefix + minor + (patch.isEmpty() ? "" : "." + patch) + SNAPSHOT;
149+
}
150+
151+
public Version toSnapshot() {
152+
return new Version(toSnapshotString());
153+
}
154+
155+
public String toReleaseString() {
156+
if (!StringUtils.isNumeric(minor)) {
157+
return verStr;
158+
}
159+
return prefix + minor + (patch.isEmpty() ? "" : "." + patch);
160+
}
161+
162+
public Boolean isGreaterThan(Version other) {
163+
if (other.isEmpty()) {
164+
return !isEmpty();
165+
}
166+
if (!isSemantic || !StringUtils.isNumeric(getMinor())) {
167+
return false;
168+
}
169+
if (!other.isSemantic()) {
170+
return true;
171+
}
172+
173+
int minor = Integer.parseInt(getMinor());
174+
int otherMinor = Integer.parseInt(other.getMinor());
175+
if (minor > otherMinor) {
176+
return true;
177+
}
178+
if (minor < otherMinor) {
179+
return false;
180+
}
181+
182+
if (!StringUtils.isNumeric(getPatch()) || !StringUtils.isNumeric(other.getPatch())) {
183+
return false;
184+
}
185+
186+
int patch = Integer.parseInt(getPatch());
187+
int otherPatch = Integer.parseInt(other.getPatch());
188+
189+
return patch > otherPatch;
190+
191+
}
192+
193+
private boolean isSemantic() {
194+
return isSemantic;
195+
}
196+
197+
public String getReleaseNoPatchString() {
198+
return prefix + minor;
199+
}
200+
201+
public Version toRelease() {
202+
if (!isSemantic) {
203+
return this;
204+
}
205+
return new Version(prefix + minor + (patch.isEmpty() ? "" : "." + patch));
206+
}
207+
}

0 commit comments

Comments
 (0)