diff --git a/src/main/java/org/kiwiproject/collect/KiwiMaps.java b/src/main/java/org/kiwiproject/collect/KiwiMaps.java index ef10025e..cad46f3d 100644 --- a/src/main/java/org/kiwiproject/collect/KiwiMaps.java +++ b/src/main/java/org/kiwiproject/collect/KiwiMaps.java @@ -1,6 +1,7 @@ package org.kiwiproject.collect; import static java.util.Objects.isNull; +import static java.util.Objects.nonNull; import static org.kiwiproject.base.KiwiPreconditions.checkEvenItemCount; import lombok.experimental.UtilityClass; @@ -227,14 +228,31 @@ private static void populate(Map map, Object... items) { * {@code key}, and (3) the value associated with the given key is {@code null}. * * @param map the map - * @param key the key check + * @param key the key to check * @param the type of the keys in the map * @param the type of the values in the map * @return {@code true} if and only if (1) {@code map} is not null or empty, (2) {@code map} contains the given * {@code key}, and (3) the value associated with the given key is {@code null} */ public static boolean keyExistsWithNullValue(Map map, K key) { - return isNotNullOrEmpty(map) && map.containsKey(key) && isNull(map.get(key)); + return keyExists(map, key) && isNull(map.get(key)); } + /** + * Checks whether the given map contains a key whose value is not null. + * + * @param map the map + * @param key the key to check + * @param the type of the keys in the map + * @param the type of the values in the map + * @return {@code true} if and only if (1) {@code map} is not null or empty, (2) {@code map} contains the given + * {@code key}, and (3) the value associated with the given key is not {@code null} + */ + public static boolean keyExistsWithNonNullValue(Map map, K key) { + return keyExists(map, key) && nonNull(map.get(key)); + } + + private static boolean keyExists(Map map, K key) { + return isNotNullOrEmpty(map) && map.containsKey(key); + } } diff --git a/src/test/java/org/kiwiproject/collect/KiwiMapsTest.java b/src/test/java/org/kiwiproject/collect/KiwiMapsTest.java index e9f89859..86caedfa 100644 --- a/src/test/java/org/kiwiproject/collect/KiwiMapsTest.java +++ b/src/test/java/org/kiwiproject/collect/KiwiMapsTest.java @@ -354,4 +354,69 @@ void shouldCreateUnmodifiableConcurrentHashMap() { assertThatThrownBy(() -> unmodifiableConcurrentHashMap.put("nine", 9)) .isInstanceOf(UnsupportedOperationException.class); } + + @Nested + class KeyExistsWithNonNullValue { + + @Nested + class ShouldReturnTrue { + + @Test + void whenKeyExistsAndHasANonNullValue() { + var key = "aKey"; + assertThat(KiwiMaps.keyExistsWithNonNullValue(Map.of(key, "aValue"), key)).isTrue(); + } + + @Test + void whenObjectKeyExistsAndHasANonNullValue() { + var key = new Object(); + assertThat(KiwiMaps.keyExistsWithNonNullValue(Map.of(key, "aValue"), key)).isTrue(); + } + } + + @Nested + class ShouldReturnFalse { + + @Test + void whenMapIsNull() { + assertThat(KiwiMaps.keyExistsWithNonNullValue(null, "aKey")).isFalse(); + } + + @Test + void whenMapIsEmpty() { + assertThat(KiwiMaps.keyExistsWithNonNullValue(Map.of(), "aKey")).isFalse(); + } + + @Test + void whenKeyIsNull() { + assertThat(KiwiMaps.keyExistsWithNonNullValue(Map.of(), null)).isFalse(); + } + + @ParameterizedTest + @BlankStringSource + void whenKeyIsBlank(String value) { + assertThat(KiwiMaps.keyExistsWithNonNullValue(Map.of(), value)).isFalse(); + } + + @Test + void whenKeyDoesNotExist() { + var map = Map.of("aKey", "aValue"); + assertThat(KiwiMaps.keyExistsWithNonNullValue(map, "anotherKey")).isFalse(); + } + + @Test + void whenKeyExistsAndHasNullValue() { + var key = "aKey"; + var map = KiwiMaps.newUnmodifiableHashMap(key, null); + assertThat(KiwiMaps.keyExistsWithNonNullValue(map, key)).isFalse(); + } + + @Test + void whenObjectKeyExistsAndHasNullValue() { + var key = new Object(); + var map = KiwiMaps.newUnmodifiableHashMap(key, null); + assertThat(KiwiMaps.keyExistsWithNonNullValue(map, key)).isFalse(); + } + } + } }