Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java method call is 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);

it says that is ambiguous.

Please Help ...

all the 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 termsQuery("operationType",1), ambiguous.

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

puzzled.....

like image 202
Vic Avatar asked Aug 06 '17 04:08

Vic


1 Answers

All constructors using varargs <any primitive>... are not callable directly using comma separated list of primitives if Object... exists because for all of those Object... is ambiguous. Compiler error message clearly states that.

We could call 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 String is also an Object it is a more special object than just Object.

The following code works and should show what happens exactly:

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]);
  }
 }
}
like image 139
Axel Richter Avatar answered Sep 28 '22 00:09

Axel Richter