diff --git a/core-java-modules/core-java-numbers-5/src/main/java/com/baeldung/IntegerNullOrZero.java b/core-java-modules/core-java-numbers-5/src/main/java/com/baeldung/IntegerNullOrZero.java new file mode 100644 index 0000000000..5fbc428b90 --- /dev/null +++ b/core-java-modules/core-java-numbers-5/src/main/java/com/baeldung/IntegerNullOrZero.java @@ -0,0 +1,26 @@ +package com.baeldung; + +import java.util.Optional; +import org.apache.commons.lang3.ObjectUtils; + +public class IntegerNullOrZero { + private IntegerNullOrZero() { + throw new RuntimeException("This class cannot be instantiated."); + } + + public static boolean usingStandardWay(Integer num) { + return num == null || num == 0; + } + + public static boolean usingTernaryOperator(Integer num) { + return 0 == (num == null ? 0 : num); + } + + public static boolean usingOptional(Integer num) { + return Optional.ofNullable(num).orElse(0) == 0; + } + + public static boolean usingObjectUtils(Integer num) { + return ObjectUtils.defaultIfNull(num, 0) == 0; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/intnullorzero/IntegerNullOrZeroUnitTest.java b/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/intnullorzero/IntegerNullOrZeroUnitTest.java new file mode 100644 index 0000000000..6926289536 --- /dev/null +++ b/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/intnullorzero/IntegerNullOrZeroUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.intnullorzero; + +import static com.baeldung.IntegerNullOrZero.usingObjectUtils; +import static com.baeldung.IntegerNullOrZero.usingOptional; +import static com.baeldung.IntegerNullOrZero.usingStandardWay; +import static com.baeldung.IntegerNullOrZero.usingTernaryOperator; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +public class IntegerNullOrZeroUnitTest { + + @Test + void givenInts_whenUsingStandardWay_thenGetExpectedResult() { + int n0 = 0; + boolean result0 = usingStandardWay(n0); + assertTrue(result0); + + boolean resultNull = usingStandardWay(null); + assertTrue(resultNull); + + int n42 = 42; + boolean result42 = usingStandardWay(n42); + assertFalse(result42); + } + + @Test + void givenInts_whenUsingTernaryOperator_thenGetExpectedResult() { + int n0 = 0; + boolean result0 = usingTernaryOperator(n0); + assertTrue(result0); + + boolean resultNull = usingTernaryOperator(null); + assertTrue(resultNull); + + int n42 = 42; + boolean result42 = usingTernaryOperator(n42); + assertFalse(result42); + } + + @Test + void givenInts_whenUsingOptional_thenGetExpectedResult() { + int n0 = 0; + boolean result0 = usingOptional(n0); + assertTrue(result0); + + boolean resultNull = usingOptional(null); + assertTrue(resultNull); + + int n42 = 42; + boolean result42 = usingOptional(n42); + assertFalse(result42); + } + + @Test + void givenInts_whenUsingObjectUtils_thenGetExpectedResult() { + int n0 = 0; + boolean result0 = usingObjectUtils(n0); + assertTrue(result0); + + boolean resultNull = usingObjectUtils(null); + assertTrue(resultNull); + + int n42 = 42; + boolean result42 = usingObjectUtils(n42); + assertFalse(result42); + } +} \ No newline at end of file