+class UbuntuKVersion implements Comparable<UbuntuKVersion> {
+
+ Integer major = 0
+ Integer minor = 0
+ Integer patch = 0
+ Integer umajor = 0
+ Integer uminor = 0
+ String suffix = ""
+ Boolean isLTS = false
+
+ UbuntuKVersion() {}
+
+ UbuntuKVersion(version) {
+ this.parse(version)
+ }
+
+ static UbuntuKVersion minKVersion() {
+ return new UbuntuKVersion("Ubuntu-lts-0.0.0-0.0")
+ }
+
+ static UbuntuKVersion maxKVersion() {
+ return new UbuntuKVersion("Ubuntu-" + Integer.MAX_VALUE + ".0.0-0.0")
+ }
+
+ static UbuntuKVersion factory(version) {
+ return new UbuntuKVersion(version)
+ }
+
+ def parse(version) {
+ this.major = 0
+ this.minor = 0
+ this.patch = 0
+ this.umajor = 0
+ this.uminor = 0
+ this.suffix = "";
+ this.isLTS = false
+
+ if (!version) {
+ throw new EmptyKVersionException("Empty kernel version")
+ }
+
+ //'Ubuntu-lts-4.8.0-27.29_16.04.1',
+ //'Ubuntu-4.4.0-70.91',
+ def match = version =~ /^Ubuntu-(lts-)??(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)(.*)??$/
+ if (!match) {
+ throw new InvalidKVersionException("Invalid kernel version: ${version}")
+ }
+
+ this.isLTS = match.group(1) != null
+
+ // Major
+ this.major = Integer.parseInt(match.group(2))
+
+ // Minor
+ this.minor = Integer.parseInt(match.group(3))
+
+ // Patch level
+ this.patch = Integer.parseInt(match.group(4))
+
+ // Ubuntu major
+ this.umajor = Integer.parseInt(match.group(5))
+
+ // Ubuntu minor
+ this.uminor = Integer.parseInt(match.group(6))
+
+ if (match.group(7) != null) {
+ this.suffix = match.group(7)
+ }
+ }
+
+ // Return true if this version is a release candidate
+ Boolean isRC() {
+ return false
+ }
+
+ // Return true if both version are of the same stable branch
+ Boolean isSameStable(UbuntuKVersion o) {
+ if (this.isLTS != o.isLTS) {
+ return false
+ }
+ if (this.major != o.major) {
+ return false
+ }
+ if (this.minor != o.minor) {
+ return false
+ }
+ if (this.patch != o.patch) {
+ return false
+ }
+
+ return true
+ }
+
+ @Override int compareTo(UbuntuKVersion o) {
+ if (this.major != o.major) {
+ return Integer.compare(this.major, o.major)
+ }
+ if (this.minor != o.minor) {
+ return Integer.compare(this.minor, o.minor)
+ }
+ if (this.patch != o.patch) {
+ return Integer.compare(this.patch, o.patch)
+ }
+ if (this.umajor != o.umajor) {
+ return Integer.compare(this.umajor, o.umajor)
+ }
+ if (this.uminor != o.uminor) {
+ return Integer.compare(this.uminor, o.uminor)
+ }
+ if (this.isLTS != o.isLTS) {
+ if (o.isLTS) {
+ return 1
+ } else {
+ return -1
+ }
+ }
+
+ // Same version
+ return 0;
+ }
+
+ String toString() {
+ String vString = "Ubuntu-"
+
+ if (this.isLTS) {
+ vString = vString.concat("lts-")
+ }
+
+ vString = vString.concat("${this.major}.${this.minor}.${this.patch}-${this.umajor}.${this.uminor}${this.suffix}")
+
+ return vString
+ }
+}
+