Skip to content

Lightweight library for Android, Java and Kotlin to compare version strings.

License

Notifications You must be signed in to change notification settings

G00fY2/version-compare

Repository files navigation

Version Compare GitHub Actions Coverage

Lightweight library for Android, Java and Kotlin to compare version strings.

This library allows you to easily compare version strings. Versions can but do not necessarily have to follow the SemVer convention. Any number of version parts as well as common pre-release suffixes will be taken into account.

Pure Java (java.util), no dependencies, very small method count.

Download Maven Central

Gradle
dependencies {
  implementation("io.github.g00fy2:versioncompare:1.5.0")
}
Maven
<dependency>
  <groupId>io.github.g00fy2</groupId>
  <artifactId>versioncompare</artifactId>
  <version>1.5.0</version>
</dependency>

⚠️ Starting with version 1.4.0 this library moved to MavenCentral. As a result the groupId had to be changed. If you use the old com.g00fy2:versioncompare artifact check out the migration guide in the release notes.

Usage

To compare two version strings just create a new Version object. Invalid inputs (null or non-numeric first char) will by default be handled as 0.0.0.

Kotlin

You can use the default comparison operators to compare version objects:

var result: Boolean

result = Version("1.2.1") > Version("1.2") // result = true

result = Version("1.0.2-rc2") < Version("1.0.2-rc3") // result = true

result = Version("1.3") == Version("1.3.0") // result = true

result = Version("2.1.0") >= Version("2.0") // result = true

result = Version("2.0.0-beta") >= Version("2.0") // result = false
result = Version("2.0.0-beta").isAtLeast("2.0", /* ignoreSuffix: */ true) // result = true
Java
boolean result;

result = new Version("1.2.1").isHigherThan("1.2"); // result = true

result = new Version("1.0.2-rc2").isLowerThan("1.0.2-rc3"); // result = true

result = new Version("1.3").isEqual("1.3.0"); // result = true

result = new Version("2.0.0-beta").isAtLeast("2.0"); // result = false
result = new Version("2.0.0-beta").isAtLeast("2.0", /* ignoreSuffix: */ true); // result = true

For more detailed usage, check out the documentation.

Version structure example

Version 1.7.3-rc2.xyz
            +-------+   +-------+   +-------+   +-------+
    String  |   1   | . |   7   | . | 3-rc2 | . |  xyz  |
            +-------+   +-------+   +-------+   +-------+
                |           |         |  |          |
  major  [1] <--            |         |   ----      |
  minor  [7] <--------------          |       | ----
  patch  [3] <------------------------        ||
         ...                            +------------+
                                suffix  |  -rc2.xyz  |
                                        +------------+
-------------------------------------------------------------------------
suffix compare logic                          ||
                                         -----  -----
                                        |            |
                                    +-------+    +-------+
              detected pre-release  |  rc2  |    | .xyz  |  ignored part
                                    +-------+    +-------+
                                       ||
                                    ---  ---
                                   |        |
                                +----+    +---+
                                | rc |    | 2 |  pre-release build
                                +----+    +---+

Notes:

  • expected separator between version numbers is .
  • suffix and pre-release number do not need a special separator 1.1rc == 1.1.rc == 1.1-rc

Supported pre-release suffixes

order suffix
5 empty or unknown
4 rc
3 beta
2 alpha
1 pre + alpha
0 snapshot

Notes:

  • higher order results in higher version 1.0 > 1.0-beta
  • pre-release builds (except for snapshots) are supported 1.0-rc3 > 1.0-rc2

Sample App

Image

Try out the sample app to compare your version inputs: Download APK

License

Copyright (C) 2021 Thomas Wirth

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.