|
| 1 | +/* |
| 2 | + * This file is part of ECInventory |
| 3 | + * <https://github.com/EndlessCodeGroup/ECInventory>. |
| 4 | + * Copyright (c) 2022 EndlessCode Group and contributors |
| 5 | + * |
| 6 | + * ECInventory is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * ECInventory is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with ECInventory. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package ru.endlesscode.inventory.internal.compat |
| 21 | + |
| 22 | +/** |
| 23 | + * Constructs Version object from the given data according to semantic versioning. |
| 24 | + * |
| 25 | + * @param major Major version. |
| 26 | + * @param minor Minor version. |
| 27 | + * @param patch Patch version. |
| 28 | + * @param qualifier Qualifier, or empty string if qualifier not exists. |
| 29 | + * @throws IllegalArgumentException when passed negative version codes. |
| 30 | + */ |
| 31 | +internal data class Version( |
| 32 | + val major: Int, |
| 33 | + val minor: Int, |
| 34 | + val patch: Int, |
| 35 | + val qualifier: String = "", |
| 36 | +) : Comparable<Version> { |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns version code in format xxyyzz, where x - major version, y - minor and z - patch. |
| 40 | + * Example: |
| 41 | + * 1.12.2 -> 11202 |
| 42 | + * 21.0.12 -> 210012 |
| 43 | + * 1.9 -> 10900 |
| 44 | + * 2.2.1 -> 20201 |
| 45 | + * Major, minor and patch versions shouldn't be higher than 99. |
| 46 | + */ |
| 47 | + val versionCode: Int |
| 48 | + get() = major * 10000 + minor * 100 + patch |
| 49 | + |
| 50 | + init { |
| 51 | + require(major >= 0 && minor >= 0 && patch >= 0) { "Version can't include negative numbers" } |
| 52 | + } |
| 53 | + |
| 54 | + operator fun compareTo(other: Int): Int = versionCode.compareTo(other) |
| 55 | + operator fun compareTo(other: String): Int = this.compareTo(parseVersion(other)) |
| 56 | + |
| 57 | + override operator fun compareTo(other: Version): Int = COMPARATOR.compare(this, other) |
| 58 | + |
| 59 | + override fun toString(): String { |
| 60 | + val qualifier = if (qualifier.isEmpty()) "" else "-$qualifier" |
| 61 | + return "$major.$minor.$patch$qualifier" |
| 62 | + } |
| 63 | + |
| 64 | + companion object { |
| 65 | + |
| 66 | + private val COMPARATOR = compareBy<Version> { it.major } |
| 67 | + .thenBy { it.minor } |
| 68 | + .thenBy { it.patch } |
| 69 | + |
| 70 | + /** |
| 71 | + * Parses version from the given string. |
| 72 | + * |
| 73 | + * @param version String representation of version. |
| 74 | + * @return The constructed version. |
| 75 | + * @throws IllegalArgumentException If passed string with wrong format. |
| 76 | + */ |
| 77 | + fun parseVersion(version: String): Version { |
| 78 | + var parts = version.split("-", limit = 2) |
| 79 | + val qualifier = if (parts.size > 1) parts[1] else "" |
| 80 | + |
| 81 | + parts = parts[0].split("\\.", limit = 3) |
| 82 | + return try { |
| 83 | + val patch = if (parts.size > 2) parts[2].toInt() else 0 |
| 84 | + val minor = if (parts.size > 1) parts[1].toInt() else 0 |
| 85 | + val major = parts[0].toInt() |
| 86 | + Version(major, minor, patch, qualifier) |
| 87 | + } catch (e: NumberFormatException) { |
| 88 | + throw IllegalArgumentException("Can not parse version string: $version", e) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments