I've to implement a generic class "myClass" that accepts T as parameter. This T has to be a subclass of Number.
myClass has an "add" method in which I have to add only positive numbers.
public class myClass<T extends Comparable<T>, Number>{
private List<T> listPositive = new LinkedList<>();
public myClass(){}
public void add(T elem){
if(comp.compare(elem, ???) > 0){ //I would like to compare with 0 but I know I can't.
listPositive.add(elem);
}
}
Comparator<T> comp = new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
};
}
My question is: How can I check if a generic number is positive or negative?
EDIT: Thanks to the answer similar to mine but this din't help me. In fact that question is about to compare two numbers with the same type. I've to check if a certain generic number is negative or positive.