version4j

Library to help compare major.minor.patch versions

License

License

GroupId

GroupId

com.kcthota
ArtifactId

ArtifactId

version4j
Last Version

Last Version

4.0
Release Date

Release Date

Type

Type

jar
Description

Description

version4j
Library to help compare major.minor.patch versions
Project URL

Project URL

https://github.com/kcthota/version4j
Source Code Management

Source Code Management

https://github.com/kcthota/version4j

Download version4j

How to add to project

<!-- https://jarcasting.com/artifacts/com.kcthota/version4j/ -->
<dependency>
    <groupId>com.kcthota</groupId>
    <artifactId>version4j</artifactId>
    <version>4.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.kcthota/version4j/
implementation 'com.kcthota:version4j:4.0'
// https://jarcasting.com/artifacts/com.kcthota/version4j/
implementation ("com.kcthota:version4j:4.0")
'com.kcthota:version4j:jar:4.0'
<dependency org="com.kcthota" name="version4j" rev="4.0">
  <artifact name="version4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.kcthota', module='version4j', version='4.0')
)
libraryDependencies += "com.kcthota" % "version4j" % "4.0"
[com.kcthota/version4j "4.0"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 3.2.0

Project Modules

There are no modules declared in this project.

version4j

=============

Build Status Coverage Status Maven Central

Library to help compare major.minor.patch versions.

Implements the standard Comparable and Comparator Interfaces.

Usage

Stable:

<dependency>
	<groupId>com.kcthota</groupId>
	<artifactId>version4j</artifactId>
	<version>4.0</version>
</dependency>

Examples

Parse Version

Attempts to parse the String to Version Object. Throws VersionNotValidException if fails to parse the String.

Currently only support major.minor.patch format. And the value of any of these parts cannot be greater than 100000.

Version.parseVersion("1.1.2"); //parsed as 1.1.2

Version.parseVersion("1"); //parsed as 1.0.0

Version.parseVersion("1.2"); //parsed as 1.2.0

Version.parseVersion("1.2.3c"); //parsed as 1.2.3

Version.parseVersion("1.2.3c"); //parsed as 1.2.3

Version.parseVersion("1.2.3.4.5"); //parsed as 1.2.3

Version.parseVersion(" "); //throws VersionNotValidException

Version.parseVersion(null); //throws VersionNotValidException

Version.parseVersion("100001.0.0"); //throws VersionNotValidException

Try Parse

Tries to parse the passed String. Returns true if successfully parses the String or returns false.

This method does not throw an exception.

Version.tryParse("1.1.2"); //true

Version.tryParse("1"); //true

Version.tryParse("1.2"); //true

Version.tryParse("1.2.3c"); //true

Version.tryParse("1.2.3c"); //true

Version.tryParse("1.2.3.4.5"); //true

Version.tryParse(" "); //false

Version.tryParse(null); //false

Version.tryParse("100001.0.0"); //false

CompareTo

Standard Comparable interface

Version v1 = new Version("1.2.3");
		
Version v2 = Version.parseVersion("1.2");
		
v1.compareTo(v2); //1

Greater than

Version v1 = new Version("0.1.9");
		
Version v2 = new Version("0.0.9");
		
v1.greaterThan(v2); //true

Less than

Version v1 = new Version("2.0.9");
		
Version v2 = new Version("10.0.1");
		
v1.lessThan(v2); //true

Sort Versions

Sorts the versions in ascending order

Version v1 = new Version("2.0.0");
Version v2 = new Version("0.095.3");
Version v3 = new Version("10.0.1");
Version v4 = new Version("0.0.1");

List<Version> versions = new ArrayList<Version>();
versions.add(v1);
versions.add(v2);
versions.add(v3);
versions.add(v4);

versions.sort(new VersionComparator());

#License

The MIT License

Copyright 2016 Krishna Chaitanya Thota. All rights reserved.

#References

Inspired by Version class from .NET framework at https://msdn.microsoft.com/en-us/library/system.version(v=vs.100).aspx

Versions

Version
4.0
3.0
2.0
1.0