Skip to content

Lightweight Android & Java library to compare version strings.

License

Notifications You must be signed in to change notification settings

janakaaleph/version-compare

 
 

Repository files navigation

Version Compare Download Android Arsenal codecov

Lightweight Android & Java library 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.

Usage

Gradle:

dependencies {
  implementation 'com.g00fy2:versioncompare:1.3.3'
}

Maven:

<dependency>
  <groupId>com.g00fy2</groupId>
  <artifactId>versioncompare</artifactId>
  <version>1.3.3</version>
</dependency>

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.

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) 2018 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.

About

Lightweight Android & Java library to compare version strings.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%