Java Version Compare VersionComparer(String a, String b, boolean includeEqual)

Here you can find the source of VersionComparer(String a, String b, boolean includeEqual)

Description

Version Comparer

License

Open Source License

Declaration

public static boolean VersionComparer(String a, String b, boolean includeEqual) 

Method Source Code

//package com.java2s;
/*/*from  w w w  .j  ava  2 s  .c o m*/
 * Copyright 2013 etao.com All right reserved. This software is the
 * confidential and proprietary information of etao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with etao.com .
 */

public class Main {

    public static boolean VersionComparer(String a, String b, boolean includeEqual) {
        if (a == null || b == null) {
            return false;
        }
        String regex = "^([0-9]+\\.)+([0-9]+)$";
        if (!a.matches(regex) || !b.matches(regex)) {
            return false;
        }
        if (a.equals(b)) {
            return includeEqual;
        }
        String as[] = a.split("\\.");
        String bs[] = b.split("\\.");
        int i = 0;
        while (as.length > i && bs.length > i) {
            int ai = Integer.parseInt(as[i]);
            int bi = Integer.parseInt(bs[i]);
            if (ai > bi) {
                return true;
            }
            if (ai < bi) {
                return false;
            }
            i++;
        }
        while (as.length > i) {
            int ai = Integer.parseInt(as[i]);
            if (ai > 0) {
                return true;
            }
            i++;
        }
        return false;
    }
}

Related

  1. versionCompare(String fromVersion, String toVersion)
  2. versionCompare(String str1, String str2)
  3. versionCompare(String str1, String str2)
  4. versionCompare(String userVersion, String supportVersion)
  5. versionCompare(String ver1, String ver2)