Java Version Compare versionCompare(int... args)

Here you can find the source of versionCompare(int... args)

Description

version Compare

License

Open Source License

Declaration

public static int versionCompare(int... args) 

Method Source Code

//package com.java2s;
/*/*  w  w w.  j  a  v  a2 s  .com*/
 * -----LICENSE START-----
 * JSHManager - A Java-based tool for managing one's ScoreHero account.
 * Copyright (C) 2008 Tim Mullin
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * -----LICENSE END-----
*/

public class Main {
    public static int versionCompare(int... args) {
        if (args.length < 2 || args.length % 2 != 0)
            throw new IllegalArgumentException("args must have a positive, even number of elements");

        int i = 0;
        String v1 = String.valueOf(args[i++]);

        for (; i < args.length / 2; i++)
            v1 += "." + args[i];

        String v2 = String.valueOf(args[i++]);

        for (; i < args.length; i++)
            v2 += "." + args[i];

        return versionCompare(v1, v2);
    }

    public static int versionCompare(String v1, String v2) {
        String[] parts1 = v1.split("\\.");
        String[] parts2 = v2.split("\\.");

        if (parts1.length != parts2.length)
            throw new IllegalArgumentException("v1 and v2 must have the same number of parts");

        int[] i1 = new int[parts1.length];
        int[] i2 = new int[parts2.length];

        for (int i = 0; i < i1.length; i++) {
            try {
                i1[i] = Integer.parseInt(parts1[i]);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("v1 contained an invalid string", e);
            }

            try {
                i2[i] = Integer.parseInt(parts2[i]);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("v2 contained an invalid string", e);
            }

            if (i1[i] != i2[i])
                return i1[i] < i2[i] ? -1 : 1;
        }

        return 0;
    }
}

Related

  1. versionCompare(final String current, final String remote)
  2. versionCompare(String firstVersionString, String secondVersionString)
  3. versionCompare(String fromVersion, String toVersion)
  4. versionCompare(String str1, String str2)
  5. versionCompare(String str1, String str2)