This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from launchdarkly/2.6.0
release 2.6.0
- Loading branch information
Showing
9 changed files
with
464 additions
and
27 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
version=2.5.1 | ||
version=2.6.0 | ||
ossrhUsername= | ||
ossrhPassword= | ||
ossrhPassword= |
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
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
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
176 changes: 176 additions & 0 deletions
176
src/main/java/com/launchdarkly/client/SemanticVersion.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package com.launchdarkly.client; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Simple implementation of semantic version parsing and comparison according to the Semantic | ||
* Versions 2.0.0 standard (http://semver.org). | ||
*/ | ||
class SemanticVersion implements Comparable<SemanticVersion> { | ||
|
||
private static Pattern VERSION_REGEX = Pattern.compile( | ||
"^(?<major>0|[1-9]\\d*)(\\.(?<minor>0|[1-9]\\d*))?(\\.(?<patch>0|[1-9]\\d*))?" + | ||
"(\\-(?<prerel>[0-9A-Za-z\\-\\.]+))?(\\+(?<build>[0-9A-Za-z\\-\\.]+))?$"); | ||
|
||
@SuppressWarnings("serial") | ||
public static class InvalidVersionException extends Exception { | ||
public InvalidVersionException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
private final int major; | ||
private final int minor; | ||
private final int patch; | ||
private final String prerelease; | ||
private final String build; | ||
|
||
public SemanticVersion(int major, int minor, int patch, String prerelease, String build) { | ||
this.major = major; | ||
this.minor = minor; | ||
this.patch = patch; | ||
this.prerelease = prerelease; | ||
this.build = build; | ||
} | ||
|
||
public int getMajor() { | ||
return major; | ||
} | ||
|
||
public int getMinor() { | ||
return minor; | ||
} | ||
|
||
public int getPatch() { | ||
return patch; | ||
} | ||
|
||
public String getPrerelease() { | ||
return prerelease; | ||
} | ||
|
||
public String getBuild() { | ||
return build; | ||
} | ||
|
||
/** | ||
* Attempts to parse a string as a semantic version according to the Semver 2.0.0 specification. | ||
* @param input the input string | ||
* @return a SemanticVersion instance | ||
* @throws InvalidVersionException if the version could not be parsed | ||
*/ | ||
public static SemanticVersion parse(String input) throws InvalidVersionException { | ||
return parse(input, false); | ||
} | ||
|
||
/** | ||
* Attempts to parse a string as a semantic version according to the Semver 2.0.0 specification, except that | ||
* the minor and patch versions may optionally be omitted. | ||
* @param input the input string | ||
* @param allowMissingMinorAndPatch true if the parser should tolerate the absence of a minor and/or | ||
* patch version; if absent, they will be treated as zero | ||
* @return a SemanticVersion instance | ||
* @throws InvalidVersionException if the version could not be parsed | ||
*/ | ||
public static SemanticVersion parse(String input, boolean allowMissingMinorAndPatch) throws InvalidVersionException { | ||
Matcher matcher = VERSION_REGEX.matcher(input); | ||
if (!matcher.matches()) { | ||
throw new InvalidVersionException("Invalid semantic version"); | ||
} | ||
int major, minor, patch; | ||
try { | ||
major = Integer.parseInt(matcher.group("major")); | ||
if (!allowMissingMinorAndPatch) { | ||
if (matcher.group("minor") == null || matcher.group("patch") == null) { | ||
throw new InvalidVersionException("Invalid semantic version"); | ||
} | ||
} | ||
minor = matcher.group("minor") == null ? 0 : Integer.parseInt(matcher.group("minor")); | ||
patch = matcher.group("patch") == null ? 0 : Integer.parseInt(matcher.group("patch")); | ||
} catch (NumberFormatException e) { | ||
throw new InvalidVersionException("Invalid semantic version"); | ||
} | ||
String prerelease = matcher.group("prerel"); | ||
String build = matcher.group("build"); | ||
return new SemanticVersion(major, minor, patch, prerelease, build); | ||
} | ||
|
||
@Override | ||
public int compareTo(SemanticVersion other) { | ||
return comparePrecedence(other); | ||
} | ||
|
||
/** | ||
* Compares this object with another SemanticVersion according to Semver 2.0.0 precedence rules. | ||
* @param other another SemanticVersion | ||
* @return 0 if equal, -1 if the current object has lower precedence, or 1 if the current object has higher precedence | ||
*/ | ||
public int comparePrecedence(SemanticVersion other) { | ||
if (other == null) { | ||
return 1; | ||
} | ||
if (major != other.major) { | ||
return Integer.compare(major, other.major); | ||
} | ||
if (minor != other.minor) { | ||
return Integer.compare(minor, other.minor); | ||
} | ||
if (patch != other.patch) { | ||
return Integer.compare(patch, other.patch); | ||
} | ||
if (prerelease == null && other.prerelease == null) { | ||
return 0; | ||
} | ||
// *no* prerelease component always has higher precedence than *any* prerelease component | ||
if (prerelease == null) { | ||
return 1; | ||
} | ||
if (other.prerelease == null) { | ||
return -1; | ||
} | ||
return compareIdentifiers(prerelease.split("\\."), other.prerelease.split("\\.")); | ||
} | ||
|
||
private int compareIdentifiers(String[] ids1, String[] ids2) { | ||
for (int i = 0; ; i++) { | ||
if (i >= ids1.length) | ||
{ | ||
// x.y is always less than x.y.z | ||
return (i >= ids2.length) ? 0 : -1; | ||
} | ||
if (i >= ids2.length) | ||
{ | ||
return 1; | ||
} | ||
// each sub-identifier is compared numerically if both are numeric; if both are non-numeric, | ||
// they're compared as strings; otherwise, the numeric one is the lesser one | ||
int n1 = 0, n2 = 0, d; | ||
boolean isNum1, isNum2; | ||
try { | ||
n1 = Integer.parseInt(ids1[i]); | ||
isNum1 = true; | ||
} catch (NumberFormatException e) { | ||
isNum1 = false; | ||
} | ||
try { | ||
n2 = Integer.parseInt(ids2[i]); | ||
isNum2 = true; | ||
} catch (NumberFormatException e) { | ||
isNum2 = false; | ||
} | ||
if (isNum1 && isNum2) | ||
{ | ||
d = Integer.compare(n1, n2); | ||
} | ||
else | ||
{ | ||
d = isNum1 ? -1 : (isNum2 ? 1 : ids1[i].compareTo(ids2[i])); | ||
} | ||
if (d != 0) | ||
{ | ||
return d; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.