Generic Interface with 2 Type Parameters

36 Views Asked by At

Please excuse me if this question seems very simple, I am pretty new to java:) I am having unchecked call to transformer(T) warning and the unchecked method invocation when trying to take a generic interface as a parameter for my method, would appreciate any help in revolving the warnings

I have a generic interface with 2 type parameters, and it has one abstract method that simply transforms from type T to type U

Public interface Transformer<T, U> { abstract U transform(T t); }

I have tried to create a class that implement the Transformer interface below

public class LastDigitsOfHashCode<T> implements Transformer<T, Integer>  {
    private final int i;

    public LastDigitsOfHashCode(int i) {
        this.i = i;
    }

    public Integer transform(T t) {
        int a = (int) Math.pow(10, this.i);
        return Math.abs(t.hashCode() % a);
    }
}

But now I have problems when I want to create another method in a different class called Box that takes in the Transformer interface as the parameter to transform the object from type T to Type U, there is unchecked call to transformer(T) warning and the unchecked method invocation which I have failed to resolve. The Box class is a generic class and has one parameter of type T (this.t)

    public <U> Box<U> map(Transformer<T, U> trans) {
        return new Box<U>(trans.transform(this.t));
    }

Please help, really have got no clue as to how to resolve the warning or how to implement the method properly, thank you!

0

There are 0 best solutions below