Skip to content

Instantly share code, notes, and snippets.

@colin-haber
Created October 25, 2012 20:33
Show Gist options
  • Save colin-haber/3955221 to your computer and use it in GitHub Desktop.
Save colin-haber/3955221 to your computer and use it in GitHub Desktop.
static public final Version toVersion(String s) {
final long major, minor, patch;
try {
String[] mainPart = s.split('[' + Pattern.quote(Version.PRERELEASE_PREFIX + Version.BUILD_PREFIX) + ']')[0].split(Pattern.quote(Version.VERSION_SEPARATOR));
major = Long.parseLong(mainPart[0]);
minor = Long.parseLong(mainPart[1]);
patch = Long.parseLong(mainPart[2]);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
return null;
}
List<String> prerelease = null;
List<String> build = null;
final int prereleaseIndex = s.indexOf(Version.PRERELEASE_PREFIX);
final int buildIndex = s.indexOf(Version.BUILD_PREFIX);
if (s.contains(Version.PRERELEASE_PREFIX)) {
prerelease = Arrays.asList(s.substring(prereleaseIndex + 1, buildIndex != -1 ? buildIndex : s.length()).split(Pattern.quote(Version.IDENTIFIER_SEPARATOR)));
}
if (s.contains(Version.BUILD_PREFIX)) {
build = Arrays.asList(s.substring(buildIndex + 1).split(Pattern.quote(Version.IDENTIFIER_SEPARATOR)));
}
return new Version(major, minor, patch, prerelease, build);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment