DATAGEODE-302 - Add 'nullSafeIsEmpty(:Stream) utility method to StreamUtils.

This commit is contained in:
John Blum
2020-04-08 14:00:37 -07:00
parent 70c345ee13
commit 6045f86da1
2 changed files with 32 additions and 0 deletions

View File

@@ -61,6 +61,18 @@ public abstract class StreamUtils {
return nullSafeStream(stream).count();
}
/**
* Null-safe utility method used to determine whether the given {@link Stream} is empty.
*
* @param stream {@link Stream} to evalute.
* @return a boolean value indicating whether the given {@link Stream} is empty.
* @see java.util.stream.Stream
* @see #nullSafeCount(Stream)
*/
public static boolean nullSafeIsEmpty(Stream<?> stream) {
return nullSafeCount(stream) == 0L;
}
/**
* Utility method used to guard against {@literal null} {@link Stream Streams}.
*

View File

@@ -106,6 +106,26 @@ public class StreamUtilsUnitTests {
assertThat(StreamUtils.nullSafeCount(Stream.of(1, 2))).isEqualTo(2);
}
@Test
public void isEmptyWithNullStream() {
assertThat(StreamUtils.nullSafeIsEmpty(null)).isTrue();
}
@Test
public void isEmptyWithEmptyStream() {
assertThat(StreamUtils.nullSafeIsEmpty(Stream.empty())).isTrue();
}
@Test
public void isEmptyWithOneElementStream() {
assertThat(StreamUtils.nullSafeIsEmpty(Stream.of(1))).isFalse();
}
@Test
public void isEmptyWithTwoElementStream() {
assertThat(StreamUtils.nullSafeIsEmpty(Stream.of(1, 2))).isFalse();
}
@Test
public void nullSafeStreamWithStream() {