Here you can find the source of compareVersions(List
static protected int compareVersions(List<Integer> list1, List<Integer> list2)
//package com.java2s; /******************************************************************************* * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution.// w ww . j ava2s. c o m * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * Oracle - initial API and implementation from Oracle TopLink * dminsky - added countOccurrencesOf(Object, List) API * 08/23/2010-2.2 Michael O'Brien * - 323043: application.xml module ordering may cause weaving not to occur causing an NPE. * warn if expected "_persistence_*_vh" method not found * instead of throwing NPE during deploy validation. ******************************************************************************/ import java.util.ArrayList; import java.util.List; public class Main { /** * INTERNAL: * Compares two version in num.num.num.num.num*** format. * -1, 0, 1 means the version1 is less than, equal, greater than version2. * Example: compareVersions("11.1.0.6.0-Production", "11.1.0.7") == -1 * Example: compareVersions("WebLogic Server 10.3.4", "10.3.3.0") == 1 */ public static int compareVersions(String version1, String version2) { return compareVersions(version(version1), version(version2)); } /** * INTERNAL: * Compares two lists of Integers * -1, 0, 1 means the first list is less than, equal, greater than the second list. * Example: {11, 1, 0, 6, 0} < {11, 1, 0, 7} */ static protected int compareVersions(List<Integer> list1, List<Integer> list2) { int n = Math.max(list1.size(), list2.size()); int res = 0; for (int i = 0; i < n; i++) { int l1 = 0; if (i < list1.size()) { l1 = list1.get(i); } int l2 = 0; if (i < list2.size()) { l2 = list2.get(i); } if (l1 < l2) { res = -1; break; } else if (l1 > l2) { res = 1; break; } } return res; } /** * INTERNAL: * Expects version in ***num.num.num.num.num*** format, converts it to a List of Integers. * Example: "11.1.0.6.0_Production" -> {11, 1, 0, 6, 0} * Example: "WebLogic Server 10.3.3.0" -> {10, 3, 3, 0} */ static protected List<Integer> version(String version) { ArrayList<Integer> list = new ArrayList<Integer>(5); // first char - a digit - in the string corresponding to the current list index int iBegin = -1; // used to remove a non-digital prefix boolean isPrefix = true; for (int i = 0; i < version.length(); i++) { char ch = version.charAt(i); if ('0' <= ch && ch <= '9') { isPrefix = false; // it's a digit if (iBegin == -1) { iBegin = i; } } else { // it's not a digit - try to create a number ending on the previous char - unless it's still part of the non-digital prefix. if (iBegin == -1) { if (!isPrefix) { break; } } else { isPrefix = false; String strNum = version.substring(iBegin, i); int num = Integer.parseInt(strNum, 10); list.add(num); iBegin = -1; if (ch != '.') { break; } } } } if (iBegin >= 0) { String strNum = version.substring(iBegin, version.length()); int num = Integer.parseInt(strNum, 10); list.add(num); } return list; } }