diff --git a/core-java-modules/core-java-lang-oop-others/pom.xml b/core-java-modules/core-java-lang-oop-others/pom.xml index d9d147f34a..dea92067f2 100644 --- a/core-java-modules/core-java-lang-oop-others/pom.xml +++ b/core-java-modules/core-java-lang-oop-others/pom.xml @@ -13,4 +13,12 @@ 0.0.1-SNAPSHOT + + + org.apache.commons + commons-lang3 + 3.12.0 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/nullchecking/Car.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/nullchecking/Car.java new file mode 100644 index 0000000000..975006d8df --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/nullchecking/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.nullchecking; + +import java.util.Objects; +import java.util.stream.Stream; + +public class Car { + + Integer power; + + Integer year; + + public boolean allNull() { + + if (power != null) { + return false; + } + + if (year != null) { + return false; + } + + return true; + } + + public boolean allNullV2() { + + return Stream.of(power, year) + .allMatch(Objects::isNull); + } +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/nullchecking/NullChecker.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/nullchecking/NullChecker.java new file mode 100644 index 0000000000..5630577cdf --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/nullchecking/NullChecker.java @@ -0,0 +1,25 @@ +package com.baeldung.nullchecking; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Objects; + +public class NullChecker { + + public static boolean allNull(Object target) { + + return Arrays.stream(target.getClass() + .getDeclaredFields()) + .peek(f -> f.setAccessible(true)) + .map(f -> getFieldValue(f, target)) + .allMatch(Objects::isNull); + } + + private static Object getFieldValue(Field field, Object target) { + try { + return field.get(target); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } +} diff --git a/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/nullchecking/NullCheckUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/nullchecking/NullCheckUnitTest.java new file mode 100644 index 0000000000..db8aab8193 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/nullchecking/NullCheckUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.nullchecking; + +import org.apache.commons.lang3.ObjectUtils; +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class NullCheckUnitTest { + + @Test + public void givenNullFields_whenCheckForNullsUsingIfs_thenReturnCorrectValue(){ + Car car = new Car(); + + boolean result = car.allNull(); + + assertTrue(result); + } + + @Test + public void givenNullFields_whenCheckForNullsUsingStreams_thenReturnCorrectValue(){ + Car car = new Car(); + + boolean result = car.allNullV2(); + + assertTrue(result); + } + + @Test + public void givenNullFields_whenCheckForNullsUsingApacheCommons_thenReturnCorrectValue(){ + Car car = new Car(); + + boolean result = ObjectUtils.allNull(car.power, car.year); + + assertTrue(result); + } + + @Test + public void givenNullFields_whenCheckForNullsUsingReflection_thenReturnCorrectValue(){ + Car car = new Car(); + + boolean result = NullChecker.allNull(car); + + assertTrue(result); + } +}