Skip to content

A library for parsing and comparing version strings

License

Notifications You must be signed in to change notification settings

solo-studios/Strata

Repository files navigation

Strata

Build Status MIT license Maven Central 100% Java Discord Server

A simple, dependency-less, library for parsing and comparing versions according to the SemVer spec.

Features

  • Can parse any valid SemVer version.
  • Has no external dependencies other than JetBrains Annotations to help document the public API.
  • Very fast. Can parse 1 million versions in under 5 seconds. (2.1 seconds on my cpu, a Ryzen 5 3600.) At that level, speed is insignificant, unless you have a really weird usecase where you need to parse hundreds of thousands of versions a second.
  • Simple to use API.
  • Includes a set of Kotlin extensions to make development in kotlin easier and more idiomatic.

Including

Builds can be found on Maven Central, and can be included with any build tool that supports maven.

<dependency>
  <groupId>ca.solo-studios</groupId>
  <artifactId>strata</artifactId>
  <version>[strata version]</version>
</dependency>

The kotlin extensions can be included as follows:

<dependency>
  <groupId>ca.solo-studios</groupId>
  <artifactId>strata-kotlin</artifactId>
  <version>[strata version]</version>
</dependency>

Gradle Groovy DSL

implementation 'ca.solo-studios:strata:[strata version]'

The kotlin extensions can be included as follows:

implementation 'ca.solo-studios:strata-kotlin:[strata version]'

Gradle Kotlin DSL

implementation("ca.solo-studios:strata:[strata version]")

The kotlin extensions can be included as follows:

implementation("ca.solo-studios:strata-kotlin:[strata version]")

Examples

Note: version parsing may throw a ParseException, which you are expected to handle. This is thrown if ever the provided version is incorrect.

Getting a version

try {
    Version version = Versions.parseVersion("1.2.3");
} catch (ParseException e) {
    // handle version parse exception
}

or

val version = "1.2.3".toVersion()

Getting a version range

try {
    VersionRange range = Versions.parseVersionRange("[1.2.3,4.5.6)");
} catch (ParseException e) {
    // handle invalid version range parse exception
}

or

val range = "[1.2.3,4.5.6)".toVersionRange()

Checking if a version is within a version range

Version version = [...]

VersionRange range = [...]

if (range.isSatisfiedBy(version)) {
    // version is within range
} else {
    // version is outside range
}

or

val version = [...]

val range = [...]

if (version in range)
    // version is within range
else
    // version is outside range