有人知道如何使用泛型编写下面的代码并避免编译器警告吗?(@SuppressWarnings("unchecked") 被认为是作弊)。
而且,也许,通过泛型检查 "left" 的类型是否与 "right" 的类型相同?
public void assertLessOrEqual(Comparable left, Comparable right) {
if (left == null || right == null || (left.compareTo(right) > 0)) {
String msg = "["+left+"] is not less than ["+right+"]";
throw new RuntimeException("assertLessOrEqual: " + msg);
}
}