Error passing Java errors enum and String varargs

I need some understanding of what I am facing the problem. I have a method with a signature addTerm(enum, String, String...), but when I use it, I get an error message saying that the addTerm method (Comparison, String, String ...) in the FTSearchBuilder type is not applicable for arguments (Comparison, String, String, String). I also get the same error if I pass only one line. Initially, I thought this was a problem with my vararg, but through testing, I found that if I changed the original argument from comparing with a string, my method would work fine. However, I had no problems passing the enumeration in other methods. Can someone explain what is happening and how to fix it?

Code below if useful.

Error

FTSearchBuilder sb = new FTSearchBuilder();
sb.addTerm(Comparison.EQ, "Form", "frmClaim", "frmClaimstub");

Works (with a slight modification to addTerm to use String instead of enum)

FTSearchBuilder sb = new FTSearchBuilder();
sb.addTerm("=", "Form", "frmClaim", "frmClaimstub");

Comparison

public enum Comparison {
    LT("<"), LTE("<="), EQ("="), GTE(">="), GT(">");

    private final String text;

    private Comparison(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }
}

addTerm () (in class FTSearchBuilder)

public class FTSearchBuilder {
private String query;

public FTSearchBuilder() {
    query = "";
}

public String getQuery() {
    return this.query;
}

public void addTerm(Comparison operator, String fieldName, String... values) {
    // Validate input
    String queryTerm = "";
    if (0 == values.length) {
        throw new ArrayIndexOutOfBoundsException("No comparison values given");
    }
    for (int x = 0; x < values.length; x++) {
        if (!StringUtil.isEmpty(values[x])) {
            if (queryTerm.equals("")) {
                queryTerm += "([" + fieldName + "]" + operator.getText() + enquote(values[x]);
            } else {
                queryTerm += " | [" + fieldName + "]" + operator.getText() + enquote(values[x]);
            }
            queryTerm += ")";
        }
    }

    if (!query.equals("")) {
        this.query += "&";
    }
    this.query += queryTerm;
}

public void addDateTerm(Comparison operator, String fieldName, String... values) {
    // Validate input
    String queryTerm = "";
    if (0 == values.length) {
        throw new ArrayIndexOutOfBoundsException("No comparison values given");
    }
    for (int x = 0; x < values.length; x++) {
        if (!StringUtil.isEmpty(values[x])) {
            if (queryTerm.equals("")) {
                queryTerm += "([" + fieldName + "]" + operator.getText() + values[x];
            } else {
                queryTerm += " | [" + fieldName + "]" + operator.getText() + values[x];
            }
            queryTerm += ")";
        }
    }

    if (!query.equals("")) {
        this.query += "&";
    }
    this.query += queryTerm;
}

private String enquote(String s) {
    return "\"" + s + "\"";
}
}
+4
source share
2 answers

Just for future troubleshooting - the problem is not in the code. I collected reusable code in plugins for use in other applications, and that included everything here. I still had a local copy of the package, and the plugin was added to the build path, so there were two identical copies of the package and classes.

FTSearchBuilder, , , , , , .

, .

+1

:

public class Test {
    enum Comparison {
        LT("<"), LTE("<="), EQ("="), GTE(">="), GT(">");

        private final String text;

        private Comparison(String text) {
            this.text = text;
        }

        public String getText() {
            return this.text;
        }
    }

    public static void main(String[] args) {
        addTerm(Comparison.EQ, "Form", "frmClaim", "frmClaimstub");
    }

    private static void addTerm(Comparison operator, String fieldName, String... values) {
        System.out.println(operator.toString());
        System.out.println(fieldName);

        if (values != null && values.length > 0){
            for (String v : values) {
                System.out.print(v + " ");
            }
        }

        System.out.println();
        System.out.println();
    }
}

:

EQ
Form
frmClaim frmClaimstub 

?

-1

Source: https://habr.com/ru/post/1533528/


All Articles