Skip to content

Commit e928761

Browse files
committed
Maven Resolver coords scheme #1
1 parent 5166082 commit e928761

File tree

9 files changed

+501
-174
lines changed

9 files changed

+501
-174
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.scm4j.commons;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
public class CommentedString {
6+
7+
private static final String DEFAULT_COMMENT_SIGN = "#";
8+
private final String strNoComment;
9+
private final String comment;
10+
private final String sourceString;
11+
12+
public CommentedString(String str) {
13+
this(str, DEFAULT_COMMENT_SIGN);
14+
}
15+
16+
public CommentedString(String str, String commentSign) {
17+
sourceString = str;
18+
Integer pos = str.indexOf(commentSign);
19+
20+
if (pos >= 0) {
21+
// add spaces between valuable chars and # to comment
22+
comment = StringUtils.difference(str.substring(0, pos).trim(), ltrim(str.substring(0, pos)))
23+
+ str.substring(pos);
24+
strNoComment = rtrim(str.substring(0, pos));
25+
} else {
26+
comment = "";
27+
strNoComment = str;
28+
}
29+
}
30+
31+
public Boolean isValuable() {
32+
return !strNoComment.trim().isEmpty();
33+
}
34+
35+
public String getStrNoComment() {
36+
return strNoComment;
37+
}
38+
39+
public String getComment() {
40+
return comment;
41+
}
42+
43+
private String ltrim(String s) {
44+
int i = 0;
45+
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
46+
i++;
47+
}
48+
return s.substring(i);
49+
}
50+
51+
private String rtrim(String s) {
52+
int i = s.length() - 1;
53+
while (i > 0 && Character.isWhitespace(s.charAt(i))) {
54+
i--;
55+
}
56+
return s.substring(0, i + 1);
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return sourceString;
62+
}
63+
}

src/main/java/org/scm4j/commons/Coords.java

Lines changed: 0 additions & 146 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package org.scm4j.commons;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
public class CoordsGradle implements ICoords {
6+
7+
private final String sourceStr;
8+
private final String artifactId;
9+
private final String commentStr;
10+
private final String extension;
11+
private final String groupId;
12+
private final String classifier;
13+
private final Version version;
14+
private final String coordsStringNoComment;
15+
16+
public CoordsGradle(String coordsString) {
17+
sourceStr = coordsString;
18+
19+
CommentedString cs = new CommentedString(coordsString);
20+
commentStr = cs.getComment();
21+
coordsStringNoComment = cs.getStrNoComment();
22+
23+
coordsString = coordsStringNoComment;
24+
25+
// Extension
26+
{
27+
Integer pos = coordsString.indexOf("@");
28+
if (pos > 0) {
29+
extension = coordsString.substring(pos).trim();
30+
coordsString = coordsString.substring(0, pos);
31+
} else {
32+
extension = "";
33+
}
34+
}
35+
36+
String[] strs = coordsString.split(":", -1);
37+
if (strs.length < 2) {
38+
throw new IllegalArgumentException("wrong mdep coord: " + coordsString);
39+
}
40+
41+
groupId = strs[0];
42+
43+
artifactId = ":" + strs[1];
44+
45+
classifier = strs.length > 3 ? ":" + strs[3].trim() : "";
46+
47+
version = new Version(strs.length > 2 ? strs[2] : "");
48+
}
49+
50+
@Override
51+
public int hashCode() {
52+
final int prime = 31;
53+
int result = 1;
54+
result = prime * result + ((coordsStringNoComment == null) ? 0 : coordsStringNoComment.hashCode());
55+
return result;
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj) {
60+
if (this == obj)
61+
return true;
62+
if (obj == null)
63+
return false;
64+
if (getClass() != obj.getClass())
65+
return false;
66+
CoordsGradle other = (CoordsGradle) obj;
67+
if (coordsStringNoComment == null) {
68+
if (other.coordsStringNoComment != null)
69+
return false;
70+
} else if (!coordsStringNoComment.equals(other.coordsStringNoComment))
71+
return false;
72+
return true;
73+
}
74+
75+
@Override
76+
public Version getVersion() {
77+
return version;
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return sourceStr;
83+
}
84+
85+
@Override
86+
public String toString(String versionStr) {
87+
String name = groupId + artifactId;
88+
if (!versionStr.isEmpty() || ((!name.isEmpty() && !(classifier + extension).isEmpty()))) {
89+
versionStr = ":" + versionStr;
90+
}
91+
return groupId + artifactId + versionStr + classifier + extension + commentStr;
92+
}
93+
94+
@Override
95+
public String getGroupId() {
96+
return groupId;
97+
}
98+
99+
@Override
100+
public String getArtifactId() {
101+
return StringUtils.removeStart(artifactId, ":");
102+
}
103+
104+
@Override
105+
public String getExtension() {
106+
return StringUtils.removeStart(extension, "@");
107+
}
108+
109+
@Override
110+
public String getClassifier() {
111+
return StringUtils.removeStart(classifier, ":");
112+
}
113+
114+
@Override
115+
public String getComment() {
116+
return commentStr;
117+
}
118+
119+
@Override
120+
public String toStringNoComment() {
121+
return coordsStringNoComment;
122+
}
123+
}

0 commit comments

Comments
 (0)