Trace
Trace

Reputation: 18859

Pass Integer.class as parameter

I have the following class:

package query;

public class Predicate<T> {

    private String operand1; 
    private String operator; 
    private T operand2; 
    private Class<T> classType; 

    public Predicate(String operand1, T operand2, Class<T> classType){
        this(operand1, "=", operand2, classType); 
    }

    public Predicate(String operand1, String operator, T operand2, Class<T> classType){
        this.operand1 = operand1; 
        this.operator = operator; 
        this.operand2 = operand2; 
        this.classType = classType; 
    }

    public String getOperand1() {
        return operand1;
    }

    public String getOperator() {
        return operator;
    }

    public T getOperand2() {
        return operand2;
    }

    public Class<T> getClassType() {
        return classType;
    }

    @Override
    public String toString() {
        return operand1 + " " + operator + " ?"; 
    }
}

The following line compiles fine:

Predicate<String> p1 = new Predicate<>("given_name", "=", "Kim", String.class); 

However, the following does not:

Predicate<Integer> p1 = new Predicate<>("id", "=", "1", Integer.class); 

The compiler mentions: Cannot infer type arguments for Predicate<>

What is wrong / how can solve this?

Upvotes: 1

Views: 498

Answers (1)

"1" is a String and incompatible with Integer.class

new Predicate<>("id", "=", 1, Integer.class)

should be ok

or

new Predicate<>("id", "=", "1", String.class)

Upvotes: 7

Related Questions