diff --git a/common/src/main/java/io/github/milkdrinkers/javasemver/Version.java b/common/src/main/java/io/github/milkdrinkers/javasemver/Version.java index a5f5a28..6304459 100644 --- a/common/src/main/java/io/github/milkdrinkers/javasemver/Version.java +++ b/common/src/main/java/io/github/milkdrinkers/javasemver/Version.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Objects; +import java.util.Optional; /** * A class representing a Semantic Version. @@ -77,6 +78,21 @@ public class Version extends VersionCompare implements Comparable { return VersionParser.parse(version); } + /** + * Create a Version object from a version string. + * + * @param version a string containing a semantic version + * @return a version object wrapped in a optional + * @apiNote Uses {@link VersionParser#parse(String)} internally + */ + public static @NotNull Optional ofOptional(String version) { + try { + return Optional.of(VersionParser.parse(version)); + } catch (Exception ignored) { + return Optional.empty(); + } + } + /** * Create a Version object from semantic version data. *