+1 on disallowing null keys and values
The question of NPE vs IllegalArgumentException isn’t black and white to me. On this exact topic in the past though I’ve delegated to Josh Bloch’s guidance in Effective Java (item 60 in 2nd ed)
You can find the text of that item in this thread on the exact topic:
I can't remember for sure if we made any decision on the permissibility of nulls in key/value of Cache entry, though I seem to remember that we said neither should be allowed. That may be just because it is my preference.
Here is what Map says:
http://download.oracle.com/javase/6/docs/api/java/util/Map.html#put%28K,%20V%29
Specific questions, given a Cache<Integer, String>
1) Are null keys allowed? My preference is NO.
if no,
1a) cache.put(null, "v1") will throw an IllegalArgumentException (this is my preference)
1b) cache.put(null, "v1") will throw an NullPointerException (this is what Map proposes)
1c) cache.put(null, "v1") silently does nothing
2) Are null values allowed? my preference is NO.
if no,
2a) cache.put(1, null) will throw an IllegalArgumentException (this is my preference)
2b) cache.put(1, null) will throw an NullPointerException (this is what Map proposes)
2c) cache.put(1, null) silently does nothing
Whatever the answers to these will propagate to other methods including putAll, remove, get
--Yannis
--
I will be participating (again) in the ALC bicycle ride, from San Francisco to Los Angeles, raising money to fight AIDS. Please support me: http://www.tofighthiv.org/goto/yannis