Java List Reverse compareVersions(String version1, String version2)

Here you can find the source of compareVersions(String version1, String version2)

Description

Compares two versions strings (ex.

License

Open Source License

Return

-1 if version1 < version2, 1 if version1 > version2, 0 if version1 = version2

Declaration

public static int compareVersions(String version1, String version2) 

Method Source Code

//package com.java2s;
/*/*w  w w  .  ja  va  2 s.c  o m*/
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

public class Main {
    /**
     * Compares two versions strings (ex. 1.6.1)
     * 
     * @return -1 if version1 < version2, 1 if version1 > version2, 0 if
     *         version1 = version2
     */
    public static int compareVersions(String version1, String version2) {
        return compareVersions(version1, version2, 3);
    }

    /**
     * Compares two versions strings (ex. 1.6.1)
     * 
     * @return -1 if version1 < version2, 1 if version1 > version2, 0 if
     *         version1 = version2
     */
    public static int compareVersions(String version1, String version2,
            int length) {
        if ((version1 == null) && (version2 == null)) {
            return 0;
        } else if ((version1 != null) && (version2 == null)) {
            return 1;
        } else if ((version1 == null) && (version2 != null)) {
            return -1;
        } else {
            String[] numbers1 = normalizeVersion(version1, length).split(
                    "\\.");
            String[] numbers2 = normalizeVersion(version2, length).split(
                    "\\.");

            for (int i = 0; i < numbers1.length; i++) {
                if (Integer.valueOf(numbers1[i]) < Integer
                        .valueOf(numbers2[i])) {
                    return -1;
                } else if (Integer.valueOf(numbers1[i]) > Integer
                        .valueOf(numbers2[i])) {
                    return 1;
                }
            }
        }

        return 0;
    }

    /**
     * Normalizes a version string so that it has 'length' number of version numbers separated by '.'
     */
    public static String normalizeVersion(String version, int length) {
        if (version == null) {
            return null;
        }

        List<String> numbers = new ArrayList<String>(Arrays.asList(version
                .split("\\.")));

        while (numbers.size() < length) {
            numbers.add("0");
        }

        StringBuilder builder = new StringBuilder();

        for (ListIterator<String> iterator = numbers.listIterator(); iterator
                .hasNext() && iterator.nextIndex() < length;) {
            String number = iterator.next();

            if (iterator.hasNext() && iterator.nextIndex() < length) {
                builder.append(number + ".");
            } else {
                builder.append(number);
            }
        }

        return builder.toString();
    }
}

Related

  1. addReverse(List list, List append)
  2. compareVersions(List list1, List list2)
  3. compareVersions(List version1, List version2)
  4. createReversedList(List inputList)
  5. firstElementOf(List list, int reverseIndexPosition)
  6. formatToIp(final List shortList, final boolean reverseOrder)
  7. getReversedListIterable(final List inList)