From 6045f86da1a16597473ac0cf7acd82282e4cb60e Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 8 Apr 2020 14:00:37 -0700 Subject: [PATCH] DATAGEODE-302 - Add 'nullSafeIsEmpty(:Stream) utility method to StreamUtils. --- .../data/gemfire/util/StreamUtils.java | 12 +++++++++++ .../gemfire/util/StreamUtilsUnitTests.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/StreamUtils.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/StreamUtils.java index b87dca0c..f4f37bbf 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/StreamUtils.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/StreamUtils.java @@ -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}. * diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/StreamUtilsUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/StreamUtilsUnitTests.java index fb3e863c..710de55d 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/StreamUtilsUnitTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/StreamUtilsUnitTests.java @@ -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() {