ArgCheck { static public <T> Set<T> setWithoutNull(Set<T> set) throws NullPointerException { return setWithoutNull(set, "Set cannot contains null value"); } private  ArgCheck(); static T requireNonNull(T obj); static T requireNonNull(T obj, String errMsg); static long requirePostive(long l); static long requirePostive(long l, String msg); static long requireStrictlyPostive(long l); static long requireStrictlyPostive(long l, String msg); static int requirePostive(int i); static int requirePostive(int i, String msg); static int requireStrictlyPostive(int i); static int requireStrictlyPostive(int i, String msg); static Set<T> setWithoutNull(Set<T> set); static Set<T> setWithoutNull(Set<T> set, String msg);  }
@Test public void testSetWithoutNullValue() { HashSet<String> hashset; hashset = new HashSet<String>(); ArgCheck.setWithoutNull(hashset); hashset.add("toto"); ArgCheck.setWithoutNull(hashset); hashset.add(null); try { ArgCheck.setWithoutNull(hashset); Assert.fail("Should have thrown an NPE"); } catch (NullPointerException e) { } String msg = "msg"; hashset = new HashSet<String>(); ArgCheck.setWithoutNull(hashset, msg); hashset.add("toto"); ArgCheck.setWithoutNull(hashset, msg); hashset.add(null); try { ArgCheck.setWithoutNull(hashset, msg); Assert.fail("Should have thrown an NPE"); } catch (NullPointerException e) { Assert.assertEquals(msg, e.getMessage()); } }