BAEL-5521 Convert boolean to int in Java (#12017)

* added class for Article examples, created unit tests and modified pom in order to call Apache Commons outside test

* changed parameter to prevent NPE

Co-authored-by: Christian Jaimes <christian.jaimes@oracle>
This commit is contained in:
chrisjaimes 2022-05-14 05:29:19 -04:00 committed by GitHub
parent 557dba376b
commit 538c116302
3 changed files with 97 additions and 1 deletions

View File

@ -23,7 +23,6 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>

View File

@ -0,0 +1,42 @@
package com.baeldung.booleantoint;
import org.apache.commons.lang3.BooleanUtils;
public class BooleanToInt {
public static int booleanPrimitiveToInt(boolean foo) {
int bar = 0;
if (foo) {
bar = 1;
}
return bar;
}
public static int booleanPrimitiveToIntTernary(boolean foo) {
return (foo) ? 1 : 0;
}
public static int booleanObjectToInt(boolean foo) {
return Boolean.compare(foo, false);
}
public static int booleanObjectToIntInverse(boolean foo) {
return Boolean.compare(foo, true) + 1;
}
public static int booleanObjectMethodToInt(Boolean foo) {
return foo.compareTo(false);
}
public static int booleanObjectMethodToIntInverse(Boolean foo) {
return foo.compareTo(true) + 1;
}
public static int booleanUtilsToInt(Boolean foo) {
return BooleanUtils.toInteger(foo);
}
public static int bitwiseBooleanToInt(Boolean foo) {
return (Boolean.hashCode(foo) >> 1) & 1;
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.booleantoint;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class BooleanToIntUnitTest {
@Test
void givenBooleanPrimitiveValue_ThenReturnInt() {
assertEquals(1, BooleanToInt.booleanPrimitiveToInt(true));
assertEquals(0, BooleanToInt.booleanPrimitiveToInt(false));
}
@Test
void givenBooleanPrimitiveValue_ThenReturnIntTernary() {
assertEquals(1, BooleanToInt.booleanPrimitiveToIntTernary(true));
assertEquals(0, BooleanToInt.booleanPrimitiveToIntTernary(false));
}
@Test
void givenBooleanObject_ThenReturnInt() {
assertEquals(0, BooleanToInt.booleanObjectToInt(false));
assertEquals(1, BooleanToInt.booleanObjectToInt(true));
}
@Test
void givenBooleanObject_ThenReturnIntInverse() {
assertEquals(0, BooleanToInt.booleanObjectToIntInverse(false));
assertEquals(1, BooleanToInt.booleanObjectToIntInverse(true));
}
@Test
void givenBooleanObject_ThenReturnIntUsingClassMethod() {
assertEquals(0, BooleanToInt.booleanObjectMethodToInt(false));
assertEquals(1, BooleanToInt.booleanObjectMethodToInt(true));
}
@Test
void givenBooleanObject_ThenReturnIntUsingClassMethodInverse() {
assertEquals(0, BooleanToInt.booleanObjectMethodToIntInverse(false));
assertEquals(1, BooleanToInt.booleanObjectMethodToIntInverse(true));
}
@Test
void givenBoolean_ThenReturnIntUsingBooleanUtils() {
assertEquals(0, BooleanToInt.booleanUtilsToInt(false));
assertEquals(1, BooleanToInt.booleanUtilsToInt(true));
}
@Test
void givenBoolean_ThenReturnIntUsingBitwiseOperators() {
assertEquals(0, BooleanToInt.bitwiseBooleanToInt(false));
assertEquals(1, BooleanToInt.bitwiseBooleanToInt(true));
}
}