Consider Bloch's min
example:
public static <???> T min (List<???> list) {
Iterator<T> itr = list.iterator();
if (itr.hasNext() == false) {
throw new IllegalArgumentException("min: Empty list");
}
T result = itr.next();
while (itr.hasNext()) {
T comp = itr.next();
if (comp.compareTo(result) < 0) { // throws NPE, CCE as needed
result = comp;
}
}
return result;
}
}
- Generify with a recursive type declaration
- Write a contract - be sure to consider all of the edge cases
- Bloch's code has a bug; write a test that finds it. Fix the code
- Does a bounded wildcard make this method more general? Can you think of
some other way to generalize this method?