Add utility method that determines whether an array of objects are all non-null.

This commit is contained in:
John Blum
2021-07-14 14:36:08 -07:00
parent c0b929c001
commit 0723ddd285
2 changed files with 43 additions and 0 deletions

View File

@@ -68,6 +68,26 @@ import org.springframework.util.StringUtils;
@SuppressWarnings("unused")
public abstract class SpringUtils {
/**
* Determines whether all the {@link Object} values in the array are {@literal non-null}
*
* @param values array of {@link Object Objects} to evaluate.
* @return a boolean value indicating whether all of the {@link Object} values
* in the array are {@literal non-null}.
*/
public static boolean areNotNull(Object... values) {
if (values != null) {
for (Object value : values) {
if (value == null) {
return false;
}
}
}
return true;
}
/**
* Determines whether a given bean registered in the {@link BeanFactory Spring container} matches by
* both {@link String name} and {@link Class type}.

View File

@@ -77,6 +77,29 @@ public class SpringUtilsUnitTests {
@Mock
private BeanDefinition mockBeanDefinition;
@Test
public void areNotNullIsNullSafe() {
assertThat(SpringUtils.areNotNull((Object[]) null)).isTrue();
}
@Test
public void areNotNullWithAllNullValuesReturnsFalse() {
assertThat(SpringUtils.areNotNull(null, null, null)).isFalse();
}
@Test
public void areNotNullWithNoNullValuesReturnsTrue() {
assertThat(SpringUtils.areNotNull(1, 2, 3)).isTrue();
}
@Test
public void areNotNullWithOneNullValueIsFalse() {
assertThat(SpringUtils.areNotNull(null, 2, 3)).isFalse();
assertThat(SpringUtils.areNotNull(1, null, 3)).isFalse();
assertThat(SpringUtils.areNotNull(1, 2, null)).isFalse();
}
@Test
public void isMatchingBeanReturnsTrue() {