Created
January 30, 2019 10:31
-
-
Save Crydust/a4d147a62363cd1933872f55c8f324ed to your computer and use it in GitHub Desktop.
NaiveMavenVersion Comparable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Objects; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* Only these three version shemes are supported: | |
* <ul> | |
* <li>major[.minor[.incremental]][-buildnumber]</li> | |
* <li>major[.minor[.incremental]][-qualifier]</li> | |
* <li>qualifier</li> | |
* </ul> | |
* Unlike maven 3, this class doesn't allow mixing dashes and dots.<br/> | |
* Also unlike maven 3, this class sorts qualifiers alphabetically. | |
*/ | |
class NaiveMavenVersion implements Comparable<NaiveMavenVersion> { | |
private static final Pattern PATTERN = Pattern.compile("" + | |
"(?<major>[0-9]{1,9})?" + | |
"(?:\\.(?<minor>[0-9]{1,9})" + | |
"(?:\\.(?<incremental>[0-9]{1,9}))?" + | |
")?" + | |
"(?:-(?:(?<buildnumber>[0-9]{1,9})|(?<qualifier>.+)))?"); | |
private final int major; | |
private final int minor; | |
private final int incremental; | |
private final int buildnumber; | |
private final String qualifier; | |
private NaiveMavenVersion(int major, int minor, int incremental, int buildnumber, String qualifier) { | |
this.major = major; | |
this.minor = minor; | |
this.incremental = incremental; | |
this.buildnumber = buildnumber; | |
this.qualifier = qualifier; | |
} | |
public static NaiveMavenVersion of(String version) { | |
if (version == null || version.isEmpty()) { | |
return new NaiveMavenVersion(0, 0, 0, 0, ""); | |
} | |
final Matcher m = PATTERN.matcher(version); | |
if (m.matches()) { | |
final String major = m.group("major"); | |
final String minor = m.group("minor"); | |
final String incremental = m.group("incremental"); | |
final String buildnumber = m.group("buildnumber"); | |
final String qualifier = m.group("qualifier"); | |
return new NaiveMavenVersion( | |
major == null ? 0 : Integer.parseInt(major), | |
minor == null ? 0 : Integer.parseInt(minor), | |
incremental == null ? 0 : Integer.parseInt(incremental), | |
buildnumber == null ? 0 : Integer.parseInt(buildnumber), | |
qualifier == null ? "" : qualifier | |
); | |
} else { | |
return new NaiveMavenVersion(0, 0, 0, 0, version); | |
} | |
} | |
@Override | |
public int compareTo(NaiveMavenVersion o) { | |
int c = Integer.compare(major, o.major); | |
if (c != 0) { | |
return c; | |
} | |
c = Integer.compare(minor, o.minor); | |
if (c != 0) { | |
return c; | |
} | |
c = Integer.compare(incremental, o.incremental); | |
if (c != 0) { | |
return c; | |
} | |
c = Integer.compare(buildnumber, o.buildnumber); | |
if (c != 0) { | |
return c; | |
} | |
c = qualifier.compareTo(o.qualifier); | |
return c; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
final NaiveMavenVersion that = (NaiveMavenVersion) o; | |
return major == that.major && | |
minor == that.minor && | |
incremental == that.incremental && | |
buildnumber == that.buildnumber && | |
qualifier.equals(that.qualifier); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(major, minor, incremental, buildnumber, qualifier); | |
} | |
@Override | |
public String toString() { | |
return major + "." + minor + "." + incremental + | |
(buildnumber == 0 ? "" : "-" + buildnumber) + | |
(qualifier.isEmpty() ? "" : "-" + qualifier); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment