Permalink
Browse files

Introduced GAV.optional flag

  • Loading branch information...
fcamblor committed Oct 23, 2015
1 parent abce8ac commit e85b0f6fd03b10fcc02b211347c5ca60335e9f4c
@@ -6,41 +6,49 @@
* Time: 2:21 PM
*/
public class GAV {
private static final String OPTIONAL_SUFFIX = "!optional";
public static GAV parse(String gav) {
String[] parts = gav.split(":");
if (parts.length < 3 || parts.length > 5) {
throw new IllegalArgumentException("can't parse '" + gav + "' as a module coordinates (GAV). " +
"It must have at least 3 parts separated by columns. (4th and 5th are optional and correspond to artifact type and classifier)");
}
boolean optional = false;
if(parts[parts.length-1].endsWith(OPTIONAL_SUFFIX)) {
optional = true;
parts[parts.length-1] = parts[parts.length-1].substring(0, parts[parts.length-1].length()-OPTIONAL_SUFFIX.length());
}
if(parts.length == 3) {
return new GAV(parts[0], parts[1], parts[2]);
return new GAV(parts[0], parts[1], parts[2], optional);
}
if(parts.length == 4) {
return new GAV(parts[0], parts[1], parts[2], parts[3]);
return new GAV(parts[0], parts[1], parts[2], parts[3], optional);
}
return new GAV(parts[0], parts[1], parts[2], parts[3], parts[4]);
return new GAV(parts[0], parts[1], parts[2], parts[3], parts[4], optional);
}
private final String groupId;
private final String artifactId;
private final String version;
private final String type;
private final String classifier;
private final boolean optional;
public GAV(String groupId, String artifactId, String version) {
this(groupId, artifactId, version, null, null);
public GAV(String groupId, String artifactId, String version, final boolean optional) {
this(groupId, artifactId, version, null, null, optional);
}
public GAV(String groupId, String artifactId, String version, String type) {
this(groupId, artifactId, version, type, null);
public GAV(String groupId, String artifactId, String version, String type, final boolean optional) {
this(groupId, artifactId, version, type, null, optional);
}
public GAV(final String groupId, final String artifactId, final String version, final String type, final String classifier) {
public GAV(final String groupId, final String artifactId, final String version, final String type, final String classifier, final boolean optional) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.type = type;
this.classifier = classifier;
this.optional = optional;
}
public String getGroupId() {
@@ -62,7 +70,11 @@ public String getType() {
public String getClassifier() {
return classifier;
}
public boolean isOptional() {
return optional;
}
@Override
public String toString() {
return groupId + ":" + artifactId + ":" + version;
@@ -74,12 +86,13 @@ public String toString() {
* through GAV.parse()
*/
public String toParseableString(){
String suffix = optional?OPTIONAL_SUFFIX:"";
if (type == null){
return groupId + ":" + artifactId + ":" + version;
return groupId + ":" + artifactId + ":" + version + suffix;
}
if(classifier == null) {
return groupId + ":" + artifactId + ":" + version + ":" + type;
return groupId + ":" + artifactId + ":" + version + ":" + type + suffix;
}
return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier;
return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier + suffix;
}
}
@@ -78,7 +78,8 @@ private GAV getGav(JSONObject jsonObject, String typeKey) {
jsonObject.getString("artifactId"),
String.valueOf(jsonObject.get("version")),
typeKey==null?null:jsonObject.has(typeKey)?jsonObject.getString(typeKey):null,
jsonObject.has("classifier")?jsonObject.getString("classifier"):null);
jsonObject.has("classifier")?jsonObject.getString("classifier"):null,
false);
}
}
static class Generator {
@@ -23,6 +23,13 @@ public void should_parse_classifier_artifact(){
shouldBeConsistent("restx:restx-ui:0.2:jar:jdk8");
}
@Test
public void should_parse_optional_artifact(){
shouldBeConsistent("restx:restx-ui:0.2!optional");
shouldBeConsistent("restx:restx-ui:0.2:zip!optional");
shouldBeConsistent("restx:restx-ui:0.2:jar:jdk8!optional");
}
@Test
public void should_parse_artifact_with_type() throws Exception {
shouldBeConsistent("restx:restx-ui:0.2:zip");

0 comments on commit e85b0f6

Please sign in to comment.