I have been using a Collection
of number values and have implemented some related functionality (mapping x->y values, etc). So far I have made use of generics to allow any subclass of Number
in the Collection
.
In this particular class I keep running into the problem that there is no easy way to cast to the generic. The usual methods like Double.valueOf()
cannot be invoked because Number
does not provide such a method.
Is there a good way around this?
I have found this post and thought that would solve it, but I cannot pass the Class.class
parameter to it.
public class myList<T extends Number> extends Collection<T> {
@Override
public boolean add(T value){
// this is no problem
}
// What I want to do:
public boolean add(double value){
return this.add(T.valueOf(value));
}
// Using the example I found:
public void add(double value){
return this.add( (T) Ideone.parse(value, T.class) ); // no such option
}
}
Thanks in advance.