Skip to content

Commit e6199d0

Browse files
committed
Coords and Version classes added
1 parent ecdb228 commit e6199d0

File tree

5 files changed

+582
-0
lines changed

5 files changed

+582
-0
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
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) {
Lines changed: 118 additions & 0 deletions
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+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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 isSnapshot() {
140+
return !snapshot.isEmpty();
141+
}
142+
143+
public String toSnapshotString() {
144+
return prefix + minor + (patch.isEmpty() ? "" : "." + patch) + SNAPSHOT;
145+
}
146+
147+
public String toReleaseString() {
148+
if (!StringUtils.isNumeric(minor)) {
149+
return verStr;
150+
}
151+
return prefix + minor + (patch.isEmpty() ? "" : "." + patch);
152+
}
153+
154+
public Boolean isGreaterThan(Version other) {
155+
if (other.isEmpty()) {
156+
return !isEmpty();
157+
}
158+
if (!isSemantic || !StringUtils.isNumeric(getMinor())) {
159+
return false;
160+
}
161+
if (!other.isSemantic()) {
162+
return true;
163+
}
164+
165+
int minor = Integer.parseInt(getMinor());
166+
int otherMinor = Integer.parseInt(other.getMinor());
167+
if (minor > otherMinor) {
168+
return true;
169+
}
170+
if (minor < otherMinor) {
171+
return false;
172+
}
173+
174+
if (!StringUtils.isNumeric(getPatch()) || !StringUtils.isNumeric(other.getPatch())) {
175+
return false;
176+
}
177+
178+
int patch = Integer.parseInt(getPatch());
179+
int otherPatch = Integer.parseInt(other.getPatch());
180+
181+
return patch > otherPatch;
182+
183+
}
184+
185+
private boolean isSemantic() {
186+
return isSemantic;
187+
}
188+
189+
public String getReleaseNoPatchString() {
190+
return prefix + minor;
191+
}
192+
193+
public Version toRelease() {
194+
if (!isSemantic) {
195+
return this;
196+
}
197+
return new Version(prefix + minor + (patch.isEmpty() ? "" : "." + patch));
198+
}
199+
}

0 commit comments

Comments
 (0)