Java method call ambiguous

There are 2 methods:

public static TermsQueryBuilder termsQuery(String name, int... values) {
    return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, Object... values) {
    return new TermsQueryBuilder(name, values);
}

when i called

termsQuery("operatorType", 1);

he says that is ambiguous.

Please, help...

all methods are in Elasticsearch, for example:

public static TermsQueryBuilder termsQuery(String name, String... values) {
    return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, int... values) {
    return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, long... values) {
    return new TermsQueryBuilder(name, values);
}
...
public static TermsQueryBuilder termsQuery(String name, Object... values) {
    return new TermsQueryBuilder(name, values);
}

when I called termsQuery("operationType","1"), not ambiguous.

when I called out termsQuery("operationType",1), ambiguous.

when I called termsQuery("operationType",Arrays.asList(searchParam.getOperatorType()), not ambiguous.

puzzled .....

+4
source share
3 answers

Just rename one or both methods and the error will disappear.

You can not overload the method signatures of your desire, so that your call can be mapped to two specific methods and types intand Objectdo not apply to the relations of the superclass-subclass, so none of them is more specific than the other.

Consider

integerTermsQuery

and

objectTermsQuery

EDIT

, String... Object..., , String Object.

+2

, 1 Integer, Object. int.

, (Object...)

+2

, varargs <any primitive>..., , , , Object... , Object... . , .

We could name them using arrays:

termsQuery("operatorType", new int[]{1, 2, 3}).

TermsQueryBuilder termsQuery(String name, Object... values)and TermsQueryBuilder termsQuery(String name, String... values)are not ambiguous, because while Stringalso it is Object, it is a more special object than simple Object.

The following code works and should show exactly what is happening:

public class TestAmbiguous {

 public static TermsQueryBuilder termsQuery(String name, int... values) {
    return new TermsQueryBuilder(name, values);
 }
 public static TermsQueryBuilder termsQuery(String name, double... values) {
    return new TermsQueryBuilder(name, values);
 }
 public static TermsQueryBuilder termsQuery(String name, Object... values) {
    return new TermsQueryBuilder(name, values);
 }
 public static TermsQueryBuilder termsQuery(String name, String... values) {
    return new TermsQueryBuilder(name, values);
 }

 public static void main(String[] args) {
  TermsQueryBuilder builder;
  builder = termsQuery("operatorType", 1, 1.0, 1.0f, "test", new Integer(1), new Double(1), new Float(1), new String("test"));
  builder = termsQuery("operatorType", "test", new String("test"));
  builder = termsQuery("operatorType", new int[]{1, 2, 3});
  builder = termsQuery("operatorType", new double[]{1, 2, 3});
  builder = termsQuery("operatorType", new float[]{1, 2, 3});
  builder = termsQuery("operatorType", (Object[]) new Float[]{1f, 2f, 3f});
 }
}

class TermsQueryBuilder {
 TermsQueryBuilder(String name, int... values) {
  System.out.println("_____________________________________");
  System.out.println("primitive int");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i]);
  }
 }
 TermsQueryBuilder(String name, double... values) {
  System.out.println("_____________________________________");
  System.out.println("primitive double");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i]);
  }
 }
 TermsQueryBuilder(String name, Object... values) {
  System.out.println("_____________________________________");
  System.out.println("Any objects");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i].getClass());
   System.out.println(values[i]);
  }
 }
 TermsQueryBuilder(String name, String... values) {
  System.out.println("_____________________________________");
  System.out.println("String objects");
  for (int i = 0 ; i < values.length; i++) {
   System.out.println(values[i].getClass());
   System.out.println(values[i]);
  }
 }
}
+1
source

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


All Articles