新增 ArrayTools#isAllElementsNotNull

dev
ZhouXY108 2024-10-11 17:14:49 +08:00
parent de0a732616
commit 88f96c7116
2 changed files with 48 additions and 3 deletions

View File

@ -21,11 +21,9 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.annotations.Beta;
@Beta
public class ArrayTools {
// isNullOrEmpty
@ -163,6 +161,33 @@ public class ArrayTools {
return arr != null && arr.length > 0;
}
// isAllElementsNotNull
/**
*
* <ol>
* <li><b> {@code null}</b> {@code null}
* <li><b> {@code null} 0</b> {@code null} {@code null}
* <li><b> {@code null}</b> {@code false}
* <li><b> {@code null}</b> {@code true}
* </ol>
*
* @param <T>
* @param arr {@code null}
* @return {@code null} {@code true} {@code false}
*
* @throws IllegalArgumentException
*/
public static <T> boolean isAllElementsNotNull(@Nonnull final T[] arr) {
AssertTools.checkParameter(arr != null, "The array cannot be null.");
for (T element : arr) {
if (element == null) {
return false;
}
}
return true;
}
// concat
/**

View File

@ -0,0 +1,20 @@
package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
@SuppressWarnings("all")
public class ArrayToolsTests {
@Test
void testIsAllNotNull() {
assertTrue(ArrayTools.isAllElementsNotNull(new Object[] { 1L, 2, 3.0, "Java" }));
assertFalse(ArrayTools.isAllElementsNotNull(new Object[] { 1L, 2, 3.0, "Java", null }));
assertFalse(ArrayTools.isAllElementsNotNull(new Object[] { null, 1L, 2, 3.0, "Java" }));
assertTrue(ArrayTools.isAllElementsNotNull(new Object[] {}));
assertThrows(IllegalArgumentException.class,
() -> ArrayTools.isAllElementsNotNull(null));
}
}