summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/utils/SemanticVersionNumber.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/sevenUnits/utils/SemanticVersionNumber.java')
-rw-r--r--src/main/java/sevenUnits/utils/SemanticVersionNumber.java94
1 files changed, 47 insertions, 47 deletions
diff --git a/src/main/java/sevenUnits/utils/SemanticVersionNumber.java b/src/main/java/sevenUnits/utils/SemanticVersionNumber.java
index e80e16e..fc47baa 100644
--- a/src/main/java/sevenUnits/utils/SemanticVersionNumber.java
+++ b/src/main/java/sevenUnits/utils/SemanticVersionNumber.java
@@ -61,7 +61,7 @@ public final class SemanticVersionNumber
private final int patch;
private final List<String> preReleaseIdentifiers;
private final List<String> buildMetadata;
-
+
/**
* Creates a builder which can be used to create a
* {@code SemanticVersionNumber}
@@ -79,7 +79,7 @@ public final class SemanticVersionNumber
this.preReleaseIdentifiers = new ArrayList<>();
this.buildMetadata = new ArrayList<>();
}
-
+
/**
* @return version number created by this builder
* @since v0.4.0
@@ -89,7 +89,7 @@ public final class SemanticVersionNumber
return new SemanticVersionNumber(this.major, this.minor, this.patch,
this.preReleaseIdentifiers, this.buildMetadata);
}
-
+
/**
* Adds one or more build metadata identifiers
*
@@ -109,7 +109,7 @@ public final class SemanticVersionNumber
}
return this;
}
-
+
/**
* Adds one or more build metadata identifiers
*
@@ -129,7 +129,7 @@ public final class SemanticVersionNumber
}
return this;
}
-
+
@Override
public boolean equals(Object obj) {
if (this == obj)
@@ -142,13 +142,13 @@ public final class SemanticVersionNumber
&& this.patch == other.patch && Objects.equals(
this.preReleaseIdentifiers, other.preReleaseIdentifiers);
}
-
+
@Override
public int hashCode() {
return Objects.hash(this.buildMetadata, this.major, this.minor,
this.patch, this.preReleaseIdentifiers);
}
-
+
/**
* Adds one or more numeric identifiers to the version number
*
@@ -167,7 +167,7 @@ public final class SemanticVersionNumber
}
return this;
}
-
+
/**
* Adds one or more pre-release identifier(s) to the version number
*
@@ -187,7 +187,7 @@ public final class SemanticVersionNumber
}
return this;
}
-
+
/**
* Adds one or more pre-release identifier(s) to the version number
*
@@ -207,7 +207,7 @@ public final class SemanticVersionNumber
}
return this;
}
-
+
/**
* Adds a string identifier and an integer identifer to pre-release data
*
@@ -229,13 +229,13 @@ public final class SemanticVersionNumber
this.preReleaseIdentifiers.add(Integer.toString(identifier2));
return this;
}
-
+
@Override
public String toString() {
return "Semantic Version Builder: " + this.build().toString();
}
}
-
+
/**
* An alternative comparison method for version numbers. This uses the
* version's natural order, but the build metadata will be compared (using
@@ -257,21 +257,21 @@ public final class SemanticVersionNumber
return naturalComparison;
};
};
-
+
/** The alphanumeric pattern all identifiers must follow */
private static final Pattern VALID_IDENTIFIER = Pattern
.compile("[0-9A-Za-z-]+");
-
+
/** The numeric pattern which causes special behaviour */
private static final Pattern NUMERIC_IDENTIFER = Pattern.compile("[0-9]+");
-
+
/** The pattern for a version number */
private static final Pattern VERSION_NUMBER = Pattern
.compile("(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)" // main
// version
+ "(?:-([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?" // pre-release
+ "(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?"); // build data
-
+
/**
* Creates a builder that can be used to create a version number
*
@@ -296,7 +296,7 @@ public final class SemanticVersionNumber
"Patch version must be non-negative.");
return new SemanticVersionNumber.Builder(major, minor, patch);
}
-
+
/**
* Compares two lists of strings based on SemVer's precedence rules
*
@@ -311,24 +311,24 @@ public final class SemanticVersionNumber
// test pre-release size
final int aSize = a.size();
final int bSize = b.size();
-
+
// no identifiers is greater than any identifiers
if (aSize != 0 && bSize == 0)
return -1;
else if (aSize == 0 && bSize != 0)
return 1;
-
+
// test identifiers one by one
for (int i = 0; i < Math.min(aSize, bSize); i++) {
final String aElement = a.get(i);
final String bElement = b.get(i);
-
+
if (NUMERIC_IDENTIFER.matcher(aElement).matches()) {
if (NUMERIC_IDENTIFER.matcher(bElement).matches()) {
// both are numbers, compare them
final int aNumber = Integer.parseInt(aElement);
final int bNumber = Integer.parseInt(bElement);
-
+
if (aNumber < bNumber)
return -1;
else if (aNumber > bNumber)
@@ -350,7 +350,7 @@ public final class SemanticVersionNumber
}
}
}
-
+
// we just tested the stuff that's in common, maybe someone has more
if (aSize < bSize)
return -1;
@@ -359,7 +359,7 @@ public final class SemanticVersionNumber
else
return 0;
}
-
+
/**
* Gets a version number from a string in the official format
*
@@ -377,12 +377,12 @@ public final class SemanticVersionNumber
throw new IllegalArgumentException(
String.format("Provided string \"%s\" is not a version number",
versionString));
-
+
// main parts
final int major = Integer.parseInt(m.group(1));
final int minor = Integer.parseInt(m.group(2));
final int patch = Integer.parseInt(m.group(3));
-
+
// pre release
final List<String> preRelease;
if (m.group(4) == null) {
@@ -390,7 +390,7 @@ public final class SemanticVersionNumber
} else {
preRelease = Arrays.asList(m.group(4).split("\\."));
}
-
+
// build metadata
final List<String> buildMetadata;
if (m.group(5) == null) {
@@ -398,12 +398,12 @@ public final class SemanticVersionNumber
} else {
buildMetadata = Arrays.asList(m.group(5).split("\\."));
}
-
+
// return number
return new SemanticVersionNumber(major, minor, patch, preRelease,
buildMetadata);
}
-
+
/**
* Tests whether a string is a valid Semantic Version string
*
@@ -415,7 +415,7 @@ public final class SemanticVersionNumber
public static final boolean isValidVersionString(String versionString) {
return VERSION_NUMBER.matcher(versionString).matches();
}
-
+
/**
* Creates a simple pre-release version number of the form
* MAJOR.MINOR.PATH-TYPE.NUMBER (e.g. 1.2.3-alpha.4).
@@ -454,7 +454,7 @@ public final class SemanticVersionNumber
List.of(preReleaseType, Integer.toString(preReleaseNumber)),
List.of());
}
-
+
/**
* Creates a {@code SemanticVersionNumber} instance without pre-release
* identifiers or build metadata.
@@ -484,14 +484,14 @@ public final class SemanticVersionNumber
return new SemanticVersionNumber(major, minor, patch, List.of(),
List.of());
}
-
+
// parts of the version number
private final int major;
private final int minor;
private final int patch;
private final List<String> preReleaseIdentifiers;
private final List<String> buildMetadata;
-
+
/**
* Creates a version number
*
@@ -511,7 +511,7 @@ public final class SemanticVersionNumber
this.preReleaseIdentifiers = preReleaseIdentifiers;
this.buildMetadata = buildMetadata;
}
-
+
/**
* @return build metadata (empty if there is none)
* @since v0.4.0
@@ -520,7 +520,7 @@ public final class SemanticVersionNumber
public List<String> buildMetadata() {
return Collections.unmodifiableList(this.buildMetadata);
}
-
+
/**
* Compares two version numbers according to the official Semantic Versioning
* order.
@@ -538,23 +538,23 @@ public final class SemanticVersionNumber
return -1;
else if (this.major > o.major)
return 1;
-
+
if (this.minor < o.minor)
return -1;
else if (this.minor > o.minor)
return 1;
-
+
if (this.patch < o.patch)
return -1;
else if (this.patch > o.patch)
return 1;
-
+
// now we just compare pre-release identifiers
// (remember: build metadata is ignored)
return SemanticVersionNumber.compareIdentifiers(
this.preReleaseIdentifiers, o.preReleaseIdentifiers);
}
-
+
/**
* Determines the compatibility of code written for this version to
* {@code other}. More specifically:
@@ -590,11 +590,11 @@ public final class SemanticVersionNumber
*/
public boolean compatibleWith(SemanticVersionNumber other) {
Objects.requireNonNull(other, "other may not be null");
-
+
return this.compareTo(other) == 0 || this.major != 0
&& this.major == other.major && this.compareTo(other) < 0;
}
-
+
@Override
public boolean equals(Object obj) {
if (this == obj)
@@ -621,7 +621,7 @@ public final class SemanticVersionNumber
return false;
return true;
}
-
+
@Override
public int hashCode() {
final int prime = 31;
@@ -635,7 +635,7 @@ public final class SemanticVersionNumber
: this.preReleaseIdentifiers.hashCode());
return result;
}
-
+
/**
* @return true iff this version is stable (major version > 0 and not a
* pre-release)
@@ -645,7 +645,7 @@ public final class SemanticVersionNumber
public boolean isStable() {
return this.major > 0 && this.preReleaseIdentifiers.isEmpty();
}
-
+
/**
* @return the MAJOR version number, incremented when you make backwards
* incompatible API changes
@@ -655,7 +655,7 @@ public final class SemanticVersionNumber
public int majorVersion() {
return this.major;
}
-
+
/**
* @return the MINOR version number, incremented when you add backwards
* compatible functionality
@@ -665,7 +665,7 @@ public final class SemanticVersionNumber
public int minorVersion() {
return this.minor;
}
-
+
/**
* @return the PATCH version number, incremented when you make backwards
* compatible bug fixes
@@ -675,7 +675,7 @@ public final class SemanticVersionNumber
public int patchVersion() {
return this.patch;
}
-
+
/**
* @return identifiers describing this pre-release (empty if not a
* pre-release)
@@ -685,7 +685,7 @@ public final class SemanticVersionNumber
public List<String> preReleaseIdentifiers() {
return Collections.unmodifiableList(this.preReleaseIdentifiers);
}
-
+
/**
* Converts a version number to a string using the official SemVer format.
* The core of a version is MAJOR.MINOR.PATCH, without zero-padding. If