字母+数字+版本号混合比较算法,;号为或,号为与

自己记录一下,笔记:

private static int compareStrings(String parm,String sword,String sign){

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

        String[] ps = parm.split(" ");
        String[] ss = sword.split(" ");

        if(sign.equals("=")&&sword.indexOf("x")<0&&sword.indexOf("*")<0&&sword.indexOf("n")<0){

            if(ps.length!=ss.length||!ps.equals(ss)) return 0;
        }

        if(ps.length==ss.length){

            for (int i=0;i 1) {

                        return compareVersion(ps[i],ss[i],sign);

                    } else {

                        char[] cp = ps[i].toCharArray();
                        char[] ct = ss[i].toCharArray();

                        for (int j = 0; j < cp.length; j++) {

                            if (Character.isDigit(cp[j]) && Character.isDigit(ct[j])) {
                                StringBuffer nump = new StringBuffer();
                                StringBuffer numt = new StringBuffer();
                                for (int jj = j; jj < cp.length; jj++) {
                                    if (Character.isDigit(cp[jj])) {

                                        nump.append(cp[jj]);
                                    }

                                }

                                for (int k = j; k < ct.length; k++) {
                                    if (Character.isDigit(ct[k])) {

                                        numt.append(ct[k]);
                                    }

                                }
                                if (sign.equals(">=")) {
                                    return Integer.valueOf(nump.toString()).intValue() >= Integer.valueOf(numt.toString()).intValue() ? 1 : 0;
                                } else if (sign.equals("<=")) {
                                    return Integer.valueOf(nump.toString()).intValue() <= Integer.valueOf(numt.toString()).intValue() ? 1 : 0;
                                } else if (sign.equals(">")) {
                                    return Integer.valueOf(nump.toString()).intValue() > Integer.valueOf(numt.toString()).intValue() ? 1 : 0;
                                } else if (sign.equals("<")) {
                                    return Integer.valueOf(nump.toString()).intValue() < Integer.valueOf(numt.toString()).intValue() ? 1 : 0;
                                } else {

                                    return Integer.valueOf(nump.toString()).intValue() == Integer.valueOf(numt.toString()).intValue() ? 1 : 0;
                                }
                            }

                        }
                    }
                }else{

                    if(isNumeric(ps[i])&&isNumeric(ss[i])){
                        if (sign.equals(">=")) {
                            return Integer.valueOf(ps[i].toString()).intValue() >= Integer.valueOf(ss[i].toString()).intValue() ? 1 : 0;
                        } else if (sign.equals("<=")) {
                            return Integer.valueOf(ps[i].toString()).intValue() <= Integer.valueOf(ss[i].toString()).intValue() ? 1 : 0;
                        } else if (sign.equals(">")) {
                            return Integer.valueOf(ps[i].toString()).intValue() > Integer.valueOf(ss[i].toString()).intValue() ? 1 : 0;
                        } else if (sign.equals("<")) {
                            return Integer.valueOf(ps[i].toString()).intValue() < Integer.valueOf(ss[i].toString()).intValue() ? 1 : 0;
                        } else {

                            return Integer.valueOf(ps[i].toString()).intValue() == Integer.valueOf(ss[i].toString()).intValue() ? 1 : 0;
                        }

                    }

                }
            }
        }
        return 1;

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

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

            int a = 0;
            for (int i=0;i=")>-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 {
                            c = c*compareVersion(parm,sub2s[ii],"=");
                        }
                    }
                    a=a+c;
                }
                else if(sub1s[i].contains(">=")){

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

                }

                else if(sub1s[i].contains("<=")){

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

                }

                else if(sub1s[i].contains("=<")){

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

                }
                else if(sub1s[i].contains("<")){

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

                }
                else if(sub1s[i].contains(">")){

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

                }
                else {

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

                }


            }
            return a>0;

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


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


            int c = 1;

            for (int ii=0;ii=")>-1){
                    c = c*compareVersion(parm,subs[ii],">=");
                }else if(subs[ii].indexOf("<=")>-1){
                    c = c*compareVersion(parm,subs[ii],"<=");
                }else if(subs[ii].indexOf(">")>-1){
                    c = c*compareVersion(parm,subs[ii],">");
                }else if(subs[ii].indexOf("<")>-1){
                    c = c*compareVersion(parm,subs[ii],"<");
                }else if (subs[ii].indexOf("=")>-1){
                    c = c*compareVersion(parm,subs[ii],"=");
                }
            }

            return c==1;

        }else{
            int c=1;
            if(expression.indexOf(">=")>-1){
                return c*compareVersion(parm,expression,">=")==1;
            }else if(expression.indexOf("<=")>-1){
                return  c*compareVersion(parm,expression,"<=")==1;
            }else if(expression.indexOf(">")>-1){
                return  c*compareVersion(parm,expression,">")==1;
            }else if(expression.indexOf("<")>-1){
                return  c*compareVersion(parm,expression,"<")==1;
            }else {
                return  c*compareVersion(parm,expression,"=")==1;
            }
        }

    }
    private static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
    private static int compareSN(String parm,String express,String sign){


        String[] parms = parm.split("-");
        String[] exps = express.split("-");

        if(parms.length!=exps.length) return 0;
        int f = 1;
        for(int p=0;p=")) {
                        f= f*(Integer.valueOf(nump.toString()).intValue() >= Integer.valueOf(numt.toString()).intValue() ? 1 : 0);
                    } else if (sign.equals("<=")) {
                        f= f*(Integer.valueOf(nump.toString()).intValue() <= Integer.valueOf(numt.toString()).intValue() ? 1 : 0);
                    } else if (sign.equals(">")) {
                        f= f*(Integer.valueOf(nump.toString()).intValue() > Integer.valueOf(numt.toString()).intValue() ? 1 : 0);
                    } else if (sign.equals("<")) {
                        f= f*(Integer.valueOf(nump.toString()).intValue() < Integer.valueOf(numt.toString()).intValue() ? 1 : 0);
                    } else {

                        f= f*(Integer.valueOf(nump.toString()).intValue() == Integer.valueOf(numt.toString()).intValue() ? 1 : 0);
                    }
                }else{
                    if(cp[j]!=ct[j]){
                        f = f* 0;
                    }

                }

            }

        }
        return f;
    }
    private static int compareVersion(String parm,String expression,String sign) {

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

        if (vs.length > 1&&expression.split(" ").length<=1) {

            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 (es[i].toLowerCase().equals("x") || es[i].toLowerCase().equals("*") || (vs[i] + "x").equals(es[i].toLowerCase())) continue;
                    if(isNumeric(vs[i])&&isNumeric(es[i])){
                    if (Integer.parseInt(vs[i]) > Integer.parseInt(es[i])) {
                        return 0;
                    }
                    }else{

                        return compareSN(vs[i],es[i],sign);
                    }

                }

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

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

                for (int i = 0; i < length; i++) {
                    if (es[i].toLowerCase().equals("x") || es[i].toLowerCase().equals("*") || (vs[i] + "x").equals(es[i].toLowerCase())) continue;

                    if(isNumeric(vs[i])&&isNumeric(es[i])){

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

                       return compareSN(vs[i],es[i],sign);
                    }
                }

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

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

                for (int i = 0; i < length; i++) {
                    if (es[i].toLowerCase().equals("x") || es[i].toLowerCase().equals("*") || (vs[i] + "x").equals(es[i].toLowerCase())) continue;
                    if(isNumeric(vs[i])&&isNumeric(es[i])){
                    if (Integer.parseInt(vs[i]) < Integer.parseInt(es[i])) {
                        return 1;
                    }
                    }else{

                        return compareSN(vs[i],es[i],sign);
                    }
                }

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

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

                for (int i = 0; i < length; i++) {
                    if (es[i].toLowerCase().equals("x") || es[i].toLowerCase().equals("*") || (vs[i] + "x").equals(es[i].toLowerCase())) continue;
                    if(isNumeric(vs[i])&&isNumeric(es[i])){
                    if (Integer.parseInt(vs[i]) >= Integer.parseInt(es[i])) {
                        return 0;
                    }
                    }else{

                        return compareSN(vs[i],es[i],sign);
                    }

                }

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

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

                for (int i = 0; i < length; i++) {
                    if (es[i].toLowerCase().equals("x") || es[i].toLowerCase().equals("*") || (vs[i] + "x").equals(es[i].toLowerCase())) continue;
                    if(isNumeric(vs[i])&&isNumeric(es[i])){
                    if (Integer.parseInt(vs[i]) <= Integer.parseInt(es[i])) {
                        return 0;
                    }
                    }else{

                        return compareSN(vs[i],es[i],sign);
                    }
                }

                return 1;
            } else {


                int length = vs.length < es.length ? vs.length : es.length;
                for (int i = 0; i < length; i++) {
                    if (es[i].toLowerCase().equals("x") || es[i].toLowerCase().equals("*") || (vs[i] + "x").equals(es[i].toLowerCase())) continue;
                    if(isNumeric(vs[i])&&isNumeric(es[i])){
                    if (Integer.parseInt(vs[i]) != Integer.parseInt(es[i])) {
                        return 0;
                    }
                    }else{

                        return compareSN(vs[i],es[i],sign);
                    }
                }

                return 1;
            }

        }
        else{

            return compareStrings(parm,expression,sign);
        }
    }
public static void main(String[] args) {

        String a = "3.2.4";
//                String b = "=<5.5.28||>=3.2.5&&<=3.23.9||<=7.45";
//                String b = ">=3.2.5&&<=3.23.9";
//                String b = "=3.2.x";
        String b = "=3.2x";
        String parm = "linux 面板 6.5";
        String parm1 = "server 2008 r2 for x64-based systems service pack 1";
        String parm2 = "intel® wireless-ac 9560";
        String parm7 = "8.1.1.6 for windows";
        String parm9 = "10.1.0-m2";
        String parm10 = "6.5.4.4-44v-21-795";
//        String sword = "=windows 10;windows 2008 r0;windows 2008 r2;windows 2012;windows 2016;windows 2019;windows 7;windows 8;windows rt";
//        String sword = ">=2016 cu15,<=2016 cu17;>=2019 cu5,<=2019 cu6";
        String sword = "linux 面板 6.*;windows 面板 6.8";
        String sword1="server 2008 r2 for x64-based systems service pack 1";
        String sword2=">intel® wireless-ac 9561";
        String sword3=">8.1.0.7 for windows";
//        String sword3 = "intel® wi-fi 64 ax201;intel® wi-fi 32 ax200;intel® wireless-ac 9560;intel® wireless-ac 9462;intel® wireless-ac 9461;intel® wireless-ac 9260;intel® dual band wireless-ac 8265;intel® dual band wireless-ac 8260;intel® dual band wireless-ac 3168;intel® wireless 7265 (rev d) family;intel® dual band wireless-ac 3165";
        String sword4 = ">=2016 cu16,<=2016 cu17;>=2019 cu5,<=2019 cu6";
        String sword5 = "=jackson-databind 2.*;<2.9.10.6";
        String sword6 = "=linux 面板 7.4.2;windows 面板 6.8";
        String sword7 = "<=8.11.12.7 for windows";
        String sword8 = "<=6.0.5.3-93o;<=6.5.4.4-44v-21-794;<=6.5.4.7-79n;<=6.5.1.11-4n;<=7.0.0.0-1";
        String sword9 = ">=10.0.0-m1";
        String sword10 = "<=6.5.4.4-44v-21-794";
//        System.out.println(isExist(sword,parm));
        System.out.println(isExist(sword1,parm1));
//        System.out.println(isExist(sword2,parm2));
        System.out.println(isExist(sword7,parm7));
        System.out.println(isExist(sword9,parm9));
        System.out.println(isExist(sword10,parm10));

//        System.out.println(compareStringsNew(parm7,sword7,"<="));
//        System.out.println(compareStringsNew(parm10,sword10,"<="));
//        System.out.println(compareStringsNew(parm9,sword9,">="));

    }

你可能感兴趣的:(demo,java,字符串,算法)