summaryrefslogtreecommitdiffstats
path: root/component
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2023-07-26 09:26:34 +0200
committerMartin Polden <mpolden@mpolden.no>2023-07-26 09:50:20 +0200
commit90e9569d097dcdd9ed91e65d0bbe1ac43e52aa76 (patch)
tree643c7d94ed16705c8bebb81c8517d7377af75edf /component
parente9613c844167b4e8a55096043f42953182cd3482 (diff)
Improve exception for invalid version
Diffstat (limited to 'component')
-rw-r--r--component/src/main/java/com/yahoo/component/Version.java20
1 files changed, 12 insertions, 8 deletions
diff --git a/component/src/main/java/com/yahoo/component/Version.java b/component/src/main/java/com/yahoo/component/Version.java
index db8606d31fa..e091a94a5ef 100644
--- a/component/src/main/java/com/yahoo/component/Version.java
+++ b/component/src/main/java/com/yahoo/component/Version.java
@@ -119,11 +119,15 @@ public final class Version implements Comparable<Version> {
* @throws IllegalArgumentException If <code>version</code> is improperly formatted.
*/
public Version(String versionString) {
- if (! "".equals(versionString)) {
- String[] components=versionString.split("\\."); // Split on dot
- major = (components.length > 0) ? Integer.parseInt(components[0]) : 0;
- minor = (components.length > 1) ? Integer.parseInt(components[1]) : 0;
- micro = (components.length > 2) ? Integer.parseInt(components[2]) : 0;
+ if (!versionString.isEmpty()) {
+ String[] components = versionString.split("\\."); // Split on dot
+ try {
+ major = (components.length > 0) ? Integer.parseInt(components[0]) : 0;
+ minor = (components.length > 1) ? Integer.parseInt(components[1]) : 0;
+ micro = (components.length > 2) ? Integer.parseInt(components[2]) : 0;
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Invalid version component in '" + versionString + "'", e);
+ }
qualifier = (components.length > 3) ? components[3] : "";
if (components.length > 4)
throw new IllegalArgumentException("Too many components in '" + versionString + "'");
@@ -199,7 +203,7 @@ public final class Version implements Comparable<Version> {
/** Returns new Version(versionString), or Version.emptyVersion if the input string is null or "" */
public static Version fromString(String versionString) {
- return (versionString == null) ? emptyVersion :new Version(versionString);
+ return versionString == null ? emptyVersion : new Version(versionString);
}
public Version withQualifier(String qualifier) {
@@ -315,7 +319,7 @@ public final class Version implements Comparable<Version> {
if (this.major != other.major) return false;
if (this.minor != other.minor) return false;
if (this.micro != other.micro) return false;
- return (this.qualifier.equals(other.qualifier));
+ return this.qualifier.equals(other.qualifier);
}
@SuppressWarnings("unused")
@@ -382,7 +386,7 @@ public final class Version implements Comparable<Version> {
/** Creates a version specification that only matches this version */
public VersionSpecification toSpecification() {
- return (this == emptyVersion)
+ return this == emptyVersion
? VersionSpecification.emptyVersionSpecification
: new VersionSpecification(getMajor(), getMinor(), getMicro(), getQualifier());
}