I'm trying to do something with generics and I can't seem to figure it out. So I have this interface.
public interface Transformation<A,B> {
public B transform(A a);
}
And I have a class that implements it.
public class AveragingTransformer implements Transformation<List<Long>,Double> {
public Double transform(List<Long> a) {
return a.get(0).doubleValue()/a.get(1);
}
}
So in my main method I have this...
public static void main(String[] args){
....
Transformation<List<?>,Double> transformation = new AveraginTransformer();
....
}
But this doesn't work for some reason, can anyone help me understand why?
EDIT
So the issue is if I want to pass an array to my transformation I can't seem to get the compiler to accept it.
So say I have...
public class foo<T>{
T val;
Transformation<? extends List<?>,T> transformation; //This gets initialized by constructor, where I will pass in the averaging transformer in this case.
public void setVal(List<?> list){
val = transformation.transform(list);
}
}
If I try to do this it gives me this error
But I want to keep the left part of my transformation generic because different foos will calculate their values in different ways with potentially different data. i.e. Some foos might calculate their val from a list of Strings but if I provide the right transformer to each, then it should work.
, Double>` is not a `Transformation
– Savior Apr 05 '16 at 15:22,Double>`. It can be a `Transformation extends List>,Double>`