I have a method to which Setobjects are assigned . The method that he delegates requires that it Setdoes not contain any null elements. I would like to check the precondition that it Setdoes not contain null elements before, in the method before delegation. The obvious code does this:
public void scan(Set<PlugIn> plugIns) {
if (plugIns == null) {
throw new NullPointerException("plugIns");
} else if (plugIns.contains(null)) {
throw new NullPointerException("plugIns null element");
}
}
But this is not true, because it Set.contains()can cause NullPointerException, if the implementation itself Setdoes not allow null elements, Dexterous, then ignoring NullPointerExceptionin this case will work but will be inelegant . Is there a neat way to test this prerequisite?
Is there a design flaw in the interface Set? If an implementation can Setnever contain zero, why not require Set.contains(null)always returning false? Or has a predicate isNullElementPermitted()?
source
share