Skip to content

Commit

Permalink
Add keyExistsWithNullValue to KiwiMaps (#1096)
Browse files Browse the repository at this point in the history
This method checks whether a Map contains a key whose
associated value is null.

Closes #1093
  • Loading branch information
sleberknight authored Jan 7, 2024
1 parent bd4d5b9 commit 3f27ff4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/org/kiwiproject/collect/KiwiMaps.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -227,14 +228,31 @@ private static <K, V> void populate(Map<K, V> 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 <K> the type of the keys in the map
* @param <V> 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 <K, V> boolean keyExistsWithNullValue(Map<K, V> 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 <K> the type of the keys in the map
* @param <V> 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 <K, V> boolean keyExistsWithNonNullValue(Map<K, V> map, K key) {
return keyExists(map, key) && nonNull(map.get(key));
}

private static <K, V> boolean keyExists(Map<K, V> map, K key) {
return isNotNullOrEmpty(map) && map.containsKey(key);
}
}
65 changes: 65 additions & 0 deletions src/test/java/org/kiwiproject/collect/KiwiMapsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.<String, Object>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();
}
}
}
}

0 comments on commit 3f27ff4

Please sign in to comment.