Code Monkey home page Code Monkey logo

version-compare's Introduction

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.

version-compare's People

Contributors

g00fy2 avatar janakaaleph avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

version-compare's Issues

Feature request: isEqual(String or Version, VersionComparator.XXX)

Hi, ๐Ÿ˜„.
Just like these below:

boolean result;

result = new Version("1.0.0").isEqual("1.2.0", VersionComparator.MAJOR); // result = true
result = new Version("1.9.0").isEqual("2.0.0", VersionComparator.MAJOR); // result = false

result = new Version("1.0.0").isEqual("1.0.2", VersionComparator.MINOR); // result = true
result = new Version("1.3.0").isEqual("1.0.0", VersionComparator.MINOR); // result = false

result = new Version("1.0.0").isEqual("1.0.0-rc4", VersionComparator.PATCH); // result = true
result = new Version("1.0.0").isEqual("1.0.5", VersionComparator.PATCH); // result = false

Request module creation for java 9+ projects.

Hi!
The reason of add this feature s allow to projects that uses java 9+ module feature recognize the module.

I tried to create a pull request but I could not.
Here is the module:

module versioncompare {
    requires org.jetbrains.annotations;
    exports io.github.g00fy2.versioncompare;
}

Some libraries requires of this.

can't get latest version from maven repository

Hi, I'm trying to include versioncompare to my project as a maven dependency. But I can't get latest version: 1.3.2

IDE: Intellij Idea CE

version-1 2 7

Here remote jar repositories configuration:
remote-jar-repositories

Version 1.3.5 has some weird behaviour

Hi There - Using the 1.3.5 sample app (and also the 1.3.5 library) I am seeing these issues

1.1.1 should be higher than 1.0.2
image

1.1.1 should be higher than 0.2.2
image

[JUnit] Version has been compiled by a more recent version of the Java Runtime (class file version 55.0)

Hi g00fy, we've been using version-compare for quite some time now and the new bump highlighted an incompatibility with our unit tests

java.lang.UnsupportedClassVersionError: com/g00fy2/versioncompare/Version has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

As I understand, you would have compiled with Java 11? Is there changes we need with our junit setup to ensure compatibility going forward?

Our Junit 4 configuration is using Android API 28 Platform for the JRE.

I can migrate the tests to espresso as version-compare compiles and runs fine in app. Thanks.

Support for "v" prefixes

Please add support for version names starting with "v", eg: "v1.0.0", "v1.0.1". That would be great!

Unexpected behavior: 106.0a1 and 106.0a2 are equal

If a version number contains a letter, the rest after the letter is ignored.
Version("106.0a1", true) == Version("106.0a2", true) evaluates to true
Version("106.0.a.1", true) == Version("106.0.b.2", true) evaluates to true

My solution (I don't know it is good enough) is to convert every letter to its Unicode code + own subversion:

val rawVersion = "106.0a1"
        var cleanedUpVersion = ""
        for (character: Char in rawVersion) {
            if (character.isDigit() || character == '.') {
                cleanedUpVersion += character
            } else {
                if (!cleanedUpVersion.endsWith('.')) {
                    cleanedUpVersion += '.'
                }
                cleanedUpVersion += character.code.toString()
                cleanedUpVersion += '.'
            }
        }

This code converts "106.0a1" to "106.0.97.1"

making release bigger that pre-releases

i have this comparing in my app, "1.0.3" and "1.0.3-rc1"

i want users to have the possible latest version, so i'm using .isAtLeast() to compare, which i think returning a wrong answer. here is my code:

Boolean isLatest = new Version("1.0.3-rc1").isAtLeast("1.0.3");

since "1.0.3-rc1" is a pre-release, so it shouldn't be the latest release when comparing with "1.0.3".

i might be mistaking, but this is the logic i could understand. Fix my thoughts if i'm wrong.

i have found this docs explaining release hierarchy.
link: https://semver.org/#spec-item-11

Maintain two major versions in parallel

Hi again :)

This isn't an issue, as much it's a new feature suggestion.

Now I'm planning to maintain two major branches in my app as this article is indicating :
Link: GitTools/GitVersion#1309
Link: https://gitversion.readthedocs.io/en/latest/git-branching-strategies/gitflow-examples/#support-branches

They could be 2.x or 1.3.x or why not many others.

I want to control each branch minor version supported for end users.

For example, if the user is using 1.3.x, he must have at least v1.3.15.

But if using 2.x, he must have at least v2.1.3

As a suggestion, it could be :

new VersionCompare("1.3.47").with("1.3.x").isAtLeast("1.3.15"); // true
new VersionCompare("1.3.47").with("2.x").isAtLeast("1.3.15"); // false

I hope this feature will be accepted. :-)

Support for SNAPSHOT versions

Hi there,

Some projects have a versioning like "1.1-SNAPSHOT" for active development version very unstable. It is way before PRE-ALPHA.

Could you support it?

Thanks,

No hashCode() implementation

The Version class is missing the hashCode() method while having the equals() method, resulting in dangerous behavior when used inside a collection such as set.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.