Java implements client version size judgment

        Due to the fragmentation of mobile client systems and versions, many back-end businesses inevitably have to deal with version compatibility. Every released app will carry this version on the request service to ensure that the server can achieve compatibility and service between new and old versions. isolate.

 

        In the process of processing the version number, there are many times when it is necessary to judge the size of the old and new versions, so I wrote a code for version processing for terminals of multiple apps such as android, iphone, and ipad. The function of management between service partitions.

 

/**
     * This method compares the configured appName and version map with the requested appNameVersion,
     * In this way, the business logic of sharding and outputting different versions of different system terminals
     * For example, mytest_iphone_1.3.0 and mytest_iphone_1.3.0.11, mytest_iphone_1.2.2 compare the size of the version
     * @param appNameVersionMap When there are two systems, IOS and android, you can configure the map to compare with the current app version, you can configure the reference values ​​mytest_android_5.3.0, mytest_iphone_1.3.2, mytest_ipad_1.3.0
     * @param appNameVersion like 12345@mytest_iphone_1.3.0
     * @return true or false one of the values ​​of appNameVersion>=appNameVersionMap, the method returns true, otherwise returns false;
     */
    public static boolean compareAppNameAndVersionMapWithAppNameVersion(Map<String, String> appNameVersionMap, String appNameVersion) {
        char[] appNameVersionCharArray = appNameVersion.toCharArray();
        for (Map.Entry<String, String> entry : appNameVersionMap.entrySet()) {
            String clientName = entry.getKey(); //"taobao_iphone" : 1.2.0.1 "taobao_android" : "3.0.2" appNameVersion=taobao_iphone_1.0.0
            String version = entry.getValue();
            char[] clientCharArr = clientName.toCharArray();
            char[] versionCharArr = version.toCharArray();

            int i = isContainsCharArr(appNameVersionCharArray, clientCharArr);
            if (i < appNameVersionCharArray.length) {
                boolean isBigger = true;
                boolean goOn = true;
                int j = 0;
                while (goOn) {
                    if (j < versionCharArr.length && !isNumbericChar(versionCharArr[j])) {
                        j++;
                    }
                    if (i < appNameVersionCharArray.length && !isNumbericChar(appNameVersionCharArray[i])) {
                        i++;
                    }
                    if (j >= versionCharArr.length) {
                        /**myapp_android#4.2.2.0.1 special case compared to 1234@myapp_android_4.2.2.0*/
                        while (i < appNameVersionCharArray.length) {
                            if (appNameVersionCharArray[i] > '0' && appNameVersionCharArray[i] <= '9') {
                                return true;
                            }
                            i++;
                        }
                        return true;
                    }
                    if (i >= appNameVersionCharArray.length) {
                        /**myapp_android#4.2.2.0 special case compared to 1234@myapp_android_4.2.2.0.1*/
                        while (j < versionCharArr.length) {
                            if (versionCharArr[j] > '0' && versionCharArr[j] <= '9') {
                                return false;
                            }
                            j++;
                        }
                        return true;
                    }
                    if (isNumbericChar(appNameVersionCharArray[i]) && isNumbericChar(versionCharArr[j])) {
                        int ttidNextVersionPlace = getNextVersionNumberPlace(appNameVersionCharArray, i);
                        int verNextVersionPlace = getNextVersionNumberPlace(versionCharArr, j);
                        if (ttidNextVersionPlace == verNextVersionPlace) {
                            if (appNameVersionCharArray[i] > versionCharArr[j]) {
                                return true;
                            }
                            if (appNameVersionCharArray[i] < versionCharArr[j]) {
                                return false;
                            }
                            i++;
                            j++;
                        } else {
                            return ttidNextVersionPlace > verNextVersionPlace;
                        }
                    }
                }
                return isBigger;
            }
        }
        return false;
    }

    /***
     * Version numbers may be multi-digit numbers such as 5.2.6.6 and 5.2.6.10
     * Get the number of digits of the next version number
     */
    public static int getNextVersionNumberPlace(char[] charArray, int i) {
        int count = 1;
        while (i < charArray.length - 1 && isNumbericChar(charArray[i + 1])) {
            i++;
            count++;
        }
        return count;
    }

    /**
     * Return the next index that matches successfully
     * The method is not empty note
     **/
    private static int isContainsCharArr(char[] charArr, char[] searchArr) {
        int i = 0;
        while (i < charArr.length) {
            int j = 0;
            while (i < charArr.length && j < searchArr.length && charArr[i++] == searchArr[j++]) ;
            if (j == searchArr.length && i < charArr.length) {
                break;
            }
        }
        return i;
    }

    private static boolean isNumbericChar(char ch) {
        return ch >= '0' && ch <= '9';
    }

 

   If you want to implement interval judgment, you only need to wrap the above method.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614599&siteId=291194637