[int-zero-or-null] Check if an Integer Value is null or Zero in Java (#13332)
This commit is contained in:
parent
f75949b0e4
commit
7605f8474c
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user