数字类型版本号比较,包含 ||,&& 与/或表达式的计算

自己记录一下,笔记:

public static void main(String[] args) {

    String a = "3.12";
            String b = "=5.5.28||>=3.2.5&&<=3.23.9";
    System.out.println(isExist(b,a));

}

private static boolean isExist(String expression,String parm){
    if(expression.contains("||")){

        String[] sub1s = expression.split("\\|\\|");

        int a = 0;
        for (int i=0;i<sub1s.length;i++){


            if(sub1s[i].contains("&&")){

                String[] sub2s = sub1s[i].split("\\&\\&");


                int c = 1;

                for (int ii=0;ii<sub2s.length;ii++){

                    if(sub2s[ii].indexOf(">=")>-1){
                        c = c*compareVersion(parm,sub2s[ii],">=");
                    }else if(sub2s[ii].indexOf("<=")>-1){
                        c = c*compareVersion(parm,sub2s[ii],"<=");
                    }else if(sub2s[ii].indexOf(">")>-1){
                        c = c*compareVersion(parm,sub2s[ii],">");
                    }else if(sub2s[ii].indexOf("<")>-1){
                        c = c*compareVersion(parm,sub2s[ii],"<");
                    }else if (sub2s[ii].indexOf("=")>-1){
                        c = c*compareVersion(parm,sub2s[ii],"=");
                    }
                }
                a=a+c;
            }else if(sub1s[i].contains("=")){

                a = a+ compareVersion(parm,sub1s[i],"=");

            }


        }
        return a>0;

    }else if(expression.contains("&&")){


    }else{

    }
    return false;
}

private static int compareVersion(String parm,String expression,String sign){

    String[] vs = parm.split("\\.");

    expression = expression.replaceAll(sign,"");

    String[] es = expression.split("\\.");

    if(sign.equals(">")){

        int length = vs.length < es.length ? vs.length : es.length;

        for (int i = 0; i < length; i++)
        {

            if (Integer.parseInt(vs[i]) < Integer.parseInt(es[i]))
            {
                return 0;
            }
        }

        return 1;
    }

    else if(sign.equals("<")){

        int length = vs.length < es.length ? vs.length : es.length;

        for (int i = 0; i < length; i++)
        {
            if (Integer.parseInt(vs[i]) > Integer.parseInt(es[i]))
            {
                return 0;
            }

        }

        return 1;
    }
    else if(sign.equals("<=")){

        int length = vs.length < es.length ? vs.length : es.length;

        for (int i = 0; i < length; i++)
        {
            if (Integer.parseInt(vs[i]) > Integer.parseInt(es[i]))
            {
                return 0;
            }

        }

        return 1;
    }

    else if(sign.equals(">=")){

        int length = vs.length < es.length ? vs.length : es.length;

        for (int i = 0; i < length; i++)
        {

            if (Integer.parseInt(vs[i]) < Integer.parseInt(es[i]))
            {
                return 0;
            }
        }

        return 1;
    }

    else if(sign.equals("=")){

        if(vs.length != es.length) return 0;

        int length = vs.length;
        for (int i = 0; i < length; i++)
        {
            if (Integer.parseInt(vs[i]) != Integer.parseInt(es[i]))
            {
                return 0;
            }
        }

        return 1;
    }

    return 1;
}
上一篇:开启 J2EE(四)—JSP及运行原理


下一篇:Docker学习:容器的优化 | Dockerfile优化 | 如何修改时区