Example usage for java.lang Integer signum

List of usage examples for java.lang Integer signum

Introduction

In this page you can find the example usage for java.lang Integer signum.

Prototype

public static int signum(int i) 

Source Link

Document

Returns the signum function of the specified int value.

Usage

From source file:uk.ac.ucl.cs.cmic.giftcloud.util.GiftCloudUtils.java

/**
 * Compares two version strings, e.g. "1.3.1" "1.3.2" etc. Non-numeric sub-versions are permitted provided they are equal in both strings
 *
 * @param versionString1/*from   ww  w.j  av  a  2 s  . c o  m*/
 * @param versionString2
 * @return 1 if version1 is greater than version2, -1 if version1 is less than version 2, or 0 if they are equal
 */
public static int compareVersionStrings(final String versionString1, final String versionString2) {
    final String[] versionNumbers1 = StringUtils.isBlank(versionString1) ? new String[] {}
            : versionString1.split("\\.", -1);
    final String[] versionNumbers2 = StringUtils.isBlank(versionString2) ? new String[] {}
            : versionString2.split("\\.", -1);

    int compareIndex = 0;
    while (compareIndex < versionNumbers1.length && compareIndex < versionNumbers2.length) {
        final String subString1 = versionNumbers1[compareIndex];
        final String subString2 = versionNumbers2[compareIndex];
        if (subString1.equals(subString2)) {
            compareIndex++;
        } else {
            return Integer.signum(Integer.valueOf(subString1).compareTo(Integer.valueOf(subString2)));
        }
    }

    // If we get here it means the strings are equal or they have different numbers of substrings
    return Integer.signum(versionNumbers1.length - versionNumbers2.length);
}