NullUtil { @SafeVarargs @SuppressWarnings("varargs") public static <K, V> Map<K, V> requireKeys(Map<K, V> map, K... requiredKeys) { requireNonNull(map); requireNonNullElements(requiredKeys); for (final K key : requiredKeys) { if (!map.containsKey(key)) { throw new NoSuchElementException("The Map does not contain the key " + key); } } return map; } private  NullUtil(); static T[] requireNonNullElements(T[] array); static T requireNonNullElements(T collection); static T[] requireNonNullElements(T[] array, String msg); static void requireNonNulls(Object o0); static void requireNonNulls(Object o0, Object o1); static void requireNonNulls(Object o0, Object o1, Object o2); static void requireNonNulls(Object o0, Object o1, Object o2, Object o3); static void requireNonNulls(Object o0, Object o1, Object o2, Object o3, Object o4); static void requireNonNulls(Object o0, Object o1, Object o2, Object o3, Object o4, Object o5); static void requireNonNulls(Object o0, Object o1, Object o2, Object o3, Object o4, Object o5, Object o6); static void requireNonNulls(Object o0, Object o1, Object o2, Object o3, Object o4, Object o5, Object o6, Object o7); @SafeVarargs // Iterating over an array is safe @SuppressWarnings("varargs") static Map<K, V> requireKeys(Map<K, V> map, K... requiredKeys); static Map<K, V> requireKeys(Map<K, V> map, K requiredKey); static Map<K, V> requireKeys(Map<K, V> map, K requiredKeyA, K requiredKeyB); static Map<K, V> requireKeys(Map<K, V> map, K requiredKeyA, K requiredKeyB, K requiredKeyC);  }
@Test void testRequireKeys_2args_1() { assertThrows(NullPointerException.class, () -> { NullUtil.requireKeys(null); }); }
@Test void testRequireKeys_2args_1_NotNull() { assertDoesNotThrow(() -> NullUtil.requireKeys(new HashMap<String, String>()) ); }
@Test void testRequireKeys_2args_2() { assertThrows(NullPointerException.class, () -> { final String[] arr = {"A"}; NullUtil.requireKeys((Map<String, String>) null, arr); }); }
@Test void testRequireKeys_2args_2_2() { assertThrows(NullPointerException.class, () -> { NullUtil.requireKeys(new HashMap<String, String>(), (String) null); }); }
@Test void testRequireKeys_2args_2_3_err() { assertThrows(NoSuchElementException.class, () -> { final Map<String, String> map = new HashMap<>(); map.put("A", "Tryggve"); map.put("B", "Sven"); final String[] arr = {"A", "B", "StangeKey"}; final Map<String, String> result = NullUtil.requireKeys(map, arr); }); }
@Test void testRequireKeys_2args_2_3() { final Map<String, String> map = new HashMap<>(); map.put("A", "Tryggve"); map.put("B", "Sven"); final String[] arr = {"A", "B"}; final Map<String, String> result = NullUtil.requireKeys(map, arr); assertEquals(map, result); }
@Test void testRequireKeys_3args() { assertThrows(NoSuchElementException.class, () -> { final Map<String, String> map = new HashMap<>(); map.put("A", "Tryggve"); map.put("B", "Sven"); final Map<String, String> result = NullUtil.requireKeys(map, "A", "Z"); }); }
@Test void testRequireKeys_3args_2() { final Map<String, String> map = new HashMap<>(); map.put("A", "Tryggve"); map.put("B", "Sven"); final Map<String, String> result = NullUtil.requireKeys(map, "A", "B"); assertEquals(map, result); }
@Test void testRequireKeys_4args() { assertThrows(NoSuchElementException.class, () -> { final Map<String, String> map = new HashMap<>(); map.put("A", "Tryggve"); map.put("B", "Sven"); map.put("C", "Glenn"); final Map<String, String> result = NullUtil.requireKeys(map, "A", "B", "Z"); }); }
@Test void testRequireKeys_4args_2() { final Map<String, String> map = new HashMap<>(); map.put("A", "Tryggve"); map.put("B", "Sven"); map.put("C", "Glenn"); final Map<String, String> result = NullUtil.requireKeys(map, "A", "B", "C"); assertEquals(map, result); }