Skip to content

Commit

Permalink
Add utility method that determines whether an array of objects are al…
Browse files Browse the repository at this point in the history
…l non-null.
  • Loading branch information
jxblum committed Jul 14, 2021
1 parent 49d0cea commit 288b851
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@
@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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down

0 comments on commit 288b851

Please sign in to comment.