Class VersionComparator

java.lang.Object
org.nuiton.version.VersionComparator
All Implemented Interfaces:
java.io.Serializable, java.util.Comparator<Version>

public class VersionComparator
extends java.lang.Object
implements java.util.Comparator<Version>, java.io.Serializable
Comparator of Version. You may be before this the documentation of Version object...

Version componant ordering

We can compare only componants of same type. Number componants use natural number ordering (0<1<2,...) (see Version.NumberVersionComponant). String componants use preRelease flag order (preRelease is before any other string componant) then natural character ordering of the lowerCase componant value. (see Version.StringVersionComponant).
alpha == Alpha, aa > rc

Snapshot ordering

A snapshot version is before the exact same version without the flag.
1.1-SNAPSHOT < 1.1

General ordering algorithm

We compare version componants at same position until there is a difference. If common componants of version are both equals, have a look to the next componant of the longuest version (if the version have the same componants size, Versions are equals!). If the next componant is a number, the longuest version is after the other one:
1 < 1.0
If the next componant is a classifier, we consider the componant preRelease flag, if setted then the longuest version is before the other one, if no, after:
1-alpha < 1 < 1-aa

Examples

Here is a list of ordered versions:
 0-SNAPSHOT
 0
 0.1
 0.2
 0.2-alpha
 0.2-alpha-1
 0.2-alpha-2-SNAPSHOT
 0.2-alpha-2
 0.2-beta
 0.2-beta-1
 0.2-rc-1
 0.2
 0.2-aa
 
Created on 7/11/14.
Since:
1.0
Author:
Tony Chemit - chemit@codelutin.com
See Also:
Version, Serialized Form
  • Constructor Summary

    Constructors 
    Constructor Description
    VersionComparator()  
  • Method Summary

    Modifier and Type Method Description
    int compare​(Version o1, Version o2)  
    static void main​(java.lang.String... args)
    Sort in reverse order the given versions and print them to the standard output.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.Comparator

    equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
  • Constructor Details

  • Method Details

    • compare

      public int compare​(Version o1, Version o2)
      Specified by:
      compare in interface java.util.Comparator<Version>
    • main

      public static void main​(java.lang.String... args)
      Sort in reverse order the given versions and print them to the standard output.
      Parameters:
      args - versions to sort