Java Version Convert To versionStringToInt(String version)

Here you can find the source of versionStringToInt(String version)

Description

Converts a version number string to an integer for easy comparison.

License

Open Source License

Return

major * 1000000 + minor * 1000 + micro

Declaration

public static int versionStringToInt(String version) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w w w  .  j  a v  a2s.c o m*/
     * Converts a version number string to an integer for easy comparison.
     * The version number must start with numbers separated with
     * dots. There can be any number of such dot-separated numbers, but only
     * the first three will be considered. After the numbers arbitrary text can
     * follow, and will be ignored.
     * 
     * The string will be trimmed before interpretation.
     * 
     * @return major * 1000000 + minor * 1000 + micro
     */
    public static int versionStringToInt(String version) {
        version = version.trim();

        int[] parts = new int[3];
        int partIdx = 0;
        boolean valid = false;
        for (int i = 0; i < version.length(); i++) {
            char c = version.charAt(i);
            if (c >= '0' && c <= '9') {
                parts[partIdx] = parts[partIdx] * 10 + (c - '0');
                valid = true;
            } else {
                if (c == '.') {
                    if (partIdx == 2)
                        break;
                    else
                        partIdx++;
                } else {
                    break;
                }
            }
        }
        if (!valid)
            throw new IllegalArgumentException(
                    "A version number string " + jQuote(version) + " must start with a number.");
        return parts[0] * 1000000 + parts[1] * 1000 + parts[2];
    }

    /**
     * Converts the parameter with <code>toString</code> (if not
     * <code>null</code>)and passes it to {@link #jQuote(String)}. 
     */
    public static String jQuote(Object obj) {
        return jQuote(obj != null ? obj.toString() : null);
    }

    /**
     * Quotes string as Java Language string literal.
     * Returns string <code>"null"</code> if <code>s</code>
     * is <code>null</code>.
     */
    public static String jQuote(String s) {
        if (s == null) {
            return "null";
        }
        int ln = s.length();
        StringBuffer b = new StringBuffer(ln + 4);
        b.append('"');
        for (int i = 0; i < ln; i++) {
            char c = s.charAt(i);
            if (c == '"') {
                b.append("\\\"");
            } else if (c == '\\') {
                b.append("\\\\");
            } else if (c < 0x20) {
                if (c == '\n') {
                    b.append("\\n");
                } else if (c == '\r') {
                    b.append("\\r");
                } else if (c == '\f') {
                    b.append("\\f");
                } else if (c == '\b') {
                    b.append("\\b");
                } else if (c == '\t') {
                    b.append("\\t");
                } else {
                    b.append("\\u00");
                    int x = c / 0x10;
                    b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
                    x = c & 0xF;
                    b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
                }
            } else {
                b.append(c);
            }
        } // for each characters
        b.append('"');
        return b.toString();
    }
}

Related

  1. versionStringToInt(String version)
  2. versionStringToNumber(String versionStr)
  3. versionToCode(String version)
  4. versionToSegments(String versionString, int idx, int i)