Skip to content

Commit 288b851

Browse files
committed
Add utility method that determines whether an array of objects are all non-null.
1 parent 49d0cea commit 288b851

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/util/SpringUtils.java

+20
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@
6868
@SuppressWarnings("unused")
6969
public abstract class SpringUtils {
7070

71+
/**
72+
* Determines whether all the {@link Object} values in the array are {@literal non-null}
73+
*
74+
* @param values array of {@link Object Objects} to evaluate.
75+
* @return a boolean value indicating whether all of the {@link Object} values
76+
* in the array are {@literal non-null}.
77+
*/
78+
public static boolean areNotNull(Object... values) {
79+
80+
if (values != null) {
81+
for (Object value : values) {
82+
if (value == null) {
83+
return false;
84+
}
85+
}
86+
}
87+
88+
return true;
89+
}
90+
7191
/**
7292
* Determines whether a given bean registered in the {@link BeanFactory Spring container} matches by
7393
* both {@link String name} and {@link Class type}.

spring-data-geode/src/test/java/org/springframework/data/gemfire/util/SpringUtilsUnitTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ public class SpringUtilsUnitTests {
7777
@Mock
7878
private BeanDefinition mockBeanDefinition;
7979

80+
@Test
81+
public void areNotNullIsNullSafe() {
82+
assertThat(SpringUtils.areNotNull((Object[]) null)).isTrue();
83+
}
84+
85+
@Test
86+
public void areNotNullWithAllNullValuesReturnsFalse() {
87+
assertThat(SpringUtils.areNotNull(null, null, null)).isFalse();
88+
}
89+
90+
@Test
91+
public void areNotNullWithNoNullValuesReturnsTrue() {
92+
assertThat(SpringUtils.areNotNull(1, 2, 3)).isTrue();
93+
}
94+
95+
@Test
96+
public void areNotNullWithOneNullValueIsFalse() {
97+
98+
assertThat(SpringUtils.areNotNull(null, 2, 3)).isFalse();
99+
assertThat(SpringUtils.areNotNull(1, null, 3)).isFalse();
100+
assertThat(SpringUtils.areNotNull(1, 2, null)).isFalse();
101+
}
102+
80103
@Test
81104
public void isMatchingBeanReturnsTrue() {
82105

0 commit comments

Comments
 (0)